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 ddc60bf0c9..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
@@ -1,25 +1,34 @@
package org.baeldung.persistence.model;
import java.io.Serializable;
-import java.util.List;
+import java.util.Set;
-import javax.persistence.Column;
+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 {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
- private long id;
+ private int id;
- @Column(nullable = false)
private String name;
- private List foos;
+ @OneToMany(mappedBy = "bar", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
+ @OrderBy(clause = "NAME DESC")
+ private Set fooSet = Sets.newHashSet();
public Bar() {
super();
@@ -33,11 +42,19 @@ public class Bar implements Serializable {
// API
- public long getId() {
+ public Set getFooSet() {
+ return fooSet;
+ }
+
+ public void setFooSet(final Set fooSet) {
+ this.fooSet = fooSet;
+ }
+
+ public int getId() {
return id;
}
- public void setId(final long id) {
+ public void setId(final int id) {
this.id = id;
}
@@ -49,10 +66,6 @@ public class Bar implements Serializable {
this.name = name;
}
- public List getFooList() {
- return foos;
- }
-
//
@Override
@@ -83,7 +96,7 @@ public class Bar implements Serializable {
@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();
}
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..db7ec3010b
--- /dev/null
+++ b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.hbm.xml
@@ -0,0 +1,19 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
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..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
@@ -7,6 +7,11 @@ 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 {
@@ -18,17 +23,29 @@ public class Foo implements Serializable {
@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();
}
public Foo(final String name) {
super();
-
this.name = name;
}
- // API
+ //
+
+ public Bar getBar() {
+ return bar;
+ }
+
+ public void setBar(final Bar bar) {
+ this.bar = bar;
+ }
public long getId() {
return id;
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..b073f6e3e6
--- /dev/null
+++ b/spring-hibernate4/src/test/java/hibernate.cfg.xml
@@ -0,0 +1,37 @@
+
+
+
+
+
+
+
+
+ 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/IntegrationTestSuite.java b/spring-hibernate4/src/test/java/org/baeldung/persistence/IntegrationTestSuite.java
new file mode 100644
index 0000000000..938e8f359c
--- /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.hibernate.FooPaginationPersistenceIntegrationTest;
+import org.baeldung.persistence.hibernate.FooSortingPersistenceServiceTest;
+import org.baeldung.persistence.service.FooServiceBasicPersistenceIntegrationTest;
+import org.baeldung.persistence.service.FooServicePersistenceIntegrationTest;
+import org.baeldung.persistence.service.ParentServicePersistenceIntegrationTest;
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+
+@RunWith(Suite.class)
+@Suite.SuiteClasses({// @formatter:off
+ FooServiceBasicPersistenceIntegrationTest.class
+ ,FooPaginationPersistenceIntegrationTest.class
+ ,FooServicePersistenceIntegrationTest.class
+ ,ParentServicePersistenceIntegrationTest.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 97%
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 1204b8ed13..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;
@@ -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;
@@ -30,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/hibernate/FooSortingPersistenceServiceTest.java b/spring-hibernate4/src/test/java/org/baeldung/persistence/hibernate/FooSortingPersistenceServiceTest.java
new file mode 100644
index 0000000000..afaa74ea27
--- /dev/null
+++ b/spring-hibernate4/src/test/java/org/baeldung/persistence/hibernate/FooSortingPersistenceServiceTest.java
@@ -0,0 +1,169 @@
+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;
+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)
+@SuppressWarnings("unchecked")
+public class FooSortingPersistenceServiceTest {
+ private SessionFactory sf;
+ private Session sess;
+
+ @Before
+ public final 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() {
+ 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());
+ }
+ }
+
+ @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());
+ }
+ }
+
+ @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_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 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 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 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 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() {
+ 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.getFooSet();
+ System.out.println("Bar Id:" + bar.getId());
+ for (final Foo foo : fooSet) {
+ System.out.println("FooName:" + foo.getName());
+ }
+ }
+ }
+
+}
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..2865810c67
--- /dev/null
+++ b/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServiceBasicPersistenceIntegrationTest.java
@@ -0,0 +1,55 @@
+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;
+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/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/service/FooSortingServiceTest.java
deleted file mode 100644
index 9fc80b8621..0000000000
--- a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java
+++ /dev/null
@@ -1,390 +0,0 @@
-package org.baeldung.persistence.service;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-
-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.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;
-
-@RunWith(SpringJUnit4ClassRunner.class)
-@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
-public class FooSortingServiceTest {
-
- // tests
-
- @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();
- }
- }
-
- }
-
- @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();
- }
- }
-
- }
-
- @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();
- }
- }
-
- }
-
- @Test
- public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrintSortedOrderedResults() {
- 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 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();
- }
- }
-
- }
-
- @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());
- }
-
- sess.getTransaction().commit();
- } catch (final Exception ex) {
- ex.printStackTrace();
- } finally {
- if (sess != null) {
- sess.close();
- }
- }
- }
-
- @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());
- }
-
- sess.getTransaction().commit();
- } catch (final Exception ex) {
- ex.printStackTrace();
- } finally {
- if (sess != null) {
- sess.close();
- }
- }
- }
-
- @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();
- }
- }
- }
-
- @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();
- }
- }
-
- }
-
- @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();
-
- 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();
- }
- }
-
- }
-
- // @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();
- }
- }
- }
-}
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 ddc60bf0c9..154d529881 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
@@ -3,14 +3,20 @@ 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.OneToMany;
+import javax.persistence.OrderBy;
@Entity
public class Bar implements Serializable {
+ private static final long serialVersionUID = 1L;
+
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@@ -19,7 +25,11 @@ public class Bar implements Serializable {
@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();
@@ -38,6 +48,7 @@ public class Bar implements Serializable {
}
public void setId(final long id) {
+
this.id = id;
}
@@ -50,7 +61,11 @@ public class Bar implements Serializable {
}
public List getFooList() {
- return foos;
+ return fooList;
+ }
+
+ public void setFooList(final List fooList) {
+ this.fooList = fooList;
}
//
@@ -83,7 +98,7 @@ public class Bar implements Serializable {
@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();
}
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..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
@@ -4,19 +4,17 @@ 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();
@@ -28,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;
}
@@ -46,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 0cc8645af8..baba4037f1 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,8 +1,6 @@
package org.baeldung.persistence.service;
-import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
import java.util.List;
@@ -18,35 +16,31 @@ import javax.persistence.criteria.Root;
import org.baeldung.persistence.model.Bar;
import org.baeldung.persistence.model.Foo;
-import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;
+import com.google.common.collect.Lists;
+
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());
}
@@ -54,16 +48,9 @@ public class FooServiceSortingTests {
@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());
}
@@ -71,10 +58,6 @@ public class FooServiceSortingTests {
@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();
@@ -84,13 +67,18 @@ public class FooServiceSortingTests {
}
@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";
+ 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 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) {
@@ -103,11 +91,6 @@ public class FooServiceSortingTests {
@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 +100,32 @@ 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);
@@ -163,14 +135,11 @@ public class FooServiceSortingTests {
for (final Foo foo : fooList) {
System.out.println("Name:" + foo.getName() + "--------Id:" + foo.getId());
}
-
}
@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);
@@ -182,4 +151,5 @@ public class FooServiceSortingTests {
}
}
+
}