Merge pull request #7 from egmp777/master

Copied the Contents of Bar and FooServiceTest for Hibernate and JPA
This commit is contained in:
Eugen 2014-05-10 01:20:15 +03:00
commit 8f86fe971a
4 changed files with 261 additions and 441 deletions

View File

@ -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<Foo> 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<Foo> fooSet = Sets.newHashSet();
//API
public Set<Foo> getFooSet() {
return fooSet;
}
public void setId(final long id) {
public void setFooList(Set<Foo> 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<Foo> 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();
}
}

View File

@ -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<Foo> 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<Foo> 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<Foo> 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<Foo> 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<Foo> 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<Foo> 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<Foo> 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<Foo> 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<Foo> 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<Foo> 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<Foo> 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<Foo> 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<Foo> fooList = Lists.newArrayList();
try {
sess.beginTransaction();
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
criteria.addOrder(Order.asc("name"));
criteria.addOrder(Order.asc("id"));
fooList = criteria.list();
for (final Foo foo : fooList) {
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
}
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<Foo> 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<Foo> fooList = Lists.newArrayList();
try {
sess.beginTransaction();
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
criteria.addOrder(Order.asc("name").nulls(NullPrecedence.LAST));
fooList = criteria.list();
assertNull(fooList.get(fooList.toArray().length - 1).getName());
for (final Foo foo : fooList) {
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
}
sess.getTransaction().commit();
} catch (final Exception ex) {
ex.printStackTrace();
} finally {
if (sess != null) {
sess.close();
}
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
criteria.addOrder(Order.asc("name").nulls(NullPrecedence.LAST));
final List<Foo> 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<Foo> fooList = Lists.newArrayList();
try {
sess.beginTransaction();
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
criteria.addOrder(Order.desc("name").nulls(NullPrecedence.FIRST));
fooList = criteria.list();
assertNull(fooList.get(0).getName());
for (final Foo foo : fooList) {
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
}
sess.getTransaction().commit();
} catch (final Exception ex) {
ex.printStackTrace();
} finally {
if (sess != null) {
sess.close();
}
}
}
@Test
public final void whenHQlSortingByStringNullLast_thenLastNull() {
Session sess = null;
List<Foo> 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<Foo> 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<Foo> fooList = new TreeSet();
List<Bar> 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<Bar> barList = query.list();
for (final Bar bar : barList) {
final Set<Foo> 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<Foo> fooList = new ArrayList();
// final List<Bar> barList = new ArrayList();
//
// try {
// final SessionFactory sf = new Configuration().configure().buildSessionFactory();
// sess = sf.openSession();
// final String hql = "FROM Foo f ORDER BY f.idx";
// final Query query = sess.createQuery(hql);
// boolean thrown = false;
// try {
// fooList = criteria.list();
// } catch (final org.hibernate.PropertyAccessException e) {
// thrown = true;
// }
// assertTrue(thrown);
//
// final Transaction tr = sess.beginTransaction();
// tr.commit();
// } catch (final Exception ex) {
// ex.printStackTrace();
// } finally {
// if (sess != null) {
// sess.close();
// }
// }
// }
@Test
public final void whenSortingStringNullsLast_thenReturnNullsLast() {
Session sess = null;
List<Foo> fooList = Lists.newArrayList();
final List<Bar> barList = Lists.newArrayList();
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<Foo> 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();
}
}
}
}

View File

@ -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<Foo> foos;
@OneToMany(mappedBy = "bar", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@OrderBy("name ASC")
List<Foo> 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<Foo> getFooList() {
return foos;
return fooList;
}
public void setFooList(final List<Foo> 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();
}
}

View File

@ -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<Foo> 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<Foo> 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<Foo> 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<Foo> 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<Bar> 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<Foo> 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<Foo> 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<Foo> 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<Foo> fooList = sortQuery.getResultList();
} catch (final javax.persistence.PersistenceException e) {
thrown = true;
final List<Foo> 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<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class);
final Root<Foo> from = criteriaQuery.from(Foo.class);
final CriteriaQuery<Foo> 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<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class);
final Root<Foo> from = criteriaQuery.from(Foo.class);
final CriteriaQuery<Foo> select = criteriaQuery.select(from);