hibernate 4 testing work
This commit is contained in:
		
							parent
							
								
									34fe047d3b
								
							
						
					
					
						commit
						15f753ce55
					
				| @ -0,0 +1,90 @@ | |||||||
|  | package org.baeldung.persistence.model; | ||||||
|  | 
 | ||||||
|  | import java.io.Serializable; | ||||||
|  | import java.util.List; | ||||||
|  | 
 | ||||||
|  | import javax.persistence.Column; | ||||||
|  | import javax.persistence.Entity; | ||||||
|  | import javax.persistence.GeneratedValue; | ||||||
|  | import javax.persistence.GenerationType; | ||||||
|  | import javax.persistence.Id; | ||||||
|  | 
 | ||||||
|  | @Entity | ||||||
|  | public class Bar implements Serializable { | ||||||
|  | 
 | ||||||
|  |     @Id | ||||||
|  |     @GeneratedValue(strategy = GenerationType.AUTO) | ||||||
|  |     private long id; | ||||||
|  | 
 | ||||||
|  |     @Column(nullable = false) | ||||||
|  |     private String name; | ||||||
|  | 
 | ||||||
|  |     private List<Foo> foos; | ||||||
|  | 
 | ||||||
|  |     public Bar() { | ||||||
|  |         super(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public Bar(final String name) { | ||||||
|  |         super(); | ||||||
|  | 
 | ||||||
|  |         this.name = name; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // API | ||||||
|  | 
 | ||||||
|  |     public long getId() { | ||||||
|  |         return id; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setId(final long id) { | ||||||
|  |         this.id = id; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public String getName() { | ||||||
|  |         return name; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setName(final String name) { | ||||||
|  |         this.name = name; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public List<Foo> getFooList() { | ||||||
|  |         return foos; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public int hashCode() { | ||||||
|  |         final int prime = 31; | ||||||
|  |         int result = 1; | ||||||
|  |         result = prime * result + ((name == null) ? 0 : name.hashCode()); | ||||||
|  |         return result; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public boolean equals(final Object obj) { | ||||||
|  |         if (this == obj) | ||||||
|  |             return true; | ||||||
|  |         if (obj == null) | ||||||
|  |             return false; | ||||||
|  |         if (getClass() != obj.getClass()) | ||||||
|  |             return false; | ||||||
|  |         final Bar other = (Bar) obj; | ||||||
|  |         if (name == null) { | ||||||
|  |             if (other.name != null) | ||||||
|  |                 return false; | ||||||
|  |         } else if (!name.equals(other.name)) | ||||||
|  |             return false; | ||||||
|  |         return true; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public String toString() { | ||||||
|  |         final StringBuilder builder = new StringBuilder(); | ||||||
|  |         builder.append("Foo [name=").append(name).append("]"); | ||||||
|  |         return builder.toString(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -3,7 +3,6 @@ package org.baeldung.persistence.service; | |||||||
| import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; | import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; | ||||||
| 
 | 
 | ||||||
| import org.baeldung.persistence.model.Foo; | import org.baeldung.persistence.model.Foo; | ||||||
| import org.baeldung.persistence.service.IFooService; |  | ||||||
| import org.baeldung.spring.PersistenceConfig; | import org.baeldung.spring.PersistenceConfig; | ||||||
| import org.junit.Ignore; | import org.junit.Ignore; | ||||||
| import org.junit.Test; | import org.junit.Test; | ||||||
|  | |||||||
| @ -1,464 +1,390 @@ | |||||||
| package org.baeldung.persistence.service; | package org.baeldung.persistence.service; | ||||||
| 
 | 
 | ||||||
| import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; | import static org.junit.Assert.assertEquals; | ||||||
|  | import static org.junit.Assert.assertNull; | ||||||
|  | import static org.junit.Assert.assertTrue; | ||||||
| 
 | 
 | ||||||
| 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; |  | ||||||
| 
 |  | ||||||
| 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.List; | ||||||
| import java.util.Set; | import java.util.Set; | ||||||
| import java.util.SortedSet; |  | ||||||
| import java.util.TreeSet; | import java.util.TreeSet; | ||||||
| 
 | 
 | ||||||
| import javax.persistence.EntityManager; | import org.baeldung.persistence.model.Bar; | ||||||
| import javax.persistence.EntityManagerFactory; | import org.baeldung.persistence.model.Foo; | ||||||
| import javax.persistence.EntityTransaction; | import org.baeldung.spring.PersistenceConfig; | ||||||
| import javax.persistence.Persistence; |  | ||||||
| 
 |  | ||||||
| import org.hibernate.Criteria; | import org.hibernate.Criteria; | ||||||
| import org.hibernate.NullPrecedence; | import org.hibernate.NullPrecedence; | ||||||
| import org.hibernate.Query; | import org.hibernate.Query; | ||||||
| import org.hibernate.Session; | import org.hibernate.Session; | ||||||
| import org.hibernate.SessionFactory; | import org.hibernate.SessionFactory; | ||||||
| import org.hibernate.Transaction; | import org.hibernate.Transaction; | ||||||
| import org.hibernate.annotations.common.util.StringHelper; |  | ||||||
| import org.hibernate.cfg.Configuration; | import org.hibernate.cfg.Configuration; | ||||||
| import org.hibernate.criterion.Order; | import org.hibernate.criterion.Order; | ||||||
| import org.junit.BeforeClass; |  | ||||||
| import org.junit.Test; | import org.junit.Test; | ||||||
|  | import org.junit.runner.RunWith; | ||||||
|  | import org.springframework.test.context.ContextConfiguration; | ||||||
|  | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||||||
|  | import org.springframework.test.context.support.AnnotationConfigContextLoader; | ||||||
|  | 
 | ||||||
|  | import com.google.common.collect.Lists; | ||||||
|  | 
 | ||||||
| @RunWith(SpringJUnit4ClassRunner.class) | @RunWith(SpringJUnit4ClassRunner.class) | ||||||
| @ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) | @ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| public class FooSortingServiceTest { | public class FooSortingServiceTest { | ||||||
| 
 | 
 | ||||||
|  |     // tests | ||||||
| 
 | 
 | ||||||
| 	@Test |     @Test | ||||||
| 	public final void whenHQlSortingByOneAttribute_thenPrintSortedResults() { |     public final void whenHQlSortingByOneAttribute_thenPrintSortedResults() { | ||||||
| 		Session sess = null; |         Session sess = null; | ||||||
| 		List<Foo> fooList = new ArrayList(); |         List<Foo> fooList = Lists.newArrayList(); | ||||||
| 
 | 
 | ||||||
| 	    try{ |         try { | ||||||
| 	      SessionFactory sf = new Configuration(). |             final SessionFactory sf = new Configuration().configure().buildSessionFactory(); | ||||||
| 	          configure().buildSessionFactory(); |             sess = sf.openSession(); | ||||||
| 	      sess = sf.openSession(); |             final String hql = "FROM Foo f ORDER BY f.name"; | ||||||
| 	      String hql = "FROM Foo f ORDER BY f.name";	       |             final Query query = sess.createQuery(hql); | ||||||
| 	      Query query = sess.createQuery(hql); |             fooList = query.list(); | ||||||
| 	      fooList = query.list(); |             for (final Foo foo : fooList) { | ||||||
| 	      for(Foo foo: fooList){ |                 System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); | ||||||
| 	        System.out.println( |             } | ||||||
| 	            "Name: " + foo.getName() |             final Transaction tr = sess.beginTransaction(); | ||||||
| 	            + ", Id: " + foo.getId()	             |             tr.commit(); | ||||||
| 	          ); |         } catch (final Exception ex) { | ||||||
| 	      } |             ex.printStackTrace(); | ||||||
| 	      Transaction tr = sess.beginTransaction(); |         } finally { | ||||||
| 	      tr.commit(); |             if (sess != null) { | ||||||
| 	    }catch(Exception ex){ |                 sess.close(); | ||||||
| 	      ex.printStackTrace(); |             } | ||||||
| 	    }finally{ |         } | ||||||
| 	      if(sess != null){ |  | ||||||
| 	        sess.close(); |  | ||||||
| 	      } |  | ||||||
| 	    } |  | ||||||
| 
 | 
 | ||||||
| 	} |     } | ||||||
| 	@Test |  | ||||||
| 	public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSortedResults() { |  | ||||||
| 		Session sess = null; |  | ||||||
| 		List<Foo> fooList = new ArrayList(); |  | ||||||
| 
 | 
 | ||||||
| 	    try{ |     @Test | ||||||
| 	      SessionFactory sf = new Configuration(). |     public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSortedResults() { | ||||||
| 	          configure().buildSessionFactory(); |         Session sess = null; | ||||||
| 	      sess = sf.openSession(); |         List<Foo> fooList = Lists.newArrayList(); | ||||||
| 	      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() |  | ||||||
| 
 | 
 | ||||||
| 	          ); |         try { | ||||||
| 	      } |             final SessionFactory sf = new Configuration().configure().buildSessionFactory(); | ||||||
| 	      Transaction tr = sess.beginTransaction(); |             sess = sf.openSession(); | ||||||
| 	      tr.commit(); |             final String hql = "FROM Foo f ORDER BY f.name ASC"; | ||||||
| 	    }catch(Exception ex){ |             final Query query = sess.createQuery(hql); | ||||||
| 	      ex.printStackTrace(); |             fooList = query.list(); | ||||||
| 	    }finally{ |             for (final Foo foo : fooList) { | ||||||
| 	      if(sess != null){ |                 System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); | ||||||
| 	        sess.close(); |             } | ||||||
| 	      } |             final Transaction tr = sess.beginTransaction(); | ||||||
| 	    } |             tr.commit(); | ||||||
|  |         } catch (final Exception ex) { | ||||||
|  |             ex.printStackTrace(); | ||||||
|  |         } finally { | ||||||
|  |             if (sess != null) { | ||||||
|  |                 sess.close(); | ||||||
|  |             } | ||||||
|  |         } | ||||||
| 
 | 
 | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	@Test |     @Test | ||||||
| 	public final void whenHQlSortingByMultipleAttributes_thenSortedResults() { |     public final void whenHQlSortingByMultipleAttributes_thenSortedResults() { | ||||||
| 		Session sess = null; |         Session sess = null; | ||||||
| 		List<Foo> fooList = new ArrayList(); |         List<Foo> fooList = Lists.newArrayList(); | ||||||
| 
 | 
 | ||||||
| 	    try{ |         try { | ||||||
| 	      SessionFactory sf = new Configuration(). |             final SessionFactory sf = new Configuration().configure().buildSessionFactory(); | ||||||
| 	          configure().buildSessionFactory(); |             sess = sf.openSession(); | ||||||
| 	      sess = sf.openSession(); |             final String hql = "FROM Foo f ORDER BY f.name, f.id"; | ||||||
| 	      String hql = "FROM Foo f ORDER BY f.name, f.id"; |             final Query query = sess.createQuery(hql); | ||||||
| 	      Query query = sess.createQuery(hql); |             fooList = query.list(); | ||||||
| 	      fooList = query.list(); |             for (final Foo foo : fooList) { | ||||||
| 	      for(Foo foo: fooList){ |                 System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); | ||||||
| 	        System.out.println( |             } | ||||||
| 	            "Name: " + foo.getName() |             final Transaction tr = sess.beginTransaction(); | ||||||
| 	            + ", Id: " + foo.getId() |             tr.commit(); | ||||||
|  |         } catch (final Exception ex) { | ||||||
|  |             ex.printStackTrace(); | ||||||
|  |         } finally { | ||||||
|  |             if (sess != null) { | ||||||
|  |                 sess.close(); | ||||||
|  |             } | ||||||
|  |         } | ||||||
| 
 | 
 | ||||||
| 	          ); |     } | ||||||
| 	      } |  | ||||||
| 	      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<Foo> fooList = Lists.newArrayList(); | ||||||
| 
 | 
 | ||||||
| 	@Test |         try { | ||||||
| 	public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrintSortedOrderedResults() { |             final SessionFactory sf = new Configuration().configure().buildSessionFactory(); | ||||||
| 		Session sess = null; |             sess = sf.openSession(); | ||||||
| 		List<Foo> fooList = new ArrayList(); |             final String hql = "FROM Foo f ORDER BY f.name DESC, f.id ASC"; | ||||||
|  |             final Query query = sess.createQuery(hql); | ||||||
|  |             fooList = query.list(); | ||||||
|  |             for (final Foo foo : fooList) { | ||||||
|  |                 System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); | ||||||
|  |             } | ||||||
|  |             final Transaction tr = sess.beginTransaction(); | ||||||
|  |             tr.commit(); | ||||||
|  |         } catch (final Exception ex) { | ||||||
|  |             ex.printStackTrace(); | ||||||
|  |         } finally { | ||||||
|  |             if (sess != null) { | ||||||
|  |                 sess.close(); | ||||||
|  |             } | ||||||
|  |         } | ||||||
| 
 | 
 | ||||||
| 	    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() |  | ||||||
| 
 | 
 | ||||||
| 	          ); |     @Test | ||||||
| 	      } |     public final void whenCriteriaSortingByOneAttr_thenPrintSortedResults() { | ||||||
| 	      Transaction tr = sess.beginTransaction(); |         Session sess = null; | ||||||
| 	      tr.commit(); |         final SessionFactory sf = new Configuration().configure().buildSessionFactory(); | ||||||
| 	    }catch(Exception ex){ |         sess = sf.openSession(); | ||||||
| 	      ex.printStackTrace(); |         List<Foo> fooList = Lists.newArrayList(); | ||||||
| 	    }finally{ |         try { | ||||||
| 	      if(sess != null){ |             sess.beginTransaction(); | ||||||
| 	        sess.close(); |             final 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 (final Foo foo : fooList) { | ||||||
|  |                 System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName()); | ||||||
|  |             } | ||||||
| 
 | 
 | ||||||
| 	} |             sess.getTransaction().commit(); | ||||||
| 	@Test |         } catch (final Exception ex) { | ||||||
| 	public final void whenCriteriaSortingByOneAttr_thenPrintSortedResults() { |             ex.printStackTrace(); | ||||||
| 		Session sess = null; |         } finally { | ||||||
| 		SessionFactory sf = new Configuration(). |             if (sess != null) { | ||||||
| 				configure().buildSessionFactory(); |                 sess.close(); | ||||||
| 		sess = sf.openSession(); |             } | ||||||
| 		List<Foo> 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() |  | ||||||
| 
 | 
 | ||||||
| 						); |     @Test | ||||||
| 			} |     public final void whenCriteriaSortingByMultipAttr_thenSortedResults() { | ||||||
|  |         Session sess = null; | ||||||
|  |         final SessionFactory sf = new Configuration().configure().buildSessionFactory(); | ||||||
|  |         sess = sf.openSession(); | ||||||
|  |         List<Foo> fooList = Lists.newArrayList(); | ||||||
|  |         try { | ||||||
|  |             sess.beginTransaction(); | ||||||
|  |             final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); | ||||||
|  |             criteria.addOrder(Order.asc("name")); | ||||||
|  |             criteria.addOrder(Order.asc("id")); | ||||||
|  |             fooList = criteria.list(); | ||||||
|  |             for (final Foo foo : fooList) { | ||||||
|  |                 System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName()); | ||||||
|  |             } | ||||||
| 
 | 
 | ||||||
| 			sess.getTransaction().commit(); |             sess.getTransaction().commit(); | ||||||
| 		}catch(Exception ex){ |         } catch (final Exception ex) { | ||||||
| 			ex.printStackTrace(); |             ex.printStackTrace(); | ||||||
| 		}finally{ |         } finally { | ||||||
| 			if(sess != null){ |             if (sess != null) { | ||||||
| 				sess.close(); |                 sess.close(); | ||||||
| 			} |             } | ||||||
| 		} |         } | ||||||
| 	} |     } | ||||||
| 	@Test |  | ||||||
| 	public final void whenCriteriaSortingByMultipAttr_thenSortedResults() { |  | ||||||
| 		Session sess = null; |  | ||||||
| 		SessionFactory sf = new Configuration(). |  | ||||||
| 				configure().buildSessionFactory(); |  | ||||||
| 		sess = sf.openSession(); |  | ||||||
| 		List<Foo> 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() |  | ||||||
| 
 | 
 | ||||||
| 				); |     @Test | ||||||
|  |     public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() { | ||||||
|  |         Session sess = null; | ||||||
|  |         final SessionFactory sf = new Configuration().configure().buildSessionFactory(); | ||||||
|  |         sess = sf.openSession(); | ||||||
|  |         List<Foo> fooList = Lists.newArrayList(); | ||||||
|  |         try { | ||||||
|  |             sess.beginTransaction(); | ||||||
|  |             final 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 (final Foo foo : fooList) { | ||||||
|  |                 System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName()); | ||||||
|  |             } | ||||||
|  |             sess.getTransaction().commit(); | ||||||
|  |         } catch (final Exception ex) { | ||||||
|  |             ex.printStackTrace(); | ||||||
|  |         } finally { | ||||||
|  |             if (sess != null) { | ||||||
|  |                 sess.close(); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
| 			} |     @Test | ||||||
|  |     public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() { | ||||||
|  |         Session sess = null; | ||||||
|  |         final SessionFactory sf = new Configuration().configure().buildSessionFactory(); | ||||||
|  |         sess = sf.openSession(); | ||||||
|  |         List<Foo> fooList = Lists.newArrayList(); | ||||||
|  |         try { | ||||||
|  |             sess.beginTransaction(); | ||||||
|  |             final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); | ||||||
|  |             criteria.addOrder(Order.desc("name").nulls(NullPrecedence.FIRST)); | ||||||
|  |             fooList = criteria.list(); | ||||||
|  |             assertNull(fooList.get(0).getName()); | ||||||
|  |             for (final Foo foo : fooList) { | ||||||
|  |                 System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName()); | ||||||
|  |             } | ||||||
|  |             sess.getTransaction().commit(); | ||||||
|  |         } catch (final Exception ex) { | ||||||
|  |             ex.printStackTrace(); | ||||||
|  |         } finally { | ||||||
|  |             if (sess != null) { | ||||||
|  |                 sess.close(); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
| 			sess.getTransaction().commit(); |     @Test | ||||||
| 		}catch(Exception ex){ |     public final void whenHQlSortingByStringNullLast_thenLastNull() { | ||||||
| 			ex.printStackTrace(); |         Session sess = null; | ||||||
| 		}finally{ |         List<Foo> fooList = Lists.newArrayList(); | ||||||
| 			if(sess != null){ |  | ||||||
| 				sess.close(); |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| 	@Test |  | ||||||
| 	public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() { |  | ||||||
| 		Session sess = null; |  | ||||||
| 		SessionFactory sf = new Configuration(). |  | ||||||
| 				configure().buildSessionFactory(); |  | ||||||
| 		sess = sf.openSession(); |  | ||||||
| 		List<Foo> 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() |  | ||||||
| 
 | 
 | ||||||
| 				); |         try { | ||||||
|  |             final SessionFactory sf = new Configuration().configure().buildSessionFactory(); | ||||||
|  |             sess = sf.openSession(); | ||||||
|  |             final String hql = "FROM Foo f ORDER BY f.name NULLS LAST"; | ||||||
| 
 | 
 | ||||||
| 			} |             final Query query = sess.createQuery(hql); | ||||||
| 			sess.getTransaction().commit(); |             fooList = query.list(); | ||||||
| 		}catch(Exception ex){ |             assertNull(fooList.get(fooList.toArray().length - 1).getName()); | ||||||
| 			ex.printStackTrace(); |             for (final Foo foo : fooList) { | ||||||
| 		}finally{ |                 System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); | ||||||
| 			if(sess != null){ |             } | ||||||
| 				sess.close(); |             final Transaction tr = sess.beginTransaction(); | ||||||
| 			} |             tr.commit(); | ||||||
| 		} |         } catch (final Exception ex) { | ||||||
| 	} |             ex.printStackTrace(); | ||||||
| 	@Test |         } finally { | ||||||
| 	public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() { |             if (sess != null) { | ||||||
| 		Session sess = null; |                 sess.close(); | ||||||
| 		SessionFactory sf = new Configuration(). |             } | ||||||
| 				configure().buildSessionFactory(); |         } | ||||||
| 		sess = sf.openSession(); |  | ||||||
| 		List<Foo> 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() |  | ||||||
| 
 | 
 | ||||||
| 				); |     } | ||||||
| 
 | 
 | ||||||
| 			} |     @Test | ||||||
| 			sess.getTransaction().commit(); |     public final void whenSortingBars_thenBarsWithSortedFoos() { | ||||||
| 		}catch(Exception ex){ |         Session sess = null; | ||||||
| 			ex.printStackTrace(); |         final Set<Foo> fooList = new TreeSet(); | ||||||
| 		}finally{ |         List<Bar> barList = Lists.newArrayList(); | ||||||
| 			if(sess != null){ |  | ||||||
| 				sess.close(); |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| 	@Test |  | ||||||
| 	public final void whenHQlSortingByStringNullLast_thenLastNull() { |  | ||||||
| 		Session sess = null; |  | ||||||
| 		List<Foo> fooList = new ArrayList(); |  | ||||||
| 
 | 
 | ||||||
| 	    try{ |         try { | ||||||
| 	      SessionFactory sf = new Configuration(). |             final SessionFactory sf = new Configuration().configure().buildSessionFactory(); | ||||||
| 	          configure().buildSessionFactory(); |             sess = sf.openSession(); | ||||||
| 	      sess = sf.openSession(); |             final String hql = "FROM Bar b ORDER BY b.id"; | ||||||
| 	      String hql = "FROM Foo f ORDER BY f.name NULLS LAST"; |             final Query query = sess.createQuery(hql); | ||||||
|  |             barList = query.list(); | ||||||
| 
 | 
 | ||||||
| 	      Query query = sess.createQuery(hql); |             for (final Bar bar : barList) { | ||||||
| 	      fooList = query.list(); |                 System.out.println("Bar Id:" + bar.getId()); | ||||||
| 	      assertNull(fooList.get(fooList.toArray().length-1).getName()); |                 for (final Foo foo : bar.getFooList()) { | ||||||
| 	      for(Foo foo: fooList){ |                     System.out.println("FooName:" + foo.getName()); | ||||||
| 	        System.out.println( |                 } | ||||||
| 	            "Name: " + foo.getName() |             } | ||||||
| 	            + ", Id: " + foo.getId() |             final Transaction tr = sess.beginTransaction(); | ||||||
|  |             tr.commit(); | ||||||
|  |         } catch (final Exception ex) { | ||||||
|  |             ex.printStackTrace(); | ||||||
|  |         } finally { | ||||||
|  |             if (sess != null) { | ||||||
|  |                 sess.close(); | ||||||
|  |             } | ||||||
|  |         } | ||||||
| 
 | 
 | ||||||
| 	          ); |     } | ||||||
| 	      } |  | ||||||
| 	      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<Foo> fooList = new ArrayList(); | ||||||
|  |     // final List<Bar> barList = new ArrayList(); | ||||||
|  |     // | ||||||
|  |     // try { | ||||||
|  |     // final SessionFactory sf = new Configuration().configure().buildSessionFactory(); | ||||||
|  |     // sess = sf.openSession(); | ||||||
|  |     // final String hql = "FROM Foo f ORDER BY f.idx"; | ||||||
|  |     // final Query query = sess.createQuery(hql); | ||||||
|  |     // boolean thrown = false; | ||||||
|  |     // try { | ||||||
|  |     // fooList = criteria.list(); | ||||||
|  |     // } catch (final org.hibernate.PropertyAccessException e) { | ||||||
|  |     // thrown = true; | ||||||
|  |     // } | ||||||
|  |     // assertTrue(thrown); | ||||||
|  |     // | ||||||
|  |     // final Transaction tr = sess.beginTransaction(); | ||||||
|  |     // tr.commit(); | ||||||
|  |     // } catch (final Exception ex) { | ||||||
|  |     // ex.printStackTrace(); | ||||||
|  |     // } finally { | ||||||
|  |     // if (sess != null) { | ||||||
|  |     // sess.close(); | ||||||
|  |     // } | ||||||
|  |     // } | ||||||
|  |     // } | ||||||
| 
 | 
 | ||||||
|  |     @Test | ||||||
|  |     public final void whenSortingStringNullsLast_thenReturnNullsLast() { | ||||||
|  |         Session sess = null; | ||||||
|  |         List<Foo> fooList = Lists.newArrayList(); | ||||||
|  |         final List<Bar> barList = Lists.newArrayList(); | ||||||
| 
 | 
 | ||||||
| 	@Test |         try { | ||||||
| 	public final void whenSortingBars_thenBarsWithSortedFoos(){ |             final SessionFactory sf = new Configuration().configure().buildSessionFactory(); | ||||||
| 		Session sess = null; |             sess = sf.openSession(); | ||||||
| 		Set <Foo> fooList = new TreeSet(); |             final String hql = "FROM Foo f ORDER BY f.name NULLS LAST"; | ||||||
| 	    List <Bar> barList = new ArrayList(); |             final Query query = sess.createQuery(hql); | ||||||
|  |             fooList = query.list(); | ||||||
|  |             assertNull(fooList.get(fooList.toArray().length - 1).getName()); | ||||||
|  |             for (final Foo foo : fooList) { | ||||||
|  |                 System.out.println("FooIDX:" + foo.getName()); | ||||||
| 
 | 
 | ||||||
| 	    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){ |             final Transaction tr = sess.beginTransaction(); | ||||||
|  |             tr.commit(); | ||||||
|  |         } catch (final Exception ex) { | ||||||
|  |             ex.printStackTrace(); | ||||||
|  |         } finally { | ||||||
|  |             if (sess != null) { | ||||||
|  |                 sess.close(); | ||||||
|  |             } | ||||||
|  |         } | ||||||
| 
 | 
 | ||||||
| 	    	System.out.println("Bar Id:"+bar.getId()); |     } | ||||||
| 			for(Foo foo:bar.getFooList()){ |  | ||||||
| 				System.out.println("FooName:"+foo.getName()); |  | ||||||
| 
 | 
 | ||||||
| 			 } |     @Test | ||||||
| 		  } |     public final void whenNullPrimitiveValueCriteriaSortingByMultipAttr_thenException() { | ||||||
| 	      Transaction tr = sess.beginTransaction(); |         Session sess = null; | ||||||
| 	      tr.commit(); |         final SessionFactory sf = new Configuration().configure().buildSessionFactory(); | ||||||
| 	    }catch(Exception ex){ |         sess = sf.openSession(); | ||||||
| 	      ex.printStackTrace(); |         List<Foo> fooList = Lists.newArrayList(); | ||||||
| 	    }finally{ |         try { | ||||||
| 	      if(sess != null){ |             sess.beginTransaction(); | ||||||
| 	        sess.close(); |             final 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(); | ||||||
| 	@Test |             } catch (final org.hibernate.PropertyAccessException e) { | ||||||
| 	public final void whenSortingPrimitiveNulls_thenException(){ |                 thrown = true; | ||||||
| 		Session sess = null; |             } | ||||||
| 		List <Foo> fooList = new ArrayList(); |             assertTrue(thrown); | ||||||
| 	    List <Bar> 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 <Foo> fooList = new ArrayList(); |  | ||||||
| 	    List <Bar> 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<Foo> 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(); |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
| 
 | 
 | ||||||
|  |             sess.getTransaction().commit(); | ||||||
|  |         } catch (final Exception ex) { | ||||||
|  |             ex.printStackTrace(); | ||||||
|  |         } finally { | ||||||
|  |             if (sess != null) { | ||||||
|  |                 sess.close(); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user