minor cleanup work
This commit is contained in:
parent
ffb24cf312
commit
86eb549596
|
@ -28,10 +28,6 @@ import com.baeldung.hibernate.criteria.util.HibernateUtil;
|
|||
|
||||
public class ApplicationView {
|
||||
|
||||
public ApplicationView() {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public boolean checkIfCriteriaTimeLower() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
|
@ -181,8 +177,7 @@ public class ApplicationView {
|
|||
CriteriaBuilder builder = session.getCriteriaBuilder();
|
||||
CriteriaQuery<Item> criteriaItem = builder.createQuery(Item.class);
|
||||
Root<Item> rootItem = criteriaItem.from(Item.class);
|
||||
criteriaItem.select(rootItem).where(builder.isNull(rootItem.get("itemDescription")))
|
||||
.where(builder.like(rootItem.get("itemName"), "chair%"));
|
||||
criteriaItem.select(rootItem).where(builder.isNull(rootItem.get("itemDescription"))).where(builder.like(rootItem.get("itemName"), "chair%"));
|
||||
final List<Item> notNullItemsList = session.createQuery(criteriaItem).getResultList();
|
||||
final String notNullDescItems[] = new String[notNullItemsList.size()];
|
||||
for (int i = 0; i < notNullItemsList.size(); i++) {
|
||||
|
|
|
@ -15,9 +15,7 @@ public class HibernateUtil {
|
|||
Configuration configuration = new Configuration();
|
||||
configuration.addAnnotatedClass(Event.class);
|
||||
configuration.configure("immutable.cfg.xml");
|
||||
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
|
||||
.applySettings(configuration.getProperties())
|
||||
.build();
|
||||
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
|
||||
return configuration.buildSessionFactory(serviceRegistry);
|
||||
} catch (Throwable ex) {
|
||||
System.out.println("Initial SessionFactory creation failed." + ex);
|
||||
|
|
|
@ -8,8 +8,7 @@ import com.baeldung.persistence.dao.common.IOperations;
|
|||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Transactional(value = "hibernateTransactionManager")
|
||||
public abstract class AbstractHibernateAuditableService<T extends Serializable> extends AbstractHibernateService<T>
|
||||
implements IOperations<T>, IAuditOperations<T> {
|
||||
public abstract class AbstractHibernateAuditableService<T extends Serializable> extends AbstractHibernateService<T> implements IOperations<T>, IAuditOperations<T> {
|
||||
|
||||
@Override
|
||||
public List<T> getEntitiesAtRevision(final Number revision) {
|
||||
|
|
|
@ -7,8 +7,7 @@ import com.baeldung.persistence.dao.common.IOperations;
|
|||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Transactional(value = "hibernateTransactionManager")
|
||||
public abstract class AbstractHibernateService<T extends Serializable> extends AbstractService<T> implements
|
||||
IOperations<T> {
|
||||
public abstract class AbstractHibernateService<T extends Serializable> extends AbstractService<T> implements IOperations<T> {
|
||||
|
||||
@Override
|
||||
public T findOne(final long id) {
|
||||
|
|
|
@ -24,8 +24,7 @@ public class HibernateCriteriaIntegrationTest {
|
|||
@Test
|
||||
public void testLikeCriteriaQuery() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final List<Item> expectedLikeList = session.createQuery("From Item where itemName like '%chair%'")
|
||||
.getResultList();
|
||||
final List<Item> expectedLikeList = session.createQuery("From Item where itemName like '%chair%'").getResultList();
|
||||
final String expectedLikeItems[] = new String[expectedLikeList.size()];
|
||||
for (int i = 0; i < expectedLikeList.size(); i++) {
|
||||
expectedLikeItems[i] = expectedLikeList.get(i).getItemName();
|
||||
|
@ -37,8 +36,7 @@ public class HibernateCriteriaIntegrationTest {
|
|||
@Test
|
||||
public void testILikeCriteriaQuery() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final List<Item> expectedChairCaseList = session.createQuery("From Item where itemName like '%Chair%'")
|
||||
.getResultList();
|
||||
final List<Item> expectedChairCaseList = session.createQuery("From Item where itemName like '%Chair%'").getResultList();
|
||||
final String expectedChairCaseItems[] = new String[expectedChairCaseList.size()];
|
||||
for (int i = 0; i < expectedChairCaseList.size(); i++) {
|
||||
expectedChairCaseItems[i] = expectedChairCaseList.get(i).getItemName();
|
||||
|
@ -51,8 +49,7 @@ public class HibernateCriteriaIntegrationTest {
|
|||
@Test
|
||||
public void testNullCriteriaQuery() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final List<Item> expectedIsNullDescItemsList = session.createQuery("From Item where itemDescription is null")
|
||||
.getResultList();
|
||||
final List<Item> expectedIsNullDescItemsList = session.createQuery("From Item where itemDescription is null").getResultList();
|
||||
final String expectedIsNullDescItems[] = new String[expectedIsNullDescItemsList.size()];
|
||||
for (int i = 0; i < expectedIsNullDescItemsList.size(); i++) {
|
||||
expectedIsNullDescItems[i] = expectedIsNullDescItemsList.get(i).getItemName();
|
||||
|
@ -64,8 +61,7 @@ public class HibernateCriteriaIntegrationTest {
|
|||
@Test
|
||||
public void testIsNotNullCriteriaQuery() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final List<Item> expectedIsNotNullDescItemsList = session.createQuery(
|
||||
"From Item where itemDescription is not null").getResultList();
|
||||
final List<Item> expectedIsNotNullDescItemsList = session.createQuery("From Item where itemDescription is not null").getResultList();
|
||||
final String expectedIsNotNullDescItems[] = new String[expectedIsNotNullDescItemsList.size()];
|
||||
for (int i = 0; i < expectedIsNotNullDescItemsList.size(); i++) {
|
||||
expectedIsNotNullDescItems[i] = expectedIsNotNullDescItemsList.get(i).getItemName();
|
||||
|
@ -78,8 +74,7 @@ public class HibernateCriteriaIntegrationTest {
|
|||
@Test
|
||||
public void testAverageProjection() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final List<Double> expectedAvgProjItemsList = session.createQuery("Select avg(itemPrice) from Item item")
|
||||
.getResultList();
|
||||
final List<Double> expectedAvgProjItemsList = session.createQuery("Select avg(itemPrice) from Item item").getResultList();
|
||||
|
||||
final Double expectedAvgProjItems[] = new Double[expectedAvgProjItemsList.size()];
|
||||
for (int i = 0; i < expectedAvgProjItemsList.size(); i++) {
|
||||
|
@ -105,8 +100,7 @@ public class HibernateCriteriaIntegrationTest {
|
|||
@Test
|
||||
public void testOrCriteriaQuery() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final List<Item> expectedOrCritItemsList = session.createQuery(
|
||||
"From Item where itemPrice>1000 or itemName like 'Chair%'").getResultList();
|
||||
final List<Item> expectedOrCritItemsList = session.createQuery("From Item where itemPrice>1000 or itemName like 'Chair%'").getResultList();
|
||||
final String expectedOrCritItems[] = new String[expectedOrCritItemsList.size()];
|
||||
for (int i = 0; i < expectedOrCritItemsList.size(); i++) {
|
||||
expectedOrCritItems[i] = expectedOrCritItemsList.get(i).getItemName();
|
||||
|
@ -118,8 +112,7 @@ public class HibernateCriteriaIntegrationTest {
|
|||
@Test
|
||||
public void testAndCriteriaQuery() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final List<Item> expectedAndCritItemsList = session.createQuery(
|
||||
"From Item where itemPrice>1000 and itemName like 'Chair%'").getResultList();
|
||||
final List<Item> expectedAndCritItemsList = session.createQuery("From Item where itemPrice>1000 and itemName like 'Chair%'").getResultList();
|
||||
final String expectedAndCritItems[] = new String[expectedAndCritItemsList.size()];
|
||||
for (int i = 0; i < expectedAndCritItemsList.size(); i++) {
|
||||
expectedAndCritItems[i] = expectedAndCritItemsList.get(i).getItemName();
|
||||
|
@ -131,8 +124,7 @@ public class HibernateCriteriaIntegrationTest {
|
|||
@Test
|
||||
public void testMultiCriteriaQuery() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final List<Item> expectedMultiCritItemsList = session.createQuery(
|
||||
"From Item where itemDescription is null and itemName like'chair%'").getResultList();
|
||||
final List<Item> expectedMultiCritItemsList = session.createQuery("From Item where itemDescription is null and itemName like'chair%'").getResultList();
|
||||
final String expectedMultiCritItems[] = new String[expectedMultiCritItemsList.size()];
|
||||
for (int i = 0; i < expectedMultiCritItemsList.size(); i++) {
|
||||
expectedMultiCritItems[i] = expectedMultiCritItemsList.get(i).getItemName();
|
||||
|
@ -144,8 +136,7 @@ public class HibernateCriteriaIntegrationTest {
|
|||
@Test
|
||||
public void testSortCriteriaQuery() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final List<Item> expectedSortCritItemsList = session.createQuery(
|
||||
"From Item order by itemName asc, itemPrice desc").getResultList();
|
||||
final List<Item> expectedSortCritItemsList = session.createQuery("From Item order by itemName asc, itemPrice desc").getResultList();
|
||||
final String expectedSortCritItems[] = new String[expectedSortCritItemsList.size()];
|
||||
for (int i = 0; i < expectedSortCritItemsList.size(); i++) {
|
||||
expectedSortCritItems[i] = expectedSortCritItemsList.get(i).getItemName();
|
||||
|
@ -157,8 +148,7 @@ public class HibernateCriteriaIntegrationTest {
|
|||
@Test
|
||||
public void testGreaterThanCriteriaQuery() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final List<Item> expectedGreaterThanList = session.createQuery("From Item where itemPrice>1000")
|
||||
.getResultList();
|
||||
final List<Item> expectedGreaterThanList = session.createQuery("From Item where itemPrice>1000").getResultList();
|
||||
final String expectedGreaterThanItems[] = new String[expectedGreaterThanList.size()];
|
||||
for (int i = 0; i < expectedGreaterThanList.size(); i++) {
|
||||
expectedGreaterThanItems[i] = expectedGreaterThanList.get(i).getItemName();
|
||||
|
@ -182,8 +172,7 @@ public class HibernateCriteriaIntegrationTest {
|
|||
@Test
|
||||
public void betweenCriteriaQuery() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final List<Item> expectedBetweenList = session.createQuery("From Item where itemPrice between 100 and 200")
|
||||
.getResultList();
|
||||
final List<Item> expectedBetweenList = session.createQuery("From Item where itemPrice between 100 and 200").getResultList();
|
||||
final String expectedPriceBetweenItems[] = new String[expectedBetweenList.size()];
|
||||
for (int i = 0; i < expectedBetweenList.size(); i++) {
|
||||
expectedPriceBetweenItems[i] = expectedBetweenList.get(i).getItemName();
|
||||
|
|
|
@ -9,7 +9,7 @@ public class HibernateCriteriaTestRunner {
|
|||
public static void main(final String[] args) {
|
||||
Result result = JUnitCore.runClasses(HibernateCriteriaTestSuite.class);
|
||||
for (Failure failure : result.getFailures()) {
|
||||
|
||||
//
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,8 +45,7 @@ public class HibernateImmutableIntegrationTest {
|
|||
|
||||
@Test
|
||||
public void updateEvent() {
|
||||
Event event = (Event) session.createQuery(
|
||||
"FROM Event WHERE title='New Event'").list().get(0);
|
||||
Event event = (Event) session.createQuery("FROM Event WHERE title='New Event'").list().get(0);
|
||||
event.setTitle("Private Event");
|
||||
session.saveOrUpdate(event);
|
||||
session.getTransaction().commit();
|
||||
|
@ -54,16 +53,14 @@ public class HibernateImmutableIntegrationTest {
|
|||
|
||||
@Test
|
||||
public void deleteEvent() {
|
||||
Event event = (Event) session.createQuery(
|
||||
"FROM Event WHERE title='New Event'").list().get(0);
|
||||
Event event = (Event) session.createQuery("FROM Event WHERE title='New Event'").list().get(0);
|
||||
session.delete(event);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addGuest() {
|
||||
Event event = (Event) session.createQuery(
|
||||
"FROM Event WHERE title='New Event'").list().get(0);
|
||||
Event event = (Event) session.createQuery("FROM Event WHERE title='New Event'").list().get(0);
|
||||
String newGuest = "Sara";
|
||||
event.getGuestList().add(newGuest);
|
||||
|
||||
|
@ -74,8 +71,7 @@ public class HibernateImmutableIntegrationTest {
|
|||
|
||||
@Test
|
||||
public void deleteCascade() {
|
||||
Event event = (Event) session.createQuery(
|
||||
"FROM Event WHERE title='New Event'").list().get(0);
|
||||
Event event = (Event) session.createQuery("FROM Event WHERE title='New Event'").list().get(0);
|
||||
String guest = event.getGuestList().iterator().next();
|
||||
event.getGuestList().remove(guest);
|
||||
|
||||
|
|
|
@ -73,8 +73,7 @@ public class FooPaginationPersistenceIntegrationTest {
|
|||
public final void whenRetrievingPaginatedEntities_thenCorrectSize() {
|
||||
final int pageNumber = 1;
|
||||
final int pageSize = 10;
|
||||
final List<Foo> fooList = session.createQuery("From Foo").setFirstResult((pageNumber - 1) * pageSize)
|
||||
.setMaxResults(pageSize).getResultList();
|
||||
final List<Foo> fooList = session.createQuery("From Foo").setFirstResult((pageNumber - 1) * pageSize).setMaxResults(pageSize).getResultList();
|
||||
assertThat(fooList, hasSize(pageSize));
|
||||
}
|
||||
|
||||
|
@ -90,8 +89,7 @@ public class FooPaginationPersistenceIntegrationTest {
|
|||
final List<Foo> fooList = Lists.newArrayList();
|
||||
int totalEntities = 0;
|
||||
while (totalEntities < countResult) {
|
||||
fooList.addAll(session.createQuery("From Foo").setFirstResult((pageNumber - 1) * pageSize)
|
||||
.setMaxResults(pageSize).getResultList());
|
||||
fooList.addAll(session.createQuery("From Foo").setFirstResult((pageNumber - 1) * pageSize).setMaxResults(pageSize).getResultList());
|
||||
totalEntities = fooList.size();
|
||||
pageNumber++;
|
||||
}
|
||||
|
@ -106,8 +104,7 @@ public class FooPaginationPersistenceIntegrationTest {
|
|||
final Long countResults = (Long) session.createQuery(countQ).uniqueResult();
|
||||
final int lastPageNumber = (int) ((countResults / pageSize) + 1);
|
||||
|
||||
final List<Foo> lastPage = session.createQuery("From Foo").setFirstResult((lastPageNumber - 1) * pageSize)
|
||||
.setMaxResults(pageSize).getResultList();
|
||||
final List<Foo> lastPage = session.createQuery("From Foo").setFirstResult((lastPageNumber - 1) * pageSize).setMaxResults(pageSize).getResultList();
|
||||
|
||||
assertThat(lastPage, hasSize(lessThan(pageSize + 1)));
|
||||
}
|
||||
|
@ -147,8 +144,7 @@ public class FooPaginationPersistenceIntegrationTest {
|
|||
CriteriaQuery<Foo> criteriaItem = builder.createQuery(Foo.class);
|
||||
Root<Foo> rootItem = criteriaItem.from(Foo.class);
|
||||
criteriaItem.select(rootItem);
|
||||
final List<Foo> firstPage = session.createQuery(criteriaItem).setFirstResult(0).setMaxResults(pageSize)
|
||||
.getResultList();
|
||||
final List<Foo> firstPage = session.createQuery(criteriaItem).setFirstResult(0).setMaxResults(pageSize).getResultList();
|
||||
|
||||
assertThat(firstPage, hasSize(pageSize));
|
||||
}
|
||||
|
@ -175,8 +171,7 @@ public class FooPaginationPersistenceIntegrationTest {
|
|||
|
||||
int totalEntities = 0;
|
||||
while (totalEntities < count.intValue()) {
|
||||
fooList.addAll(session.createQuery(criteriaFoo).setFirstResult((pageNumber - 1) * pageSize)
|
||||
.setMaxResults(pageSize).getResultList());
|
||||
fooList.addAll(session.createQuery(criteriaFoo).setFirstResult((pageNumber - 1) * pageSize).setMaxResults(pageSize).getResultList());
|
||||
totalEntities = fooList.size();
|
||||
pageNumber++;
|
||||
}
|
||||
|
|
|
@ -11,8 +11,6 @@ import javax.persistence.criteria.CriteriaQuery;
|
|||
import javax.persistence.criteria.Order;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.hibernate.Criteria;
|
||||
import org.hibernate.NullPrecedence;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.junit.After;
|
||||
|
|
|
@ -37,14 +37,9 @@ public class SaveMethodsTest {
|
|||
|
||||
@BeforeClass
|
||||
public static void beforeTests() {
|
||||
Configuration configuration = new Configuration().addAnnotatedClass(Person.class)
|
||||
.setProperty("hibernate.dialect", HSQLDialect.class.getName())
|
||||
.setProperty("hibernate.connection.driver_class", org.hsqldb.jdbcDriver.class.getName())
|
||||
.setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:test")
|
||||
.setProperty("hibernate.connection.username", "sa").setProperty("hibernate.connection.password", "")
|
||||
.setProperty("hibernate.hbm2ddl.auto", "update");
|
||||
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
|
||||
configuration.getProperties()).build();
|
||||
Configuration configuration = new Configuration().addAnnotatedClass(Person.class).setProperty("hibernate.dialect", HSQLDialect.class.getName()).setProperty("hibernate.connection.driver_class", org.hsqldb.jdbcDriver.class.getName())
|
||||
.setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:test").setProperty("hibernate.connection.username", "sa").setProperty("hibernate.connection.password", "").setProperty("hibernate.hbm2ddl.auto", "update");
|
||||
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
|
||||
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
|
||||
}
|
||||
|
||||
|
|
|
@ -96,16 +96,14 @@ public class FooStoredProceduresIntegrationTest {
|
|||
fooService.create(new Foo("NewFooName"));
|
||||
|
||||
// Stored procedure getFoosByName using createSQLQuery()
|
||||
List<Foo> allFoosByName = session.createQuery("CALL GetFoosByName(:fooName)", Foo.class)
|
||||
.setParameter("fooName", "NewFooName").getResultList();
|
||||
List<Foo> allFoosByName = session.createQuery("CALL GetFoosByName(:fooName)", Foo.class).setParameter("fooName", "NewFooName").getResultList();
|
||||
for (Foo foo : allFoosByName) {
|
||||
LOGGER.info("getFoosByName() using SQL Query : found => {}", foo.toString());
|
||||
}
|
||||
|
||||
// Stored procedure getFoosByName using getNamedQuery()
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Foo> allFoosByName2 = session.getNamedQuery("callGetFoosByName").setParameter("fooName", "NewFooName")
|
||||
.getResultList();
|
||||
List<Foo> allFoosByName2 = session.getNamedQuery("callGetFoosByName").setParameter("fooName", "NewFooName").getResultList();
|
||||
for (Foo foo : allFoosByName2) {
|
||||
LOGGER.info("getFoosByName() using Native Query : found => {}", foo.toString());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue