From 730ebd57afc3b8f71bf360a24b47b7df57415368 Mon Sep 17 00:00:00 2001 From: egmp777 Date: Fri, 9 May 2014 15:45:46 -0500 Subject: [PATCH 01/12] Update FooSortingServiceTest.java --- .../service/FooSortingServiceTest.java | 460 ++++++------------ 1 file changed, 145 insertions(+), 315 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 9fc80b8621..9d6f3c355a 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 @@ -1,390 +1,220 @@ package org.baeldung.persistence.service; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +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; +import static org.junit.Assert.*; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.After; import java.util.List; import java.util.Set; -import java.util.TreeSet; - -import org.baeldung.persistence.model.Bar; -import org.baeldung.persistence.model.Foo; -import org.baeldung.spring.PersistenceConfig; 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.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.criterion.Order; -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; +import com.cc.example.hibernate.Foo; +import com.cc.example.hibernate.Bar; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) -public class FooSortingServiceTest { - // tests + + +public class FooSortingServiceTest { + private static Configuration configuration; + private static StandardServiceRegistryBuilder builder; + private static SessionFactory sf; + private static Session sess; + + @BeforeClass + public static void before() { + + configuration = new Configuration().configure(); + builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()); + sf = configuration.buildSessionFactory(builder.build()); + sess = sf.openSession(); + sess.beginTransaction(); + } + @After + public void after() { + + } @Test public final void whenHQlSortingByOneAttribute_thenPrintSortedResults() { - Session sess = null; - List fooList = Lists.newArrayList(); - try { - final SessionFactory sf = new Configuration().configure().buildSessionFactory(); - sess = sf.openSession(); - final String hql = "FROM Foo f ORDER BY f.name"; - 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(); - } + final String hql = "FROM Foo f ORDER BY f.name"; + final Query query = sess.createQuery(hql); + final List fooList = query.list(); + for (final Foo foo : fooList) { + System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); } + sess.getTransaction().commit(); + + } + + @Test + public final void whenHQlSortingByStringNullLast_thenLastNull() { + + final String hql = "FROM Foo f ORDER BY f.name NULLS LAST"; + final Query query = sess.createQuery(hql); + final List fooList = query.list(); + assertNull(fooList.get(fooList.toArray().length - 1).getName()); + for (final Foo foo : fooList) { + System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); + } + sess.getTransaction().commit(); + + } + + @Test + public final void whenSortingByStringNullsFirst_thenReturnNullsFirst() { + + final String hql = "FROM Foo f ORDER BY f.name NULLS FIRST"; + final Query query = sess.createQuery(hql); + final List fooList = query.list(); + assertNull(fooList.get(0).getName()); + for (final Foo foo : fooList) { + System.out.println("Name:" + foo.getName()); + + } + sess.getTransaction().commit(); } @Test public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSortedResults() { - Session sess = null; - List fooList = Lists.newArrayList(); - try { - final SessionFactory sf = new Configuration().configure().buildSessionFactory(); - sess = sf.openSession(); - final String hql = "FROM Foo f ORDER BY f.name 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(); - } + final String hql = "FROM Foo f ORDER BY f.name ASC"; + final Query query = sess.createQuery(hql); + final List fooList = query.list(); + for (final Foo foo : fooList) { + System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId() + + ); } + sess.getTransaction().commit(); } @Test public final void whenHQlSortingByMultipleAttributes_thenSortedResults() { - Session sess = null; - List fooList = Lists.newArrayList(); - try { - final SessionFactory sf = new Configuration().configure().buildSessionFactory(); - sess = sf.openSession(); - final String hql = "FROM Foo f ORDER BY f.name, f.id"; - 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(); - } + final String hql = "FROM Foo f ORDER BY f.name, f.id"; + final Query query = sess.createQuery(hql); + final List fooList = query.list(); + for (final Foo foo : fooList) { + System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId() + + ); } + sess.getTransaction().commit(); } @Test - public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrintSortedOrderedResults() { - Session sess = null; - List fooList = Lists.newArrayList(); + public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrintSortedResults() { - try { - final SessionFactory sf = new Configuration().configure().buildSessionFactory(); - sess = sf.openSession(); - 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(); - } + final String hql = "FROM Foo f ORDER BY f.name DESC, f.id ASC"; + final Query query = sess.createQuery(hql); + final List fooList = query.list(); + for (final Foo foo : fooList) { + System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); } + sess.getTransaction().commit(); } @Test - public final void whenCriteriaSortingByOneAttr_thenPrintSortedResults() { - Session sess = null; - final SessionFactory sf = new Configuration().configure().buildSessionFactory(); - sess = sf.openSession(); - List fooList = Lists.newArrayList(); - try { - sess.beginTransaction(); - 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()); - } + public final void whenHQLCriteriaSortingByOneAttr_thenPrintSortedResults() { - sess.getTransaction().commit(); - } catch (final Exception ex) { - ex.printStackTrace(); - } finally { - if (sess != null) { - sess.close(); - } + final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); + criteria.addOrder(Order.asc("id")); + final List fooList = criteria.list(); + for (final Foo foo : fooList) { + System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName()); } + sess.getTransaction().commit(); + } @Test - public final void whenCriteriaSortingByMultipAttr_thenSortedResults() { - Session sess = null; - final SessionFactory sf = new Configuration().configure().buildSessionFactory(); - sess = sf.openSession(); - List 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()); - } + public final void whenHQLCriteriaSortingByMultipAttr_thenSortedResults() { - sess.getTransaction().commit(); - } catch (final Exception ex) { - ex.printStackTrace(); - } finally { - if (sess != null) { - sess.close(); - } + final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); + criteria.addOrder(Order.asc("name")); + criteria.addOrder(Order.asc("id")); + final List fooList = criteria.list(); + for (final Foo foo : fooList) { + System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName()); } + sess.getTransaction().commit(); + } @Test public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() { - Session sess = null; - final SessionFactory sf = new Configuration().configure().buildSessionFactory(); - sess = sf.openSession(); - List 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(); - } + + final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); + criteria.addOrder(Order.asc("name").nulls(NullPrecedence.LAST)); + final List 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(); + } @Test public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() { - Session sess = null; - final SessionFactory sf = new Configuration().configure().buildSessionFactory(); - sess = sf.openSession(); - List 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(); - } - } - } - - @Test - public final void whenHQlSortingByStringNullLast_thenLastNull() { - Session sess = null; - List fooList = Lists.newArrayList(); - - 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); - fooList = query.list(); - assertNull(fooList.get(fooList.toArray().length - 1).getName()); - 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(); - } + + final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); + criteria.addOrder(Order.desc("name").nulls(NullPrecedence.FIRST)); + final List 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(); } @Test public final void whenSortingBars_thenBarsWithSortedFoos() { - Session sess = null; - final Set fooList = new TreeSet(); - List barList = Lists.newArrayList(); - try { - final SessionFactory sf = new Configuration().configure().buildSessionFactory(); - sess = sf.openSession(); - final String hql = "FROM Bar b ORDER BY b.id"; - final Query query = sess.createQuery(hql); - barList = query.list(); + final String hql = "FROM Bar b ORDER BY b.id"; + final Query query = sess.createQuery(hql); + final List barList = query.list(); + for (final Bar bar : barList) { + final Set fooSet = bar.getFooList(); + System.out.println("Bar Id:" + bar.getId()); + for (final Foo foo : fooSet) { + System.out.println("FooName:" + foo.getName()); - for (final Bar bar : barList) { - System.out.println("Bar Id:" + bar.getId()); - for (final Foo foo : bar.getFooList()) { - System.out.println("FooName:" + foo.getName()); - } - } - final Transaction tr = sess.beginTransaction(); - tr.commit(); - } catch (final Exception ex) { - ex.printStackTrace(); - } finally { - if (sess != null) { - sess.close(); } } + sess.getTransaction().commit(); } - // @Test - // public final void whenSortingPrimitiveNulls_thenException() { - // Session sess = null; - // List fooList = new ArrayList(); - // final List 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 fooList = Lists.newArrayList(); - final List barList = Lists.newArrayList(); - - 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); - fooList = query.list(); - assertNull(fooList.get(fooList.toArray().length - 1).getName()); - for (final Foo foo : fooList) { - System.out.println("FooIDX:" + foo.getName()); - - } - - final Transaction tr = sess.beginTransaction(); - tr.commit(); - } catch (final Exception ex) { - ex.printStackTrace(); - } finally { - if (sess != null) { - sess.close(); - } - } - - } - - @Test - public final void whenNullPrimitiveValueCriteriaSortingByMultipAttr_thenException() { - Session sess = null; - final SessionFactory sf = new Configuration().configure().buildSessionFactory(); - sess = sf.openSession(); - List fooList = Lists.newArrayList(); - try { - sess.beginTransaction(); - 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(); - } catch (final org.hibernate.PropertyAccessException e) { - thrown = true; - } - assertTrue(thrown); - - sess.getTransaction().commit(); - } catch (final Exception ex) { - ex.printStackTrace(); - } finally { - if (sess != null) { - sess.close(); - } - } - } } From 80cb34833ca1b4698ea4de4746884fe6ceacfc26 Mon Sep 17 00:00:00 2001 From: egmp777 Date: Fri, 9 May 2014 15:48:57 -0500 Subject: [PATCH 02/12] Update Bar.java --- .../org/baeldung/persistence/model/Bar.java | 68 ++++++++++--------- 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.java b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.java index ddc60bf0c9..0489dbc21a 100644 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.java +++ b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.java @@ -1,60 +1,63 @@ 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; +import java.util.Set; +import com.google.common.collect.Sets; +import javax.persistence.*; +import org.hibernate.annotations.OrderBy; @Entity +@NamedQuery(name = "Bar.findAll", query = "SELECT b FROM Bar b") public class Bar implements Serializable { - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private long id; - - @Column(nullable = false) - private String name; - - private List foos; - public Bar() { super(); } public Bar(final String name) { super(); - + this.name = name; } - // API + private static final long serialVersionUID = 1L; + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private int id; - public long getId() { - return id; + private String name; + + @OneToMany(mappedBy = "bar", cascade = CascadeType.ALL, fetch = FetchType.EAGER) + @OrderBy(clause = "NAME DESC") + Set fooSet = Sets.newHashSet(); + + //API + + public Set getFooSet() { + return fooSet; } - public void setId(final long id) { + public void setFooList(Set fooSet) { + this.fooSet = fooSet; + } + + public int getId() { + return this.id; + } + + public void setId(int id) { this.id = id; } public String getName() { - return name; + return this.name; } - public void setName(final String name) { + public void setName(String name) { this.name = name; } - - public List getFooList() { - return foos; - } - // - + @Override public int hashCode() { final int prime = 31; @@ -62,7 +65,7 @@ public class Bar implements Serializable { result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } - + @Override public boolean equals(final Object obj) { if (this == obj) @@ -79,12 +82,13 @@ public class Bar implements Serializable { return false; return true; } - + @Override public String toString() { final StringBuilder builder = new StringBuilder(); - builder.append("Foo [name=").append(name).append("]"); + builder.append("Bar [name=").append(name).append("]"); return builder.toString(); } + } From 82ccad7146e0d734edc33273df9cd1b56546f718 Mon Sep 17 00:00:00 2001 From: egmp777 Date: Fri, 9 May 2014 15:51:04 -0500 Subject: [PATCH 03/12] Update Bar.java --- .../org/baeldung/persistence/model/Bar.java | 48 +++++++++++-------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/spring-jpa/src/main/java/org/baeldung/persistence/model/Bar.java b/spring-jpa/src/main/java/org/baeldung/persistence/model/Bar.java index ddc60bf0c9..b3e789b9ce 100644 --- a/spring-jpa/src/main/java/org/baeldung/persistence/model/Bar.java +++ b/spring-jpa/src/main/java/org/baeldung/persistence/model/Bar.java @@ -1,60 +1,66 @@ 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; +import javax.persistence.CascadeType; @Entity public class Bar implements Serializable { - + @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; - + @Column(nullable = false) private String name; - - private List foos; - + + @OneToMany(mappedBy = "bar", fetch = FetchType.EAGER, cascade = CascadeType.ALL) + @OrderBy("name ASC") + List fooList; + 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 getFooList() { - return foos; + return fooList; } - + + public void setFooList(final List fooList) { + this.fooList = fooList; + } + // - + @Override public int hashCode() { final int prime = 31; @@ -62,7 +68,7 @@ public class Bar implements Serializable { result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } - + @Override public boolean equals(final Object obj) { if (this == obj) @@ -79,12 +85,12 @@ public class Bar implements Serializable { return false; return true; } - + @Override public String toString() { final StringBuilder builder = new StringBuilder(); - builder.append("Foo [name=").append(name).append("]"); + builder.append("Bar [name=").append(name).append("]"); return builder.toString(); } - + } From 554df1148d0ca5132195078ffe3e2735ee9af58e Mon Sep 17 00:00:00 2001 From: egmp777 Date: Fri, 9 May 2014 15:52:41 -0500 Subject: [PATCH 04/12] Update FooServiceSortingTests.java --- .../service/FooServiceSortingTests.java | 126 ++++++++---------- 1 file changed, 53 insertions(+), 73 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 0cc8645af8..e1ecbd2015 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 @@ -1,11 +1,7 @@ package org.baeldung.persistence.service; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - +import static org.junit.Assert.*; import java.util.List; - import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; @@ -15,82 +11,79 @@ import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Root; - -import org.baeldung.persistence.model.Bar; -import org.baeldung.persistence.model.Foo; -import org.junit.After; +import com.google.common.collect.Lists; 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 static EntityManager entityManager; + private static EntityManagerFactory emf; + private static EntityTransaction entityTransaction; + private static CriteriaBuilder criteriaBuilder; @BeforeClass public static void before() { - // - } - @After - public final void after() { - // + emf = Persistence.createEntityManagerFactory("punit"); + entityManager = emf.createEntityManager(); + entityTransaction = entityManager.getTransaction(); + entityTransaction.begin(); + criteriaBuilder = entityManager.getCriteriaBuilder(); } @Test public final void whenSortingByOneAttributeDefaultOrder_thenPrintSortedResult() { - final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit"); - final EntityManager entityManager = emf.createEntityManager(); - final EntityTransaction entityTransaction = entityManager.getTransaction(); - entityTransaction.begin(); final String jql = "Select f from Foo as f order by f.id"; final Query sortQuery = entityManager.createQuery(jql); final List fooList = sortQuery.getResultList(); - assertEquals(1, fooList.get(0).getId()); - assertEquals(100, fooList.get(fooList.toArray().length - 1).getId()); for (final Foo foo : fooList) { System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId()); } + } @Test public final void whenSortingByOneAttributeSetOrder_thenSortedPrintResult() { - final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit"); - final EntityManager entityManager = emf.createEntityManager(); - final EntityTransaction entityTransaction = entityManager.getTransaction(); - entityTransaction.begin(); final String jql = "Select f from Foo as f order by f.id desc"; final Query sortQuery = entityManager.createQuery(jql); final List fooList = sortQuery.getResultList(); - assertEquals(100, fooList.get(0).getId()); - assertEquals(1, fooList.get(fooList.toArray().length - 1).getId()); for (final Foo foo : fooList) { System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId()); } + } @Test public final void whenSortingByTwoAttributes_thenPrintSortedResult() { - final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit"); - final EntityManager entityManager = emf.createEntityManager(); - final EntityTransaction entityTransaction = entityManager.getTransaction(); - entityTransaction.begin(); + final String jql = "Select f from Foo as f order by f.name asc, f.id desc"; final Query sortQuery = entityManager.createQuery(jql); final List fooList = sortQuery.getResultList(); for (final Foo foo : fooList) { System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId()); } + + } + + @Test + public final void whenSortingFooByBar_thenBarsSorted() { + + final String jql = "Select f from Foo as f order by f.name, f.bar.id"; + final Query barJoinQuery = entityManager.createQuery(jql); + final List fooList = barJoinQuery.getResultList(); + for (final Foo foo : fooList) { + System.out.println("Name:" + foo.getName() + "-------BarId:" + foo.getBar().getId()); + } } @Test public final void whenSortinfBar_thenPrintBarsSortedWithFoos() { - final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit"); - final EntityManager entityManager = emf.createEntityManager(); - final EntityTransaction entityTransaction = entityManager.getTransaction(); - entityTransaction.begin(); - final String jql = "Select b from Bar as b order by b.id"; + final String jql = "Select b from Bar as b order by b.id"; final Query barQuery = entityManager.createQuery(jql); final List barList = barQuery.getResultList(); for (final Bar bar : barList) { @@ -99,15 +92,12 @@ public class FooServiceSortingTests { System.out.println("FooName:" + foo.getName()); } } + } @Test public final void whenSortingByStringNullLast_thenLastNull() { - final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit"); - final EntityManager entityManager = emf.createEntityManager(); - final EntityTransaction entityTransaction = entityManager.getTransaction(); - entityTransaction.begin(); final String jql = "Select f from Foo as f order by f.name desc NULLS LAST"; final Query sortQuery = entityManager.createQuery(jql); final List fooList = sortQuery.getResultList(); @@ -117,43 +107,34 @@ public class FooServiceSortingTests { } } - // @Test - // public final void whenSortingByStringNullFirst_thenFirstNull() { - // final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit"); - // final EntityManager entityManager = emf.createEntityManager(); - // final EntityTransaction entityTransaction = entityManager.getTransaction(); - // entityTransaction.begin(); - // final String jql = "Select f from Foo as f order by f.name desc NULLS FIRST"; - // final Query sortQuery = entityManager.createQuery(jql); - // final List fooList = sortQuery.getResultList(); - // assertNull(fooList.get(0).getName()); - // for (final Foo foo : fooList) { - // System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getTest_Null()); - // } - // } - @Test - public final void whenSortingByIntNull_thenException() { - final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit"); - final EntityManager entityManager = emf.createEntityManager(); - final EntityTransaction entityTransaction = entityManager.getTransaction(); - entityTransaction.begin(); - final String jql = "Select f from Foo as f order by f.test_Null desc NULLS FIRST"; + public final void whenSortingByStringNullFirst_thenFirstNull() { + + final Foo nullNameFoo = new Foo(); + nullNameFoo.setName(null); + + final Bar bar = new Bar(); + final List fooList1 = Lists.newArrayList(); + bar.setName("Bar_Me"); + nullNameFoo.setBar(bar); + fooList1.add(nullNameFoo); + bar.setFooList(fooList1); + entityManager.persist(bar); + entityManager.persist(nullNameFoo); + entityTransaction.commit(); + final String jql = "Select f from Foo as f order by f.name desc NULLS FIRST"; final Query sortQuery = entityManager.createQuery(jql); - boolean thrown = false; - try { - final List fooList = sortQuery.getResultList(); - } catch (final javax.persistence.PersistenceException e) { - thrown = true; + final List fooList = sortQuery.getResultList(); + assertNull(fooList.get(0).getName()); + for (final Foo foo : fooList) { + System.out.println("Name:" + foo.getName()); } - assertTrue(thrown); } @Test public final void whenSortingFooWithCriteria_thenPrintSortedFoos() { - final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit"); - final EntityManager entityManager = emf.createEntityManager(); - final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); + + criteriaBuilder = entityManager.getCriteriaBuilder(); final CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(Foo.class); final Root from = criteriaQuery.from(Foo.class); final CriteriaQuery select = criteriaQuery.select(from); @@ -168,9 +149,8 @@ public class FooServiceSortingTests { @Test public final void whenSortingFooWithCriteriaAndMultipleAttributes_thenPrintSortedFoos() { - final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit"); - final EntityManager entityManager = emf.createEntityManager(); - final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); + + criteriaBuilder = entityManager.getCriteriaBuilder(); final CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(Foo.class); final Root from = criteriaQuery.from(Foo.class); final CriteriaQuery select = criteriaQuery.select(from); From 08aa91739eef1e3f620ad479f75a8223d100a8eb Mon Sep 17 00:00:00 2001 From: egmp777 Date: Fri, 9 May 2014 18:05:11 -0500 Subject: [PATCH 05/12] Synched with Local --- .../org/baeldung/persistence/model/Bar.java | 53 +++++++++---------- .../org/baeldung/persistence/model/Foo.java | 1 - 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/spring-jpa/src/main/java/org/baeldung/persistence/model/Bar.java b/spring-jpa/src/main/java/org/baeldung/persistence/model/Bar.java index b3e789b9ce..c2da09957f 100644 --- a/spring-jpa/src/main/java/org/baeldung/persistence/model/Bar.java +++ b/spring-jpa/src/main/java/org/baeldung/persistence/model/Bar.java @@ -1,7 +1,6 @@ package org.baeldung.persistence.model; import java.io.Serializable; - import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; @@ -12,53 +11,53 @@ import javax.persistence.CascadeType; @Entity public class Bar implements Serializable { + private static final long serialVersionUID = 1L; + @Id @GeneratedValue(strategy = GenerationType.AUTO) - private long id; - - @Column(nullable = false) - private String name; + private int id; @OneToMany(mappedBy = "bar", fetch = FetchType.EAGER, cascade = CascadeType.ALL) @OrderBy("name ASC") List fooList; - public Bar() { + private String name; + + public Bar(){ super(); } - public Bar(final String name) { + 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; } + + //API public List getFooList() { return fooList; } - public void setFooList(final List fooList) { + public void setFooList(List fooList) { this.fooList = fooList; } + public int getId() { + return this.id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + // @Override diff --git a/spring-jpa/src/main/java/org/baeldung/persistence/model/Foo.java b/spring-jpa/src/main/java/org/baeldung/persistence/model/Foo.java index 8e1dee33e8..aed28cc07f 100644 --- a/spring-jpa/src/main/java/org/baeldung/persistence/model/Foo.java +++ b/spring-jpa/src/main/java/org/baeldung/persistence/model/Foo.java @@ -1,7 +1,6 @@ package org.baeldung.persistence.model; import java.io.Serializable; - import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; From a5c1d774ec7246e59789286604342035f9bf417f Mon Sep 17 00:00:00 2001 From: eugenp Date: Sat, 10 May 2014 15:33:07 +0300 Subject: [PATCH 06/12] initial compilation fixes --- .../service/FooSortingServiceTest.java | 32 ++++++-------- .../org/baeldung/persistence/model/Bar.java | 42 ++++++++++--------- .../service/FooServiceSortingTests.java | 27 ++++-------- 3 files changed, 43 insertions(+), 58 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 9d6f3c355a..1532c3ef3f 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 @@ -1,26 +1,13 @@ package org.baeldung.persistence.service; -import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; +import static org.junit.Assert.assertNull; -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.BeforeClass; -import org.junit.Test; -import org.junit.After; import java.util.List; import java.util.Set; + +import org.baeldung.persistence.model.Bar; +import org.baeldung.persistence.model.Foo; +import org.baeldung.spring.PersistenceConfig; import org.hibernate.Criteria; import org.hibernate.NullPrecedence; import org.hibernate.Query; @@ -29,8 +16,13 @@ import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.criterion.Order; -import com.cc.example.hibernate.Foo; -import com.cc.example.hibernate.Bar; +import org.junit.After; +import org.junit.BeforeClass; +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; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) diff --git a/spring-jpa/src/main/java/org/baeldung/persistence/model/Bar.java b/spring-jpa/src/main/java/org/baeldung/persistence/model/Bar.java index b3e789b9ce..3f76680d3b 100644 --- a/spring-jpa/src/main/java/org/baeldung/persistence/model/Bar.java +++ b/spring-jpa/src/main/java/org/baeldung/persistence/model/Bar.java @@ -1,66 +1,70 @@ package org.baeldung.persistence.model; import java.io.Serializable; +import java.util.List; +import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; +import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; -import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +import javax.persistence.OrderBy; @Entity public class Bar implements Serializable { - + @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; - + @Column(nullable = false) private String name; - + @OneToMany(mappedBy = "bar", fetch = FetchType.EAGER, cascade = CascadeType.ALL) @OrderBy("name ASC") List fooList; - + 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 getFooList() { return fooList; } - + public void setFooList(final List fooList) { this.fooList = fooList; } - + // - + @Override public int hashCode() { final int prime = 31; @@ -68,7 +72,7 @@ public class Bar implements Serializable { result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } - + @Override public boolean equals(final Object obj) { if (this == obj) @@ -85,12 +89,12 @@ public class Bar implements Serializable { return false; return true; } - + @Override public String toString() { final StringBuilder builder = new StringBuilder(); builder.append("Bar [name=").append(name).append("]"); return builder.toString(); } - + } 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 e1ecbd2015..37ed1dc2f0 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 @@ -1,7 +1,9 @@ package org.baeldung.persistence.service; -import static org.junit.Assert.*; +import static org.junit.Assert.assertNull; + import java.util.List; + import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; @@ -11,11 +13,13 @@ import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Root; -import com.google.common.collect.Lists; + +import org.baeldung.persistence.model.Bar; +import org.baeldung.persistence.model.Foo; import org.junit.BeforeClass; import org.junit.Test; -import com.cc.jpa.example.Foo; -import com.cc.jpa.example.Bar; + +import com.google.common.collect.Lists; public class FooServiceSortingTests { private static EntityManager entityManager; @@ -25,7 +29,6 @@ public class FooServiceSortingTests { @BeforeClass public static void before() { - emf = Persistence.createEntityManagerFactory("punit"); entityManager = emf.createEntityManager(); entityTransaction = entityManager.getTransaction(); @@ -35,43 +38,36 @@ public class FooServiceSortingTests { @Test public final void whenSortingByOneAttributeDefaultOrder_thenPrintSortedResult() { - final String jql = "Select f from Foo as f order by f.id"; final Query sortQuery = entityManager.createQuery(jql); final List fooList = sortQuery.getResultList(); for (final Foo foo : fooList) { System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId()); } - } @Test public final void whenSortingByOneAttributeSetOrder_thenSortedPrintResult() { - final String jql = "Select f from Foo as f order by f.id desc"; final Query sortQuery = entityManager.createQuery(jql); final List fooList = sortQuery.getResultList(); for (final Foo foo : fooList) { System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId()); } - } @Test public final void whenSortingByTwoAttributes_thenPrintSortedResult() { - final String jql = "Select f from Foo as f order by f.name asc, f.id desc"; final Query sortQuery = entityManager.createQuery(jql); final List fooList = sortQuery.getResultList(); for (final Foo foo : fooList) { System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId()); } - } @Test public final void whenSortingFooByBar_thenBarsSorted() { - final String jql = "Select f from Foo as f order by f.name, f.bar.id"; final Query barJoinQuery = entityManager.createQuery(jql); final List fooList = barJoinQuery.getResultList(); @@ -82,7 +78,6 @@ public class FooServiceSortingTests { @Test public final void whenSortinfBar_thenPrintBarsSortedWithFoos() { - final String jql = "Select b from Bar as b order by b.id"; final Query barQuery = entityManager.createQuery(jql); final List barList = barQuery.getResultList(); @@ -92,12 +87,10 @@ public class FooServiceSortingTests { System.out.println("FooName:" + foo.getName()); } } - } @Test public final void whenSortingByStringNullLast_thenLastNull() { - final String jql = "Select f from Foo as f order by f.name desc NULLS LAST"; final Query sortQuery = entityManager.createQuery(jql); final List fooList = sortQuery.getResultList(); @@ -109,7 +102,6 @@ public class FooServiceSortingTests { @Test public final void whenSortingByStringNullFirst_thenFirstNull() { - final Foo nullNameFoo = new Foo(); nullNameFoo.setName(null); @@ -133,7 +125,6 @@ public class FooServiceSortingTests { @Test public final void whenSortingFooWithCriteria_thenPrintSortedFoos() { - criteriaBuilder = entityManager.getCriteriaBuilder(); final CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(Foo.class); final Root from = criteriaQuery.from(Foo.class); @@ -144,12 +135,10 @@ public class FooServiceSortingTests { for (final Foo foo : fooList) { System.out.println("Name:" + foo.getName() + "--------Id:" + foo.getId()); } - } @Test public final void whenSortingFooWithCriteriaAndMultipleAttributes_thenPrintSortedFoos() { - criteriaBuilder = entityManager.getCriteriaBuilder(); final CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(Foo.class); final Root from = criteriaQuery.from(Foo.class); From 942b186e60ac6a7b4cef12b1aad1b8a882ace36c Mon Sep 17 00:00:00 2001 From: egmp777 Date: Sat, 10 May 2014 15:13:30 -0500 Subject: [PATCH 07/12] Hibernate/JPA Sorting Repo Compiled in Eclipse --- spring-hibernate4/.project | 6 +++ .../org.hibernate.eclipse.console.prefs | 3 ++ .../baeldung/persistence/model/Bar.hbm.xml | 21 ++++++++ .../org/baeldung/persistence/model/Bar.java | 8 +-- .../baeldung/persistence/model/Foo.hbm.xml | 21 ++++++++ .../org/baeldung/persistence/model/Foo.java | 45 ++++++++++------ .../src/test/java/hibernate.cfg.xml | 37 +++++++++++++ .../service/FooSortingServiceTest.java | 48 ++++++----------- spring-jpa/.classpath | 6 +-- spring-jpa/.project | 6 +++ .../org.hibernate.eclipse.console.prefs | 3 ++ .../org/baeldung/persistence/model/Bar.java | 52 ++++++++++--------- .../org/baeldung/persistence/model/Foo.java | 34 ++++++++---- .../src/test/java/META-INF/persistence.xml | 16 ++++++ .../service/FooServiceSortingTests.java | 13 +++-- 15 files changed, 225 insertions(+), 94 deletions(-) create mode 100644 spring-hibernate4/.settings/org.hibernate.eclipse.console.prefs create mode 100644 spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.hbm.xml create mode 100644 spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.hbm.xml create mode 100644 spring-hibernate4/src/test/java/hibernate.cfg.xml create mode 100644 spring-jpa/.settings/org.hibernate.eclipse.console.prefs create mode 100644 spring-jpa/src/test/java/META-INF/persistence.xml diff --git a/spring-hibernate4/.project b/spring-hibernate4/.project index b687191646..10df76aa23 100644 --- a/spring-hibernate4/.project +++ b/spring-hibernate4/.project @@ -30,6 +30,11 @@ + + org.hibernate.eclipse.console.hibernateBuilder + + + org.springframework.ide.eclipse.core.springnature @@ -39,5 +44,6 @@ org.eclipse.m2e.core.maven2Nature org.eclipse.wst.common.project.facet.core.nature org.eclipse.wst.jsdt.core.jsNature + org.hibernate.eclipse.console.hibernateNature diff --git a/spring-hibernate4/.settings/org.hibernate.eclipse.console.prefs b/spring-hibernate4/.settings/org.hibernate.eclipse.console.prefs new file mode 100644 index 0000000000..7505c63770 --- /dev/null +++ b/spring-hibernate4/.settings/org.hibernate.eclipse.console.prefs @@ -0,0 +1,3 @@ +default.configuration= +eclipse.preferences.version=1 +hibernate3.enabled=true diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.hbm.xml b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.hbm.xml new file mode 100644 index 0000000000..bbfbb73329 --- /dev/null +++ b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.hbm.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.java b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.java index 0489dbc21a..cba3173f2f 100644 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.java +++ b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.java @@ -36,8 +36,8 @@ public class Bar implements Serializable { public Set getFooSet() { return fooSet; } - - public void setFooList(Set fooSet) { + + public void setFooSet(final Set fooSet) { this.fooSet = fooSet; } @@ -45,7 +45,7 @@ public class Bar implements Serializable { return this.id; } - public void setId(int id) { + public void setId(final int id) { this.id = id; } @@ -53,7 +53,7 @@ public class Bar implements Serializable { return this.name; } - public void setName(String name) { + public void setName(final String name) { this.name = name; } // diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.hbm.xml b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.hbm.xml new file mode 100644 index 0000000000..09922c8783 --- /dev/null +++ b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.hbm.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java index 8e1dee33e8..29e9b332ec 100644 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java +++ b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java @@ -2,46 +2,61 @@ package org.baeldung.persistence.model; import java.io.Serializable; -import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; + +import org.hibernate.annotations.Fetch; +import org.hibernate.annotations.FetchMode; @Entity public class Foo implements Serializable { - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private long id; - - @Column(nullable = false) - private String name; + private static final long serialVersionUID = 1L; public Foo() { super(); + + } + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long id; + private String name; + @ManyToOne(targetEntity = Bar.class) + @JoinColumn(name = "BAR_ID") + @Fetch(FetchMode.JOIN) + private Bar bar = new Bar(); + + public Bar getBar() { + return bar; } - public Foo(final String name) { - super(); - - this.name = name; + public void setBar(final Bar bar) { + this.bar = bar; } - // API + public int getBar_Id() { + return bar_Id; + } + + public void setBar_Id(final int bar_Id) { + this.bar_Id = bar_Id; + } + + private int bar_Id; 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; } diff --git a/spring-hibernate4/src/test/java/hibernate.cfg.xml b/spring-hibernate4/src/test/java/hibernate.cfg.xml new file mode 100644 index 0000000000..915295111e --- /dev/null +++ b/spring-hibernate4/src/test/java/hibernate.cfg.xml @@ -0,0 +1,37 @@ + + + + + + + + +com.mysql.jdbc.Driver +jdbc:mysql://localhost:3306/HIBERTEST2_TEST +root + + + +1 + + +org.hibernate.dialect.MySQLDialect + + +thread + + +org.hibernate.cache.internal.NoCacheProvider + + +true + + + + + + + + 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 9d6f3c355a..b808a5e36b 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 @@ -1,26 +1,13 @@ package org.baeldung.persistence.service; -import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; +import static org.junit.Assert.assertNull; -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.BeforeClass; -import org.junit.Test; -import org.junit.After; import java.util.List; import java.util.Set; + +import org.baeldung.persistence.model.Bar; +import org.baeldung.persistence.model.Foo; +import org.baeldung.spring.PersistenceConfig; import org.hibernate.Criteria; import org.hibernate.NullPrecedence; import org.hibernate.Query; @@ -29,8 +16,14 @@ import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.criterion.Order; -import com.cc.example.hibernate.Foo; -import com.cc.example.hibernate.Bar; +import org.junit.After; +import org.junit.BeforeClass; +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; + @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) @@ -81,7 +74,6 @@ public class FooSortingServiceTest { System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); } sess.getTransaction().commit(); - } @Test @@ -96,7 +88,6 @@ public class FooSortingServiceTest { } sess.getTransaction().commit(); - } @Test @@ -108,10 +99,9 @@ public class FooSortingServiceTest { for (final Foo foo : fooList) { System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId() - ); + ); } sess.getTransaction().commit(); - } @Test @@ -123,10 +113,9 @@ public class FooSortingServiceTest { for (final Foo foo : fooList) { System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId() - ); + ); } sess.getTransaction().commit(); - } @Test @@ -139,7 +128,6 @@ public class FooSortingServiceTest { System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); } sess.getTransaction().commit(); - } @Test @@ -152,7 +140,6 @@ public class FooSortingServiceTest { System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName()); } sess.getTransaction().commit(); - } @Test @@ -166,7 +153,6 @@ public class FooSortingServiceTest { System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName()); } sess.getTransaction().commit(); - } @Test @@ -178,10 +164,8 @@ public class FooSortingServiceTest { 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(); - } @Test @@ -206,7 +190,7 @@ public class FooSortingServiceTest { final Query query = sess.createQuery(hql); final List barList = query.list(); for (final Bar bar : barList) { - final Set fooSet = bar.getFooList(); + final Set fooSet = bar.getFooSet(); System.out.println("Bar Id:" + bar.getId()); for (final Foo foo : fooSet) { System.out.println("FooName:" + foo.getName()); diff --git a/spring-jpa/.classpath b/spring-jpa/.classpath index ca257cf1f9..d1d54e092a 100644 --- a/spring-jpa/.classpath +++ b/spring-jpa/.classpath @@ -22,11 +22,7 @@ - - - - - + diff --git a/spring-jpa/.project b/spring-jpa/.project index 235ae29ecf..5bb2baa2b9 100644 --- a/spring-jpa/.project +++ b/spring-jpa/.project @@ -30,6 +30,11 @@ + + org.hibernate.eclipse.console.hibernateBuilder + + + org.springframework.ide.eclipse.core.springnature @@ -39,5 +44,6 @@ org.eclipse.m2e.core.maven2Nature org.eclipse.wst.common.project.facet.core.nature org.eclipse.wst.jsdt.core.jsNature + org.hibernate.eclipse.console.hibernateNature diff --git a/spring-jpa/.settings/org.hibernate.eclipse.console.prefs b/spring-jpa/.settings/org.hibernate.eclipse.console.prefs new file mode 100644 index 0000000000..7505c63770 --- /dev/null +++ b/spring-jpa/.settings/org.hibernate.eclipse.console.prefs @@ -0,0 +1,3 @@ +default.configuration= +eclipse.preferences.version=1 +hibernate3.enabled=true diff --git a/spring-jpa/src/main/java/org/baeldung/persistence/model/Bar.java b/spring-jpa/src/main/java/org/baeldung/persistence/model/Bar.java index c2da09957f..03162b8447 100644 --- a/spring-jpa/src/main/java/org/baeldung/persistence/model/Bar.java +++ b/spring-jpa/src/main/java/org/baeldung/persistence/model/Bar.java @@ -1,65 +1,69 @@ package org.baeldung.persistence.model; import java.io.Serializable; -import javax.persistence.Column; +import java.util.List; + +import javax.persistence.CascadeType; import javax.persistence.Entity; +import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; -import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +import javax.persistence.OrderBy; @Entity public class Bar implements Serializable { - + private static final long serialVersionUID = 1L; - + @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; - + @OneToMany(mappedBy = "bar", fetch = FetchType.EAGER, cascade = CascadeType.ALL) @OrderBy("name ASC") List fooList; - + private String name; - + public Bar(){ super(); } - + public Bar(final String name){ super(); this.name = name; } //API - + public List getFooList() { return fooList; } - - public void setFooList(List fooList) { + + public void setFooList(final List fooList) { this.fooList = fooList; } - + public int getId() { - return this.id; + return id; } - - public void setId(int id) { + + public void setId(final int id) { this.id = id; } - + public String getName() { - return this.name; + return name; } - - public void setName(String name) { + + public void setName(final String name) { this.name = name; } - + // - + @Override public int hashCode() { final int prime = 31; @@ -67,7 +71,7 @@ public class Bar implements Serializable { result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } - + @Override public boolean equals(final Object obj) { if (this == obj) @@ -84,12 +88,12 @@ public class Bar implements Serializable { return false; return true; } - + @Override public String toString() { final StringBuilder builder = new StringBuilder(); builder.append("Bar [name=").append(name).append("]"); return builder.toString(); } - + } diff --git a/spring-jpa/src/main/java/org/baeldung/persistence/model/Foo.java b/spring-jpa/src/main/java/org/baeldung/persistence/model/Foo.java index aed28cc07f..585cefb159 100644 --- a/spring-jpa/src/main/java/org/baeldung/persistence/model/Foo.java +++ b/spring-jpa/src/main/java/org/baeldung/persistence/model/Foo.java @@ -1,21 +1,20 @@ package org.baeldung.persistence.model; import java.io.Serializable; + import javax.persistence.Column; import javax.persistence.Entity; +import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; @Entity public class Foo implements Serializable { - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private long id; - - @Column(nullable = false) - private String name; + private static final long serialVersionUID = 1L; public Foo() { super(); @@ -27,13 +26,30 @@ public class Foo implements Serializable { this.name = name; } - // API + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "ID") + private long id; + @Column(name = "NAME") + private String name; + + @ManyToOne(targetEntity = Bar.class, fetch = FetchType.EAGER) + @JoinColumn(name = "BAR_ID") + private Bar bar; + + public Bar getBar() { + return bar; + } + + public void setBar(final Bar bar) { + this.bar = bar; + } public long getId() { return id; } - public void setId(final long id) { + public void setId(final int id) { this.id = id; } @@ -45,8 +61,6 @@ public class Foo implements Serializable { this.name = name; } - // - @Override public int hashCode() { final int prime = 31; diff --git a/spring-jpa/src/test/java/META-INF/persistence.xml b/spring-jpa/src/test/java/META-INF/persistence.xml new file mode 100644 index 0000000000..e528491795 --- /dev/null +++ b/spring-jpa/src/test/java/META-INF/persistence.xml @@ -0,0 +1,16 @@ + + + + org.baeldung.persistence.model.Foo + org.baeldung.persistence.model.Bar + + + + + + + + + + + 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 e1ecbd2015..1a15faf2c1 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 @@ -1,7 +1,9 @@ package org.baeldung.persistence.service; -import static org.junit.Assert.*; +import static org.junit.Assert.assertNull; + import java.util.List; + import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; @@ -11,11 +13,13 @@ import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Root; -import com.google.common.collect.Lists; + +import org.baeldung.persistence.model.Bar; +import org.baeldung.persistence.model.Foo; import org.junit.BeforeClass; import org.junit.Test; -import com.cc.jpa.example.Foo; -import com.cc.jpa.example.Bar; + +import com.google.common.collect.Lists; public class FooServiceSortingTests { private static EntityManager entityManager; @@ -162,4 +166,5 @@ public class FooServiceSortingTests { } } + } From ade8cb51db7eb2aa8051b353384b2664e57a0c73 Mon Sep 17 00:00:00 2001 From: egmp777 Date: Sat, 10 May 2014 18:33:51 -0500 Subject: [PATCH 08/12] Corrections After the Merge Today --- .../org/baeldung/persistence/model/Bar.java | 34 ++- .../org/baeldung/persistence/model/Foo.java | 19 +- .../service/FooSortingServiceTest.java | 219 +++++++++--------- 3 files changed, 138 insertions(+), 134 deletions(-) diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.java b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.java index cba3173f2f..b6cedbd772 100644 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.java +++ b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.java @@ -2,10 +2,20 @@ package org.baeldung.persistence.model; import java.io.Serializable; import java.util.Set; -import com.google.common.collect.Sets; -import javax.persistence.*; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.NamedQuery; +import javax.persistence.OneToMany; + import org.hibernate.annotations.OrderBy; +import com.google.common.collect.Sets; + @Entity @NamedQuery(name = "Bar.findAll", query = "SELECT b FROM Bar b") public class Bar implements Serializable { @@ -16,7 +26,7 @@ public class Bar implements Serializable { public Bar(final String name) { super(); - + this.name = name; } @@ -31,18 +41,18 @@ public class Bar implements Serializable { @OrderBy(clause = "NAME DESC") Set fooSet = Sets.newHashSet(); - //API - + // API + public Set getFooSet() { return fooSet; } - + public void setFooSet(final Set fooSet) { this.fooSet = fooSet; } public int getId() { - return this.id; + return id; } public void setId(final int id) { @@ -50,14 +60,15 @@ public class Bar implements Serializable { } public String getName() { - return this.name; + return name; } public void setName(final String name) { this.name = name; } + // - + @Override public int hashCode() { final int prime = 31; @@ -65,7 +76,7 @@ public class Bar implements Serializable { result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } - + @Override public boolean equals(final Object obj) { if (this == obj) @@ -82,7 +93,7 @@ public class Bar implements Serializable { return false; return true; } - + @Override public String toString() { final StringBuilder builder = new StringBuilder(); @@ -90,5 +101,4 @@ public class Bar implements Serializable { return builder.toString(); } - } diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java index 29e9b332ec..1f4ec7a544 100644 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java +++ b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java @@ -21,6 +21,12 @@ public class Foo implements Serializable { super(); } + + public Foo(final String name) { + super(); + this.name = name; + } + @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; @@ -38,25 +44,18 @@ public class Foo implements Serializable { this.bar = bar; } - public int getBar_Id() { - return bar_Id; - } - - public void setBar_Id(final int bar_Id) { - this.bar_Id = bar_Id; - } - - private int bar_Id; - 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; } 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 a6dc8bd1b8..103d87b1f3 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 @@ -1,21 +1,16 @@ package org.baeldung.persistence.service; -import static org.junit.Assert.assertNull; - import java.util.List; import java.util.Set; import org.baeldung.persistence.model.Bar; import org.baeldung.persistence.model.Foo; import org.baeldung.spring.PersistenceConfig; -import org.hibernate.Criteria; -import org.hibernate.NullPrecedence; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; -import org.hibernate.criterion.Order; import org.junit.After; import org.junit.BeforeClass; import org.junit.Test; @@ -49,138 +44,138 @@ public class FooSortingServiceTest { } - @Test - public final void whenHQlSortingByOneAttribute_thenPrintSortedResults() { + /* @Test + public final void whenHQlSortingByOneAttribute_thenPrintSortedResults() { - final String hql = "FROM Foo f ORDER BY f.name"; - final Query query = sess.createQuery(hql); - final List fooList = query.list(); - for (final Foo foo : fooList) { - System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); - } - sess.getTransaction().commit(); + final String hql = "FROM Foo f ORDER BY f.name"; + final Query query = sess.createQuery(hql); + final List fooList = query.list(); + for (final Foo foo : fooList) { + System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); + } + sess.getTransaction().commit(); - } + } - @Test - public final void whenHQlSortingByStringNullLast_thenLastNull() { + @Test + public final void whenHQlSortingByStringNullLast_thenLastNull() { - final String hql = "FROM Foo f ORDER BY f.name NULLS LAST"; - final Query query = sess.createQuery(hql); - final List fooList = query.list(); - assertNull(fooList.get(fooList.toArray().length - 1).getName()); - for (final Foo foo : fooList) { - System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); - } - sess.getTransaction().commit(); - } + final String hql = "FROM Foo f ORDER BY f.name NULLS LAST"; + final Query query = sess.createQuery(hql); + final List fooList = query.list(); + assertNull(fooList.get(fooList.toArray().length - 1).getName()); + for (final Foo foo : fooList) { + System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); + } + sess.getTransaction().commit(); + } - @Test - public final void whenSortingByStringNullsFirst_thenReturnNullsFirst() { + @Test + public final void whenSortingByStringNullsFirst_thenReturnNullsFirst() { - final String hql = "FROM Foo f ORDER BY f.name NULLS FIRST"; - final Query query = sess.createQuery(hql); - final List fooList = query.list(); - assertNull(fooList.get(0).getName()); - for (final Foo foo : fooList) { - System.out.println("Name:" + foo.getName()); + final String hql = "FROM Foo f ORDER BY f.name NULLS FIRST"; + final Query query = sess.createQuery(hql); + final List fooList = query.list(); + assertNull(fooList.get(0).getName()); + for (final Foo foo : fooList) { + System.out.println("Name:" + foo.getName()); - } - sess.getTransaction().commit(); - } + } + sess.getTransaction().commit(); + } - @Test - public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSortedResults() { + @Test + public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSortedResults() { - final String hql = "FROM Foo f ORDER BY f.name ASC"; - final Query query = sess.createQuery(hql); - final List fooList = query.list(); - for (final Foo foo : fooList) { - System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId() + final String hql = "FROM Foo f ORDER BY f.name ASC"; + final Query query = sess.createQuery(hql); + final List fooList = query.list(); + for (final Foo foo : fooList) { + System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId() - ); - } - sess.getTransaction().commit(); - } + ); + } + sess.getTransaction().commit(); + } - @Test - public final void whenHQlSortingByMultipleAttributes_thenSortedResults() { + @Test + public final void whenHQlSortingByMultipleAttributes_thenSortedResults() { - final String hql = "FROM Foo f ORDER BY f.name, f.id"; - final Query query = sess.createQuery(hql); - final List fooList = query.list(); - for (final Foo foo : fooList) { - System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId() + final String hql = "FROM Foo f ORDER BY f.name, f.id"; + final Query query = sess.createQuery(hql); + final List fooList = query.list(); + for (final Foo foo : fooList) { + System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId() - ); - } - sess.getTransaction().commit(); - } + ); + } + sess.getTransaction().commit(); + } - @Test - public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrintSortedResults() { + @Test + public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrintSortedResults() { - final String hql = "FROM Foo f ORDER BY f.name DESC, f.id ASC"; - final Query query = sess.createQuery(hql); - final List fooList = query.list(); - for (final Foo foo : fooList) { - System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); - } - sess.getTransaction().commit(); - } + final String hql = "FROM Foo f ORDER BY f.name DESC, f.id ASC"; + final Query query = sess.createQuery(hql); + final List fooList = query.list(); + for (final Foo foo : fooList) { + System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); + } + sess.getTransaction().commit(); + } - @Test - public final void whenHQLCriteriaSortingByOneAttr_thenPrintSortedResults() { + @Test + public final void whenHQLCriteriaSortingByOneAttr_thenPrintSortedResults() { - final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); - criteria.addOrder(Order.asc("id")); - final List fooList = criteria.list(); - for (final Foo foo : fooList) { - System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName()); - } - sess.getTransaction().commit(); - } + final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); + criteria.addOrder(Order.asc("id")); + final List fooList = criteria.list(); + for (final Foo foo : fooList) { + System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName()); + } + sess.getTransaction().commit(); + } - @Test - public final void whenHQLCriteriaSortingByMultipAttr_thenSortedResults() { + @Test + public final void whenHQLCriteriaSortingByMultipAttr_thenSortedResults() { - final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); - criteria.addOrder(Order.asc("name")); - criteria.addOrder(Order.asc("id")); - final List fooList = criteria.list(); - for (final Foo foo : fooList) { - System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName()); - } - sess.getTransaction().commit(); - } + final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); + criteria.addOrder(Order.asc("name")); + criteria.addOrder(Order.asc("id")); + final List fooList = criteria.list(); + for (final Foo foo : fooList) { + System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName()); + } + sess.getTransaction().commit(); + } - @Test - public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() { + @Test + public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() { - final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); - criteria.addOrder(Order.asc("name").nulls(NullPrecedence.LAST)); - final List 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(); - } + final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); + criteria.addOrder(Order.asc("name").nulls(NullPrecedence.LAST)); + final List 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(); + } - @Test - public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() { + @Test + public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() { - final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); - criteria.addOrder(Order.desc("name").nulls(NullPrecedence.FIRST)); - final List fooList = criteria.list(); - assertNull(fooList.get(0).getName()); - for (final Foo foo : fooList) { - System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName()); + final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); + criteria.addOrder(Order.desc("name").nulls(NullPrecedence.FIRST)); + final List 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(); + } + sess.getTransaction().commit(); - } + }*/ @Test public final void whenSortingBars_thenBarsWithSortedFoos() { From a2c98ab37f314e609d1cb6159fd0a1ac24e5b78d Mon Sep 17 00:00:00 2001 From: eugenp Date: Sun, 11 May 2014 10:35:35 +0300 Subject: [PATCH 09/12] minor cleanup --- .../org/baeldung/persistence/model/Bar.java | 21 ++++---- .../baeldung/persistence/model/Foo.hbm.xml | 2 - .../org/baeldung/persistence/model/Foo.java | 22 ++++---- .../src/test/java/hibernate.cfg.xml | 8 +-- ...erviceBasicPersistenceIntegrationTest.java | 54 +++++++++++++++++++ .../service/FooSortingServiceTest.java | 37 ++++--------- 6 files changed, 90 insertions(+), 54 deletions(-) create mode 100644 spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServiceBasicPersistenceIntegrationTest.java diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.java b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.java index b6cedbd772..beca727200 100644 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.java +++ b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.java @@ -20,6 +20,16 @@ import com.google.common.collect.Sets; @NamedQuery(name = "Bar.findAll", query = "SELECT b FROM Bar b") public class Bar implements Serializable { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private int id; + + private String name; + + @OneToMany(mappedBy = "bar", cascade = CascadeType.ALL, fetch = FetchType.EAGER) + @OrderBy(clause = "NAME DESC") + private Set fooSet = Sets.newHashSet(); + public Bar() { super(); } @@ -30,17 +40,6 @@ public class Bar implements Serializable { this.name = name; } - private static final long serialVersionUID = 1L; - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private int id; - - private String name; - - @OneToMany(mappedBy = "bar", cascade = CascadeType.ALL, fetch = FetchType.EAGER) - @OrderBy(clause = "NAME DESC") - Set fooSet = Sets.newHashSet(); - // API public Set getFooSet() { diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.hbm.xml b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.hbm.xml index 09922c8783..db7ec3010b 100644 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.hbm.xml +++ b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.hbm.xml @@ -15,7 +15,5 @@ - - diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java index 1f4ec7a544..f6280c0363 100644 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java +++ b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java @@ -2,6 +2,7 @@ package org.baeldung.persistence.model; import java.io.Serializable; +import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; @@ -15,7 +16,17 @@ import org.hibernate.annotations.FetchMode; @Entity public class Foo implements Serializable { - private static final long serialVersionUID = 1L; + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long id; + + @Column(nullable = false) + private String name; + + @ManyToOne(targetEntity = Bar.class) + @JoinColumn(name = "BAR_ID") + @Fetch(FetchMode.JOIN) + private Bar bar = new Bar(); public Foo() { super(); @@ -27,14 +38,7 @@ public class Foo implements Serializable { this.name = name; } - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private long id; - private String name; - @ManyToOne(targetEntity = Bar.class) - @JoinColumn(name = "BAR_ID") - @Fetch(FetchMode.JOIN) - private Bar bar = new Bar(); + // public Bar getBar() { return bar; diff --git a/spring-hibernate4/src/test/java/hibernate.cfg.xml b/spring-hibernate4/src/test/java/hibernate.cfg.xml index 915295111e..3c4e53efb8 100644 --- a/spring-hibernate4/src/test/java/hibernate.cfg.xml +++ b/spring-hibernate4/src/test/java/hibernate.cfg.xml @@ -9,9 +9,9 @@ com.mysql.jdbc.Driver -jdbc:mysql://localhost:3306/HIBERTEST2_TEST -root - +jdbc:mysql://localhost:3306/spring_hibernate4_01?createDatabaseIfNotExist=true +tutorialuser +tutorialmy5ql 1 @@ -33,5 +33,5 @@ - + diff --git a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServiceBasicPersistenceIntegrationTest.java b/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServiceBasicPersistenceIntegrationTest.java new file mode 100644 index 0000000000..457f5cc519 --- /dev/null +++ b/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServiceBasicPersistenceIntegrationTest.java @@ -0,0 +1,54 @@ +package org.baeldung.persistence.service; + +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; + +import org.baeldung.persistence.model.Foo; +import org.baeldung.spring.PersistenceConfig; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +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) +public class FooServiceBasicPersistenceIntegrationTest { + + @Autowired + private SessionFactory sessionFactory; + + @Autowired + private IFooService fooService; + + private Session session; + + // tests + + @Before + public final void before() { + session = sessionFactory.openSession(); + } + + @After + public final void after() { + session.close(); + } + + // tests + + @Test + public final void whenContextIsBootstrapped_thenNoExceptions() { + // + } + + @Test + public final void whenEntityIsCreated_thenNoExceptions() { + fooService.create(new Foo(randomAlphabetic(6))); + } + +} 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 103d87b1f3..b75e2b2f3b 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 @@ -12,7 +12,7 @@ import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.junit.After; -import org.junit.BeforeClass; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; @@ -21,32 +21,26 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) - - - public class FooSortingServiceTest { - private static Configuration configuration; - private static StandardServiceRegistryBuilder builder; - private static SessionFactory sf; - private static Session sess; + private SessionFactory sf; + private Session sess; - @BeforeClass - public static void before() { - - configuration = new Configuration().configure(); - builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()); + @Before + public void before() { + final Configuration configuration = new Configuration().configure(); + final StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()); sf = configuration.buildSessionFactory(builder.build()); sess = sf.openSession(); sess.beginTransaction(); } + @After public void after() { - + // } /* @Test public final void whenHQlSortingByOneAttribute_thenPrintSortedResults() { - final String hql = "FROM Foo f ORDER BY f.name"; final Query query = sess.createQuery(hql); final List fooList = query.list(); @@ -54,12 +48,10 @@ public class FooSortingServiceTest { System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); } sess.getTransaction().commit(); - } @Test public final void whenHQlSortingByStringNullLast_thenLastNull() { - final String hql = "FROM Foo f ORDER BY f.name NULLS LAST"; final Query query = sess.createQuery(hql); final List fooList = query.list(); @@ -72,7 +64,6 @@ public class FooSortingServiceTest { @Test public final void whenSortingByStringNullsFirst_thenReturnNullsFirst() { - final String hql = "FROM Foo f ORDER BY f.name NULLS FIRST"; final Query query = sess.createQuery(hql); final List fooList = query.list(); @@ -86,7 +77,6 @@ public class FooSortingServiceTest { @Test public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSortedResults() { - final String hql = "FROM Foo f ORDER BY f.name ASC"; final Query query = sess.createQuery(hql); final List fooList = query.list(); @@ -100,7 +90,6 @@ public class FooSortingServiceTest { @Test public final void whenHQlSortingByMultipleAttributes_thenSortedResults() { - final String hql = "FROM Foo f ORDER BY f.name, f.id"; final Query query = sess.createQuery(hql); final List fooList = query.list(); @@ -114,7 +103,6 @@ public class FooSortingServiceTest { @Test public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrintSortedResults() { - final String hql = "FROM Foo f ORDER BY f.name DESC, f.id ASC"; final Query query = sess.createQuery(hql); final List fooList = query.list(); @@ -126,7 +114,6 @@ public class FooSortingServiceTest { @Test public final void whenHQLCriteriaSortingByOneAttr_thenPrintSortedResults() { - final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); criteria.addOrder(Order.asc("id")); final List fooList = criteria.list(); @@ -138,7 +125,6 @@ public class FooSortingServiceTest { @Test public final void whenHQLCriteriaSortingByMultipAttr_thenSortedResults() { - final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); criteria.addOrder(Order.asc("name")); criteria.addOrder(Order.asc("id")); @@ -151,7 +137,6 @@ public class FooSortingServiceTest { @Test public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() { - final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); criteria.addOrder(Order.asc("name").nulls(NullPrecedence.LAST)); final List fooList = criteria.list(); @@ -164,7 +149,6 @@ public class FooSortingServiceTest { @Test public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() { - final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); criteria.addOrder(Order.desc("name").nulls(NullPrecedence.FIRST)); final List fooList = criteria.list(); @@ -174,12 +158,10 @@ public class FooSortingServiceTest { } sess.getTransaction().commit(); - }*/ @Test public final void whenSortingBars_thenBarsWithSortedFoos() { - final String hql = "FROM Bar b ORDER BY b.id"; final Query query = sess.createQuery(hql); final List barList = query.list(); @@ -192,7 +174,6 @@ public class FooSortingServiceTest { } } sess.getTransaction().commit(); - } } From 890d4348cd99f4c6f26095f883e0688ab7083b92 Mon Sep 17 00:00:00 2001 From: eugenp Date: Sun, 11 May 2014 10:39:34 +0300 Subject: [PATCH 10/12] minor testing work --- .../org/baeldung/persistence/model/Foo.java | 1 - .../persistence/IntegrationTestSuite.java | 21 +++++++++++++++++++ ...erviceBasicPersistenceIntegrationTest.java | 1 + ...ePaginationPersistenceIntegrationTest.java | 1 + .../FooServicePersistenceIntegrationTest.java | 1 + .../FooSortingServiceTest.java | 2 +- 6 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 spring-hibernate4/src/test/java/org/baeldung/persistence/IntegrationTestSuite.java rename spring-hibernate4/src/test/java/org/baeldung/persistence/{service => test}/FooSortingServiceTest.java (99%) diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java index f6280c0363..0c2007a945 100644 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java +++ b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java @@ -30,7 +30,6 @@ public class Foo implements Serializable { public Foo() { super(); - } public Foo(final String name) { diff --git a/spring-hibernate4/src/test/java/org/baeldung/persistence/IntegrationTestSuite.java b/spring-hibernate4/src/test/java/org/baeldung/persistence/IntegrationTestSuite.java new file mode 100644 index 0000000000..61f4dc456a --- /dev/null +++ b/spring-hibernate4/src/test/java/org/baeldung/persistence/IntegrationTestSuite.java @@ -0,0 +1,21 @@ +package org.baeldung.persistence; + +import org.baeldung.persistence.service.FooServiceBasicPersistenceIntegrationTest; +import org.baeldung.persistence.service.FooServicePaginationPersistenceIntegrationTest; +import org.baeldung.persistence.service.FooServicePersistenceIntegrationTest; +import org.baeldung.persistence.service.ParentServicePersistenceIntegrationTest; +import org.baeldung.persistence.test.FooSortingServiceTest; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +@RunWith(Suite.class) +@Suite.SuiteClasses({// @formatter:off + FooServiceBasicPersistenceIntegrationTest.class + ,FooServicePaginationPersistenceIntegrationTest.class + ,FooServicePersistenceIntegrationTest.class + ,ParentServicePersistenceIntegrationTest.class + ,FooSortingServiceTest.class +}) // @formatter:on +public class IntegrationTestSuite { + // +} diff --git a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServiceBasicPersistenceIntegrationTest.java b/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServiceBasicPersistenceIntegrationTest.java index 457f5cc519..2865810c67 100644 --- a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServiceBasicPersistenceIntegrationTest.java +++ b/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServiceBasicPersistenceIntegrationTest.java @@ -3,6 +3,7 @@ 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.hibernate.Session; import org.hibernate.SessionFactory; diff --git a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServicePaginationPersistenceIntegrationTest.java b/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServicePaginationPersistenceIntegrationTest.java index 1204b8ed13..24f1e241ce 100644 --- a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServicePaginationPersistenceIntegrationTest.java +++ b/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServicePaginationPersistenceIntegrationTest.java @@ -8,6 +8,7 @@ import static org.junit.Assert.assertThat; import java.util.List; import org.baeldung.persistence.model.Foo; +import org.baeldung.persistence.service.IFooService; import org.baeldung.spring.PersistenceConfig; import org.hibernate.Criteria; import org.hibernate.Query; diff --git a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java b/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java index 84e5d39d15..5b45d65e57 100644 --- a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java +++ b/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java @@ -3,6 +3,7 @@ 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; diff --git a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java b/spring-hibernate4/src/test/java/org/baeldung/persistence/test/FooSortingServiceTest.java similarity index 99% rename from spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java rename to spring-hibernate4/src/test/java/org/baeldung/persistence/test/FooSortingServiceTest.java index b75e2b2f3b..a7d3172762 100644 --- a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java +++ b/spring-hibernate4/src/test/java/org/baeldung/persistence/test/FooSortingServiceTest.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.service; +package org.baeldung.persistence.test; import java.util.List; import java.util.Set; From 1e9eefc8339f36102bed1906c29f6f132f5e3bc1 Mon Sep 17 00:00:00 2001 From: eugenp Date: Sun, 11 May 2014 10:45:01 +0300 Subject: [PATCH 11/12] minor testing work --- .../org/baeldung/persistence/IntegrationTestSuite.java | 8 ++++---- .../FooPaginationPersistenceIntegrationTest.java} | 4 ++-- .../FooSortingPersistenceServiceTest.java} | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) rename spring-hibernate4/src/test/java/org/baeldung/persistence/{service/FooServicePaginationPersistenceIntegrationTest.java => hibernate/FooPaginationPersistenceIntegrationTest.java} (98%) rename spring-hibernate4/src/test/java/org/baeldung/persistence/{test/FooSortingServiceTest.java => hibernate/FooSortingPersistenceServiceTest.java} (98%) diff --git a/spring-hibernate4/src/test/java/org/baeldung/persistence/IntegrationTestSuite.java b/spring-hibernate4/src/test/java/org/baeldung/persistence/IntegrationTestSuite.java index 61f4dc456a..938e8f359c 100644 --- a/spring-hibernate4/src/test/java/org/baeldung/persistence/IntegrationTestSuite.java +++ b/spring-hibernate4/src/test/java/org/baeldung/persistence/IntegrationTestSuite.java @@ -1,20 +1,20 @@ package org.baeldung.persistence; +import org.baeldung.persistence.hibernate.FooPaginationPersistenceIntegrationTest; +import org.baeldung.persistence.hibernate.FooSortingPersistenceServiceTest; import org.baeldung.persistence.service.FooServiceBasicPersistenceIntegrationTest; -import org.baeldung.persistence.service.FooServicePaginationPersistenceIntegrationTest; import org.baeldung.persistence.service.FooServicePersistenceIntegrationTest; import org.baeldung.persistence.service.ParentServicePersistenceIntegrationTest; -import org.baeldung.persistence.test.FooSortingServiceTest; import org.junit.runner.RunWith; import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses({// @formatter:off FooServiceBasicPersistenceIntegrationTest.class - ,FooServicePaginationPersistenceIntegrationTest.class + ,FooPaginationPersistenceIntegrationTest.class ,FooServicePersistenceIntegrationTest.class ,ParentServicePersistenceIntegrationTest.class - ,FooSortingServiceTest.class + ,FooSortingPersistenceServiceTest.class }) // @formatter:on public class IntegrationTestSuite { // diff --git a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServicePaginationPersistenceIntegrationTest.java b/spring-hibernate4/src/test/java/org/baeldung/persistence/hibernate/FooPaginationPersistenceIntegrationTest.java similarity index 98% rename from spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServicePaginationPersistenceIntegrationTest.java rename to spring-hibernate4/src/test/java/org/baeldung/persistence/hibernate/FooPaginationPersistenceIntegrationTest.java index 24f1e241ce..ec90c3779c 100644 --- a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServicePaginationPersistenceIntegrationTest.java +++ b/spring-hibernate4/src/test/java/org/baeldung/persistence/hibernate/FooPaginationPersistenceIntegrationTest.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.service; +package org.baeldung.persistence.hibernate; import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; import static org.hamcrest.Matchers.hasSize; @@ -31,7 +31,7 @@ import com.google.common.collect.Lists; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) -public class FooServicePaginationPersistenceIntegrationTest { +public class FooPaginationPersistenceIntegrationTest { @Autowired private SessionFactory sessionFactory; diff --git a/spring-hibernate4/src/test/java/org/baeldung/persistence/test/FooSortingServiceTest.java b/spring-hibernate4/src/test/java/org/baeldung/persistence/hibernate/FooSortingPersistenceServiceTest.java similarity index 98% rename from spring-hibernate4/src/test/java/org/baeldung/persistence/test/FooSortingServiceTest.java rename to spring-hibernate4/src/test/java/org/baeldung/persistence/hibernate/FooSortingPersistenceServiceTest.java index a7d3172762..3c01995dc0 100644 --- a/spring-hibernate4/src/test/java/org/baeldung/persistence/test/FooSortingServiceTest.java +++ b/spring-hibernate4/src/test/java/org/baeldung/persistence/hibernate/FooSortingPersistenceServiceTest.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.test; +package org.baeldung.persistence.hibernate; import java.util.List; import java.util.Set; @@ -21,7 +21,7 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) -public class FooSortingServiceTest { +public class FooSortingPersistenceServiceTest { private SessionFactory sf; private Session sess; From a9738345e8e1ea5c5539645f4ba4e36fb5eb1a1c Mon Sep 17 00:00:00 2001 From: eugenp Date: Sun, 11 May 2014 16:17:22 +0300 Subject: [PATCH 12/12] small cleanup work --- .../src/test/java/hibernate.cfg.xml | 48 ++-- .../FooSortingPersistenceServiceTest.java | 216 +++++++++--------- 2 files changed, 127 insertions(+), 137 deletions(-) diff --git a/spring-hibernate4/src/test/java/hibernate.cfg.xml b/spring-hibernate4/src/test/java/hibernate.cfg.xml index 3c4e53efb8..b073f6e3e6 100644 --- a/spring-hibernate4/src/test/java/hibernate.cfg.xml +++ b/spring-hibernate4/src/test/java/hibernate.cfg.xml @@ -7,31 +7,31 @@ - -com.mysql.jdbc.Driver -jdbc:mysql://localhost:3306/spring_hibernate4_01?createDatabaseIfNotExist=true -tutorialuser -tutorialmy5ql - - -1 - - -org.hibernate.dialect.MySQLDialect - - -thread - - -org.hibernate.cache.internal.NoCacheProvider - - -true + + com.mysql.jdbc.Driver + jdbc:mysql://localhost:3306/spring_hibernate4_01?createDatabaseIfNotExist=true + tutorialuser + tutorialmy5ql - + + 1 - - - + + org.hibernate.dialect.MySQLDialect + + + thread + + + org.hibernate.cache.internal.NoCacheProvider + + + true + + + + + + diff --git a/spring-hibernate4/src/test/java/org/baeldung/persistence/hibernate/FooSortingPersistenceServiceTest.java b/spring-hibernate4/src/test/java/org/baeldung/persistence/hibernate/FooSortingPersistenceServiceTest.java index 3c01995dc0..afaa74ea27 100644 --- a/spring-hibernate4/src/test/java/org/baeldung/persistence/hibernate/FooSortingPersistenceServiceTest.java +++ b/spring-hibernate4/src/test/java/org/baeldung/persistence/hibernate/FooSortingPersistenceServiceTest.java @@ -1,16 +1,21 @@ package org.baeldung.persistence.hibernate; +import static org.junit.Assert.assertNull; + import java.util.List; import java.util.Set; import org.baeldung.persistence.model.Bar; import org.baeldung.persistence.model.Foo; import org.baeldung.spring.PersistenceConfig; +import org.hibernate.Criteria; +import org.hibernate.NullPrecedence; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; +import org.hibernate.criterion.Order; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -21,12 +26,13 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) +@SuppressWarnings("unchecked") public class FooSortingPersistenceServiceTest { private SessionFactory sf; private Session sess; @Before - public void before() { + public final void before() { final Configuration configuration = new Configuration().configure(); final StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()); sf = configuration.buildSessionFactory(builder.build()); @@ -36,129 +42,115 @@ public class FooSortingPersistenceServiceTest { @After public void after() { - // + sess.getTransaction().commit(); } - /* @Test - public final void whenHQlSortingByOneAttribute_thenPrintSortedResults() { - final String hql = "FROM Foo f ORDER BY f.name"; - final Query query = sess.createQuery(hql); - final List fooList = query.list(); - for (final Foo foo : fooList) { - System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); - } - sess.getTransaction().commit(); - } + @Test + public final void whenHQlSortingByOneAttribute_thenPrintSortedResults() { + final String hql = "FROM Foo f ORDER BY f.name"; + final Query query = sess.createQuery(hql); + final List fooList = query.list(); + for (final Foo foo : fooList) { + System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); + } + } - @Test - public final void whenHQlSortingByStringNullLast_thenLastNull() { - final String hql = "FROM Foo f ORDER BY f.name NULLS LAST"; - final Query query = sess.createQuery(hql); - final List fooList = query.list(); - assertNull(fooList.get(fooList.toArray().length - 1).getName()); - for (final Foo foo : fooList) { - System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); - } - sess.getTransaction().commit(); - } + @Test + public final void whenHQlSortingByStringNullLast_thenLastNull() { + final String hql = "FROM Foo f ORDER BY f.name NULLS LAST"; + final Query query = sess.createQuery(hql); + final List fooList = query.list(); - @Test - public final void whenSortingByStringNullsFirst_thenReturnNullsFirst() { - final String hql = "FROM Foo f ORDER BY f.name NULLS FIRST"; - final Query query = sess.createQuery(hql); - final List fooList = query.list(); - assertNull(fooList.get(0).getName()); - for (final Foo foo : fooList) { - System.out.println("Name:" + foo.getName()); + assertNull(fooList.get(fooList.toArray().length - 1).getName()); + for (final Foo foo : fooList) { + System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); + } + } - } - sess.getTransaction().commit(); - } + @Test + public final void whenSortingByStringNullsFirst_thenReturnNullsFirst() { + final String hql = "FROM Foo f ORDER BY f.name NULLS FIRST"; + final Query query = sess.createQuery(hql); + final List fooList = query.list(); + assertNull(fooList.get(0).getName()); + for (final Foo foo : fooList) { + System.out.println("Name:" + foo.getName()); - @Test - public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSortedResults() { - final String hql = "FROM Foo f ORDER BY f.name ASC"; - final Query query = sess.createQuery(hql); - final List fooList = query.list(); - for (final Foo foo : fooList) { - System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId() + } + } - ); - } - sess.getTransaction().commit(); - } + @Test + public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSortedResults() { + final String hql = "FROM Foo f ORDER BY f.name ASC"; + final Query query = sess.createQuery(hql); + final List fooList = query.list(); + for (final Foo foo : fooList) { + System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); + } + } - @Test - public final void whenHQlSortingByMultipleAttributes_thenSortedResults() { - final String hql = "FROM Foo f ORDER BY f.name, f.id"; - final Query query = sess.createQuery(hql); - final List fooList = query.list(); - for (final Foo foo : fooList) { - System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId() + @Test + public final void whenHQlSortingByMultipleAttributes_thenSortedResults() { + final String hql = "FROM Foo f ORDER BY f.name, f.id"; + final Query query = sess.createQuery(hql); + final List fooList = query.list(); + for (final Foo foo : fooList) { + System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); + } + } - ); - } - sess.getTransaction().commit(); - } + @Test + public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrintSortedResults() { + final String hql = "FROM Foo f ORDER BY f.name DESC, f.id ASC"; + final Query query = sess.createQuery(hql); + final List fooList = query.list(); + for (final Foo foo : fooList) { + System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); + } + } - @Test - public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrintSortedResults() { - final String hql = "FROM Foo f ORDER BY f.name DESC, f.id ASC"; - final Query query = sess.createQuery(hql); - final List fooList = query.list(); - for (final Foo foo : fooList) { - System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); - } - sess.getTransaction().commit(); - } + @Test + public final void whenHQLCriteriaSortingByOneAttr_thenPrintSortedResults() { + final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); + criteria.addOrder(Order.asc("id")); + final List fooList = criteria.list(); + for (final Foo foo : fooList) { + System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName()); + } + } - @Test - public final void whenHQLCriteriaSortingByOneAttr_thenPrintSortedResults() { - final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); - criteria.addOrder(Order.asc("id")); - final List fooList = criteria.list(); - for (final Foo foo : fooList) { - System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName()); - } - sess.getTransaction().commit(); - } + @Test + public final void whenHQLCriteriaSortingByMultipAttr_thenSortedResults() { + final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); + criteria.addOrder(Order.asc("name")); + criteria.addOrder(Order.asc("id")); + final List fooList = criteria.list(); + for (final Foo foo : fooList) { + System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName()); + } + } - @Test - public final void whenHQLCriteriaSortingByMultipAttr_thenSortedResults() { - final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); - criteria.addOrder(Order.asc("name")); - criteria.addOrder(Order.asc("id")); - final List fooList = criteria.list(); - for (final Foo foo : fooList) { - System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName()); - } - sess.getTransaction().commit(); - } + @Test + public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() { + final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); + criteria.addOrder(Order.asc("name").nulls(NullPrecedence.LAST)); + final List 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()); + } + } - @Test - public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() { - final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); - criteria.addOrder(Order.asc("name").nulls(NullPrecedence.LAST)); - final List 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(); - } - - @Test - public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() { - final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); - criteria.addOrder(Order.desc("name").nulls(NullPrecedence.FIRST)); - final List 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(); - }*/ + @Test + public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() { + final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); + criteria.addOrder(Order.desc("name").nulls(NullPrecedence.FIRST)); + final List fooList = criteria.list(); + assertNull(fooList.get(0).getName()); + for (final Foo foo : fooList) { + System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName()); + } + } @Test public final void whenSortingBars_thenBarsWithSortedFoos() { @@ -170,10 +162,8 @@ public class FooSortingPersistenceServiceTest { System.out.println("Bar Id:" + bar.getId()); for (final Foo foo : fooSet) { System.out.println("FooName:" + foo.getName()); - } } - sess.getTransaction().commit(); } }