From 1c2e4f37f27caf3b5feb45b9551de2c9311da3a9 Mon Sep 17 00:00:00 2001 From: egmp777 Date: Sun, 13 Apr 2014 19:51:00 -0500 Subject: [PATCH 1/6] SortingTestsForArticle --- .../service/FooServiceSortingTests.java | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingTests.java diff --git a/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingTests.java b/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingTests.java new file mode 100644 index 0000000000..85d158c1ad --- /dev/null +++ b/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingTests.java @@ -0,0 +1,72 @@ +package org.baeldung.persistence.service; + +import static org.junit.Assert.*; + +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.Persistence; +import javax.persistence.Query; + +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.cc.jpa.example.Foo; +public class FooServiceSortingTests { +private EntityManager entityManager; + + @BeforeClass + public static void before(){ + + + } + + @After + public final void after() { + + } + + @Test + public final void whenSortingByOneAttributeDefault_thenSortedResult() { + + + Query sortQuery = entityManager.createQuery + ("Select f from Foo as f order by f.id"); + List fooList = sortQuery.getResultList(); + for(Foo foo:fooList){ + System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId()); + } + + } + + @Test + public final void whenSortingByOneAttribute_thenSortedResult() { + + Query sortQuery = entityManager.createQuery + ("Select f from Foo as f order by f.id desc"); + List fooList = sortQuery.getResultList(); + for(Foo foo:fooList){ + System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId()); + } + + } + + @Test + public final void whenSortingByTwoAttributes_thenSortedResult() { + + Query sortQuery = entityManager.createQuery + ("Select f from Foo as f order by f.name asc, f.id desc"); + List fooList = sortQuery.getResultList(); + for(Foo foo:fooList){ + System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId()); + } + + } + +} From d244b373b8812df0d5d29e387b935d25d34afd63 Mon Sep 17 00:00:00 2001 From: egmp777 Date: Sat, 19 Apr 2014 17:52:21 -0500 Subject: [PATCH 2/6] All Tets --- .../service/FooServiceSortingTests.java | 44 +++++++++++++++---- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingTests.java b/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingTests.java index 85d158c1ad..32c5fd3789 100644 --- a/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingTests.java +++ b/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingTests.java @@ -32,16 +32,15 @@ private EntityManager entityManager; } - @Test - public final void whenSortingByOneAttributeDefault_thenSortedResult() { + @Test + public final void whenSortingByOneAttributeDefault_thenSortedResult() { - - Query sortQuery = entityManager.createQuery - ("Select f from Foo as f order by f.id"); - List fooList = sortQuery.getResultList(); - for(Foo foo:fooList){ + Query sortQuery = entityManager.createQuery + ("Select f from Foo as f order by f.id"); + List fooList = sortQuery.getResultList(); + for(Foo foo:fooList){ System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId()); - } + } } @@ -68,5 +67,34 @@ private EntityManager entityManager; } } + + @Test + public final void whenSortingFooWithCriteria_thenSortedFoos(){ + + Root from = criteriaQuery.from(Foo.class); + CriteriaQuery select = criteriaQuery.select(from); + criteriaQuery.orderBy(criteriaBuilder.asc(from.get("name"))); + TypedQuery typedQuery = entityManager.createQuery(select); + ListfooList = typedQuery.getResultList(); + for(Foo foo:fooList){ + System.out.println("Name:"+foo.getName()+"--------Id:"+foo.getId()); + } + + } + + @Test + public final void whenSortingFooWithCriteriaAndMultipleAttributes_thenSortedFoos(){ + + Root from = criteriaQuery.from(Foo.class); + CriteriaQuery select = criteriaQuery.select(from); + criteriaQuery.orderBy(criteriaBuilder.asc(from.get("name")), criteriaBuilder.desc(from.get("id"))); + TypedQuery typedQuery = entityManager.createQuery(select); + ListfooList = typedQuery.getResultList(); + for(Foo foo:fooList){ + System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId()); + } + } + + } From 845cad104e830095beff20ab747b0c50dd287aff Mon Sep 17 00:00:00 2001 From: egmp777 Date: Wed, 30 Apr 2014 12:58:51 -0500 Subject: [PATCH 3/6] All JPA Sorting Tests --- .../service/FooServiceSortingTests.java | 240 +++++++++++++----- 1 file changed, 181 insertions(+), 59 deletions(-) diff --git a/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingTests.java b/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingTests.java index 32c5fd3789..d7af8f4dda 100644 --- a/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingTests.java +++ b/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingTests.java @@ -7,94 +7,216 @@ import java.util.List; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; +import javax.persistence.OrderBy; import javax.persistence.Persistence; import javax.persistence.Query; +import javax.persistence.TypedQuery; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Order; +import javax.persistence.criteria.Root; import org.hibernate.Session; import org.hibernate.SessionFactory; +import org.hibernate.annotations.common.util.StringHelper; import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import com.cc.jpa.example.Foo; +import com.cc.jpa.example.Bar; + + public class FooServiceSortingTests { -private EntityManager entityManager; - + private EntityManager entityManager; + @BeforeClass public static void before(){ - - + + } + + @After + public final void after() { + } - @After - public final void after() { - - } - - @Test - public final void whenSortingByOneAttributeDefault_thenSortedResult() { - - Query sortQuery = entityManager.createQuery - ("Select f from Foo as f order by f.id"); - List fooList = sortQuery.getResultList(); - for(Foo foo:fooList){ - System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId()); - } - - } - @Test - public final void whenSortingByOneAttribute_thenSortedResult() { - + public final void whenSortingByOneAttributeDefaultOrder_thenPrintSortedResult() { + + EntityManagerFactory emf = Persistence + .createEntityManagerFactory("punit"); + EntityManager entityManager = emf.createEntityManager(); + EntityTransaction entityTransaction = entityManager + .getTransaction(); + entityTransaction.begin(); + String jql = "Select f from Foo as f order by f.id"; Query sortQuery = entityManager.createQuery - ("Select f from Foo as f order by f.id desc"); + (jql); + List fooList = sortQuery.getResultList(); + assertEquals(1,fooList.get(0).getId()); + assertEquals(100,fooList.get(fooList.toArray().length-1).getId()); + for(Foo foo:fooList){ + System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId()); + } + + } + + @Test + public final void whenSortingByOneAttributeSetOrder_thenSortedPrintResult() { + + EntityManagerFactory emf = Persistence + .createEntityManagerFactory("punit"); + EntityManager entityManager = emf.createEntityManager(); + EntityTransaction entityTransaction = entityManager + .getTransaction(); + entityTransaction.begin(); + String jql = "Select f from Foo as f order by f.id desc"; + Query sortQuery = entityManager.createQuery + (jql); + List fooList = sortQuery.getResultList(); + assertEquals(100,fooList.get(0).getId()); + assertEquals(1,fooList.get(fooList.toArray().length-1).getId()); + for(Foo foo:fooList){ + System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId()); + } + + } + + @Test + public final void whenSortingByTwoAttributes_thenPrintSortedResult() { + + EntityManagerFactory emf = Persistence + .createEntityManagerFactory("punit"); + EntityManager entityManager = emf.createEntityManager(); + EntityTransaction entityTransaction = entityManager + .getTransaction(); + entityTransaction.begin(); + String jql = "Select f from Foo as f order by f.name asc, f.id desc"; + Query sortQuery = entityManager.createQuery + (jql); List fooList = sortQuery.getResultList(); for(Foo foo:fooList){ System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId()); } + + } + + @Test + public final void whenSortinfBar_thenPrintBarsSortedWithFoos(){ + EntityManagerFactory emf = Persistence + .createEntityManagerFactory("punit"); + EntityManager entityManager = emf.createEntityManager(); + EntityTransaction entityTransaction = entityManager + .getTransaction(); + entityTransaction.begin(); + String jql = "Select b from Bar as b order by b.id"; + + Query barQuery = entityManager.createQuery(jql); + ListbarList = barQuery.getResultList(); + for(Bar bar:barList){ + System.out.println("Bar Id:"+bar.getId()); + for(Foo foo:bar.getFooList()){ + System.out.println("FooName:"+foo.getName()); + } + } + + } + + @Test + public final void whenSortingByStringNullLast_thenLastNull() { + + EntityManagerFactory emf = Persistence + .createEntityManagerFactory("punit"); + EntityManager entityManager = emf.createEntityManager(); + EntityTransaction entityTransaction = entityManager + .getTransaction(); + entityTransaction.begin(); + String jql = "Select f from Foo as f order by f.name desc NULLS LAST"; + Query sortQuery = entityManager.createQuery + (jql); + List fooList = sortQuery.getResultList(); + assertNull(fooList.get(fooList.toArray().length-1).getName()); + for(Foo foo:fooList){ + System.out.println("Name:"+foo.getName()); + } + } + @Test + public final void whenSortingByStringNullFirst_thenFirstNull() { + + EntityManagerFactory emf = Persistence + .createEntityManagerFactory("punit"); + EntityManager entityManager = emf.createEntityManager(); + EntityTransaction entityTransaction = entityManager + .getTransaction(); + entityTransaction.begin(); + String jql = "Select f from Foo as f order by f.name desc NULLS FIRST"; + Query sortQuery = entityManager.createQuery + (jql); + List fooList = sortQuery.getResultList(); + assertNull(fooList.get(0).getName()); + for(Foo foo:fooList){ + System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getTest_Null()); + } + } + @Test + public final void whenSortingByIntNull_thenException() { + + EntityManagerFactory emf = Persistence + .createEntityManagerFactory("punit"); + EntityManager entityManager = emf.createEntityManager(); + EntityTransaction entityTransaction = entityManager + .getTransaction(); + entityTransaction.begin(); + String jql = "Select f from Foo as f order by f.test_Null desc NULLS FIRST"; + Query sortQuery = entityManager.createQuery + (jql); + boolean thrown = false; + try { + List fooList = sortQuery.getResultList(); + } catch (javax.persistence.PersistenceException e) { + thrown = true; + } + assertTrue(thrown); + } + + @Test + public final void whenSortingFooWithCriteria_thenPrintSortedFoos(){ + + EntityManagerFactory emf = Persistence + .createEntityManagerFactory("punit"); + EntityManager entityManager = emf.createEntityManager(); + CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); + CriteriaQuery criteriaQuery = criteriaBuilder + .createQuery(Foo.class); + Root from = criteriaQuery.from(Foo.class); + CriteriaQuery select = criteriaQuery.select(from); + criteriaQuery.orderBy(criteriaBuilder.asc(from.get("name"))); + TypedQuery typedQuery = entityManager.createQuery(select); + ListfooList = typedQuery.getResultList(); + for(Foo foo:fooList){ + System.out.println("Name:"+foo.getName()+"--------Id:"+foo.getId()); + } } @Test - public final void whenSortingByTwoAttributes_thenSortedResult() { + public final void whenSortingFooWithCriteriaAndMultipleAttributes_thenPrintSortedFoos(){ - Query sortQuery = entityManager.createQuery - ("Select f from Foo as f order by f.name asc, f.id desc"); - List fooList = sortQuery.getResultList(); + EntityManagerFactory emf = Persistence + .createEntityManagerFactory("punit"); + EntityManager entityManager = emf.createEntityManager(); + CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); + CriteriaQuery criteriaQuery = criteriaBuilder + .createQuery(Foo.class); + Root from = criteriaQuery.from(Foo.class); + CriteriaQuery select = criteriaQuery.select(from); + criteriaQuery.orderBy(criteriaBuilder.asc(from.get("name")), criteriaBuilder.desc(from.get("id"))); + TypedQuery typedQuery = entityManager.createQuery(select); + ListfooList = typedQuery.getResultList(); for(Foo foo:fooList){ System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId()); } - } - - @Test - public final void whenSortingFooWithCriteria_thenSortedFoos(){ - - Root from = criteriaQuery.from(Foo.class); - CriteriaQuery select = criteriaQuery.select(from); - criteriaQuery.orderBy(criteriaBuilder.asc(from.get("name"))); - TypedQuery typedQuery = entityManager.createQuery(select); - ListfooList = typedQuery.getResultList(); - for(Foo foo:fooList){ - System.out.println("Name:"+foo.getName()+"--------Id:"+foo.getId()); - } - - } - - @Test - public final void whenSortingFooWithCriteriaAndMultipleAttributes_thenSortedFoos(){ - - Root from = criteriaQuery.from(Foo.class); - CriteriaQuery select = criteriaQuery.select(from); - criteriaQuery.orderBy(criteriaBuilder.asc(from.get("name")), criteriaBuilder.desc(from.get("id"))); - TypedQuery typedQuery = entityManager.createQuery(select); - ListfooList = typedQuery.getResultList(); - for(Foo foo:fooList){ - System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId()); - } - } - - - + } From 5dc96ceda43e86af352fc42e284ba00aec83dad0 Mon Sep 17 00:00:00 2001 From: egmp777 Date: Wed, 30 Apr 2014 20:50:08 -0500 Subject: [PATCH 4/6] All Hibernate Criteria and HQL Tests --- .../service/FooSortingServiceTest.java | 468 ++++++++++++++++++ 1 file changed, 468 insertions(+) create mode 100644 spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java diff --git a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java b/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java new file mode 100644 index 0000000000..55daf93ed6 --- /dev/null +++ b/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java @@ -0,0 +1,468 @@ +package org.baeldung.persistence.service; + +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; + +import org.baeldung.persistence.model.Foo; +import org.baeldung.persistence.service.IFooService; +import org.baeldung.spring.PersistenceConfig; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataAccessException; +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.dao.InvalidDataAccessApiUsageException; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) +package com.cc.code.samples.hibernate.sorting; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.Persistence; + +import org.hibernate.Criteria; +import org.hibernate.NullPrecedence; +import org.hibernate.Query; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.Transaction; +import org.hibernate.annotations.common.util.StringHelper; +import org.hibernate.cfg.Configuration; +import org.hibernate.criterion.Order; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.cc.example.hibernate.Foo; +import com.cc.example.hibernate.Bar; + +public class FooSortingServiceTest { + + + @Test + public final void whenHQlSortingByOneAttribute_thenPrintSortedResults() { + Session sess = null; + List fooList = new ArrayList(); + + try{ + SessionFactory sf = new Configuration(). + configure().buildSessionFactory(); + sess = sf.openSession(); + String hql = "FROM Foo f ORDER BY f.name"; + Query query = sess.createQuery(hql); + fooList = query.list(); + for(Foo foo: fooList){ + System.out.println( + "Name: " + foo.getName() + + ", Id: " + foo.getId() + + ); + } + Transaction tr = sess.beginTransaction(); + tr.commit(); + }catch(Exception ex){ + ex.printStackTrace(); + }finally{ + if(sess != null){ + sess.close(); + } + } + + } + @Test + public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSortedResults() { + Session sess = null; + List fooList = new ArrayList(); + + try{ + SessionFactory sf = new Configuration(). + configure().buildSessionFactory(); + sess = sf.openSession(); + String hql = "FROM Foo f ORDER BY f.name ASC"; + Query query = sess.createQuery(hql); + fooList = query.list(); + for(Foo foo: fooList){ + System.out.println( + "Name: " + foo.getName() + + ", Id: " + foo.getId() + + ); + } + Transaction tr = sess.beginTransaction(); + tr.commit(); + }catch(Exception ex){ + ex.printStackTrace(); + }finally{ + if(sess != null){ + sess.close(); + } + } + + } + + @Test + public final void whenHQlSortingByMultipleAttributes_thenSortedResults() { + Session sess = null; + List fooList = new ArrayList(); + + try{ + SessionFactory sf = new Configuration(). + configure().buildSessionFactory(); + sess = sf.openSession(); + String hql = "FROM Foo f ORDER BY f.name, f.id"; + Query query = sess.createQuery(hql); + fooList = query.list(); + for(Foo foo: fooList){ + System.out.println( + "Name: " + foo.getName() + + ", Id: " + foo.getId() + + ); + } + Transaction tr = sess.beginTransaction(); + tr.commit(); + }catch(Exception ex){ + ex.printStackTrace(); + }finally{ + if(sess != null){ + sess.close(); + } + } + + } + + @Test + public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrintSortedOrderedResults() { + Session sess = null; + List fooList = new ArrayList(); + + try{ + SessionFactory sf = new Configuration(). + configure().buildSessionFactory(); + sess = sf.openSession(); + String hql = "FROM Foo f ORDER BY f.name DESC, f.id ASC"; + Query query = sess.createQuery(hql); + fooList = query.list(); + for(Foo foo: fooList){ + System.out.println( + "Name: " + foo.getName() + + ", Id: " + foo.getId() + + ); + } + Transaction tr = sess.beginTransaction(); + tr.commit(); + }catch(Exception ex){ + ex.printStackTrace(); + }finally{ + if(sess != null){ + sess.close(); + } + } + + } + @Test + public final void whenCriteriaSortingByOneAttr_thenPrintSortedResults() { + Session sess = null; + SessionFactory sf = new Configuration(). + configure().buildSessionFactory(); + sess = sf.openSession(); + List fooList = new ArrayList(); + try{ + sess.beginTransaction(); + Criteria criteria = sess.createCriteria(Foo.class, "FOO"); + criteria.addOrder(Order.asc("id")); + fooList = criteria.list(); + assertEquals(1,fooList.get(0).getId()); + assertEquals(100,fooList.get(fooList.toArray().length-1).getId()); + for(Foo foo: fooList){ + System.out.println( + "Id: " + foo.getId() + + ", FirstName: " + foo.getName() + + ); + } + + sess.getTransaction().commit(); + }catch(Exception ex){ + ex.printStackTrace(); + }finally{ + if(sess != null){ + sess.close(); + } + } + } + @Test + public final void whenCriteriaSortingByMultipAttr_thenSortedResults() { + Session sess = null; + SessionFactory sf = new Configuration(). + configure().buildSessionFactory(); + sess = sf.openSession(); + List fooList = new ArrayList(); + try{ + sess.beginTransaction(); + Criteria criteria = sess.createCriteria(Foo.class, "FOO"); + criteria.addOrder(Order.asc("name")); + criteria.addOrder(Order.asc("id")); + fooList = criteria.list(); + for(Foo foo: fooList){ + System.out.println( + "Id: " + foo.getId() + + ", FirstName: " + foo.getName() + + ); + + } + + sess.getTransaction().commit(); + }catch(Exception ex){ + ex.printStackTrace(); + }finally{ + if(sess != null){ + sess.close(); + } + } + } + @Test + public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() { + Session sess = null; + SessionFactory sf = new Configuration(). + configure().buildSessionFactory(); + sess = sf.openSession(); + List fooList = new ArrayList(); + try{ + sess.beginTransaction(); + Criteria criteria = sess.createCriteria(Foo.class, "FOO"); + criteria.addOrder(Order.asc("name").nulls(NullPrecedence.LAST)); + fooList = criteria.list(); + assertNull(fooList.get(fooList.toArray().length-1).getName()); + for(Foo foo: fooList){ + System.out.println( + "Id: " + foo.getId() + + ", FirstName: " + foo.getName() + + ); + + } + sess.getTransaction().commit(); + }catch(Exception ex){ + ex.printStackTrace(); + }finally{ + if(sess != null){ + sess.close(); + } + } + } + @Test + public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() { + Session sess = null; + SessionFactory sf = new Configuration(). + configure().buildSessionFactory(); + sess = sf.openSession(); + List fooList = new ArrayList(); + try{ + sess.beginTransaction(); + Criteria criteria = sess.createCriteria(Foo.class, "FOO"); + criteria.addOrder(Order.desc("name").nulls(NullPrecedence.FIRST)); + fooList = criteria.list(); + assertNull(fooList.get(0).getName()); + for(Foo foo: fooList){ + System.out.println( + "Id: " + foo.getId() + + ", FirstName: " + foo.getName() + + ); + + } + sess.getTransaction().commit(); + }catch(Exception ex){ + ex.printStackTrace(); + }finally{ + if(sess != null){ + sess.close(); + } + } + } + @Test + public final void whenHQlSortingByStringNullLast_thenLastNull() { + Session sess = null; + List fooList = new ArrayList(); + + try{ + SessionFactory sf = new Configuration(). + configure().buildSessionFactory(); + sess = sf.openSession(); + String hql = "FROM Foo f ORDER BY f.name NULLS LAST"; + + Query query = sess.createQuery(hql); + fooList = query.list(); + assertNull(fooList.get(fooList.toArray().length-1).getName()); + for(Foo foo: fooList){ + System.out.println( + "Name: " + foo.getName() + + ", Id: " + foo.getId() + + ); + } + Transaction tr = sess.beginTransaction(); + tr.commit(); + }catch(Exception ex){ + ex.printStackTrace(); + }finally{ + if(sess != null){ + sess.close(); + } + } + + } + + + @Test + public final void whenSortingBars_thenBarsWithSortedFoos(){ + Session sess = null; + Set fooList = new TreeSet(); + List barList = new ArrayList(); + + try{ + SessionFactory sf = new Configuration(). + configure().buildSessionFactory(); + sess = sf.openSession(); + String hql = "FROM Bar b ORDER BY b.id"; + Query query = sess.createQuery(hql); + barList = query.list(); + + for(Bar bar:barList){ + + System.out.println("Bar Id:"+bar.getId()); + for(Foo foo:bar.getFooList()){ + System.out.println("FooName:"+foo.getName()); + + } + } + Transaction tr = sess.beginTransaction(); + tr.commit(); + }catch(Exception ex){ + ex.printStackTrace(); + }finally{ + if(sess != null){ + sess.close(); + } + } + + + } + @Test + public final void whenSortingPrimitiveNulls_thenException(){ + Session sess = null; + List fooList = new ArrayList(); + List barList = new ArrayList(); + + try{ + SessionFactory sf = new Configuration(). + configure().buildSessionFactory(); + sess = sf.openSession(); + String hql = "FROM Foo f ORDER BY f.idx"; + Query query = sess.createQuery(hql); + boolean thrown = false; + try { + fooList = criteria.list(); + } catch (org.hibernate.PropertyAccessException e) { + thrown = true; + } + assertTrue(thrown); + + Transaction tr = sess.beginTransaction(); + tr.commit(); + }catch(Exception ex){ + ex.printStackTrace(); + }finally{ + if(sess != null){ + sess.close(); + } + } + + + } + @Test + public final void whenSortingStringNullsLast_thenReturnNullsLast(){ + Session sess = null; + List fooList = new ArrayList(); + List barList = new ArrayList(); + + try{ + SessionFactory sf = new Configuration(). + configure().buildSessionFactory(); + sess = sf.openSession(); + String hql = "FROM Foo f ORDER BY f.name NULLS LAST"; + Query query = sess.createQuery(hql); + fooList = query.list(); + assertNull(fooList.get(fooList.toArray().length-1).getName()); + for(Foo foo:fooList){ + System.out.println("FooIDX:"+foo.getName()); + + } + + Transaction tr = sess.beginTransaction(); + tr.commit(); + }catch(Exception ex){ + ex.printStackTrace(); + }finally{ + if(sess != null){ + sess.close(); + } + } + + + } + @Test + public final void whenNullPrimitiveValueCriteriaSortingByMultipAttr_thenException() { + Session sess = null; + SessionFactory sf = new Configuration(). + configure().buildSessionFactory(); + sess = sf.openSession(); + List fooList = new ArrayList(); + try{ + sess.beginTransaction(); + Criteria criteria = sess.createCriteria(Foo.class, "FOO"); + criteria.addOrder(Order.desc("name").nulls(NullPrecedence.FIRST)); + criteria.addOrder(Order.asc("idx")); + boolean thrown = false; + try { + fooList = criteria.list(); + } catch (org.hibernate.PropertyAccessException e) { + thrown = true; + } + assertTrue(thrown); + + + sess.getTransaction().commit(); + }catch(Exception ex){ + ex.printStackTrace(); + }finally{ + if(sess != null){ + sess.close(); + } + } + } +} + +} From 782af18d95a307b77dd3544c7ead3f6ce720ac81 Mon Sep 17 00:00:00 2001 From: egmp777 Date: Wed, 30 Apr 2014 20:56:35 -0500 Subject: [PATCH 5/6] Deleted Local Package and imports --- .../persistence/service/FooSortingServiceTest.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java b/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java index 55daf93ed6..0ba6ccff9d 100644 --- a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java +++ b/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java @@ -16,10 +16,6 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.AnnotationConfigContextLoader; -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) -package com.cc.code.samples.hibernate.sorting; - import static org.junit.Assert.*; import org.junit.Before; @@ -51,9 +47,10 @@ import org.hibernate.cfg.Configuration; import org.hibernate.criterion.Order; import org.junit.BeforeClass; import org.junit.Test; +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) + -import com.cc.example.hibernate.Foo; -import com.cc.example.hibernate.Bar; public class FooSortingServiceTest { From 3886a06aed7935fa6ca0ecbbe01dbd7b4875351e Mon Sep 17 00:00:00 2001 From: egmp777 Date: Wed, 30 Apr 2014 20:59:38 -0500 Subject: [PATCH 6/6] Final Hibernate HQL and Criteria Tests --- .../baeldung/persistence/service/FooSortingServiceTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java b/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java index 0ba6ccff9d..e4011702b3 100644 --- a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java +++ b/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java @@ -70,8 +70,7 @@ public class FooSortingServiceTest { for(Foo foo: fooList){ System.out.println( "Name: " + foo.getName() - + ", Id: " + foo.getId() - + + ", Id: " + foo.getId() ); } Transaction tr = sess.beginTransaction();