Merge pull request #7 from egmp777/master
Copied the Contents of Bar and FooServiceTest for Hibernate and JPA
This commit is contained in:
commit
8f86fe971a
@ -1,26 +1,15 @@
|
|||||||
package org.baeldung.persistence.model;
|
package org.baeldung.persistence.model;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.List;
|
import java.util.Set;
|
||||||
|
import com.google.common.collect.Sets;
|
||||||
import javax.persistence.Column;
|
import javax.persistence.*;
|
||||||
import javax.persistence.Entity;
|
import org.hibernate.annotations.OrderBy;
|
||||||
import javax.persistence.GeneratedValue;
|
|
||||||
import javax.persistence.GenerationType;
|
|
||||||
import javax.persistence.Id;
|
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
|
@NamedQuery(name = "Bar.findAll", query = "SELECT b FROM Bar b")
|
||||||
public class Bar implements Serializable {
|
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() {
|
public Bar() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
@ -31,28 +20,42 @@ public class Bar implements Serializable {
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
// API
|
private static final long serialVersionUID = 1L;
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private int id;
|
||||||
|
|
||||||
public long getId() {
|
private String name;
|
||||||
return id;
|
|
||||||
|
@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;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return this.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(final String name) {
|
public void setName(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Foo> getFooList() {
|
|
||||||
return foos;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -83,8 +86,9 @@ public class Bar implements Serializable {
|
|||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
final StringBuilder builder = new StringBuilder();
|
final StringBuilder builder = new StringBuilder();
|
||||||
builder.append("Foo [name=").append(name).append("]");
|
builder.append("Bar [name=").append(name).append("]");
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,390 +1,220 @@
|
|||||||
package org.baeldung.persistence.service;
|
package org.baeldung.persistence.service;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||||
import static org.junit.Assert.assertNull;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
|
import org.baeldung.persistence.model.Foo;
|
||||||
|
import org.baeldung.persistence.service.IFooService;
|
||||||
|
import org.baeldung.spring.PersistenceConfig;
|
||||||
|
import org.junit.Ignore;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.dao.DataAccessException;
|
||||||
|
import org.springframework.dao.DataIntegrityViolationException;
|
||||||
|
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.After;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
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.Criteria;
|
||||||
import org.hibernate.NullPrecedence;
|
import org.hibernate.NullPrecedence;
|
||||||
import org.hibernate.Query;
|
import org.hibernate.Query;
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
import org.hibernate.Transaction;
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
import org.hibernate.cfg.Configuration;
|
import org.hibernate.cfg.Configuration;
|
||||||
import org.hibernate.criterion.Order;
|
import org.hibernate.criterion.Order;
|
||||||
import org.junit.Test;
|
import com.cc.example.hibernate.Foo;
|
||||||
import org.junit.runner.RunWith;
|
import com.cc.example.hibernate.Bar;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|
||||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||||
public class FooSortingServiceTest {
|
|
||||||
|
|
||||||
// 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
|
@Test
|
||||||
public final void whenHQlSortingByOneAttribute_thenPrintSortedResults() {
|
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 String hql = "FROM Foo f ORDER BY f.name";
|
||||||
final Query query = sess.createQuery(hql);
|
final Query query = sess.createQuery(hql);
|
||||||
fooList = query.list();
|
final List<Foo> fooList = query.list();
|
||||||
for (final Foo foo : fooList) {
|
for (final Foo foo : fooList) {
|
||||||
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
|
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
|
||||||
}
|
}
|
||||||
final Transaction tr = sess.beginTransaction();
|
sess.getTransaction().commit();
|
||||||
tr.commit();
|
|
||||||
} catch (final Exception ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
} finally {
|
|
||||||
if (sess != null) {
|
|
||||||
sess.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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
|
@Test
|
||||||
public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSortedResults() {
|
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 String hql = "FROM Foo f ORDER BY f.name ASC";
|
||||||
final Query query = sess.createQuery(hql);
|
final Query query = sess.createQuery(hql);
|
||||||
fooList = query.list();
|
final List<Foo> fooList = query.list();
|
||||||
for (final Foo foo : fooList) {
|
for (final Foo foo : fooList) {
|
||||||
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
|
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
sess.getTransaction().commit();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenHQlSortingByMultipleAttributes_thenSortedResults() {
|
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 String hql = "FROM Foo f ORDER BY f.name, f.id";
|
||||||
final Query query = sess.createQuery(hql);
|
final Query query = sess.createQuery(hql);
|
||||||
fooList = query.list();
|
final List<Foo> fooList = query.list();
|
||||||
for (final Foo foo : fooList) {
|
for (final Foo foo : fooList) {
|
||||||
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
|
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
sess.getTransaction().commit();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrintSortedOrderedResults() {
|
public final void whenHQlSortingByMultipleAttributes_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 DESC, f.id ASC";
|
final String hql = "FROM Foo f ORDER BY f.name DESC, f.id ASC";
|
||||||
final Query query = sess.createQuery(hql);
|
final Query query = sess.createQuery(hql);
|
||||||
fooList = query.list();
|
final List<Foo> fooList = query.list();
|
||||||
for (final Foo foo : fooList) {
|
for (final Foo foo : fooList) {
|
||||||
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
|
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
|
||||||
}
|
}
|
||||||
final Transaction tr = sess.beginTransaction();
|
sess.getTransaction().commit();
|
||||||
tr.commit();
|
|
||||||
} catch (final Exception ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
} finally {
|
|
||||||
if (sess != null) {
|
|
||||||
sess.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenCriteriaSortingByOneAttr_thenPrintSortedResults() {
|
public final void whenHQLCriteriaSortingByOneAttr_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");
|
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
||||||
criteria.addOrder(Order.asc("id"));
|
criteria.addOrder(Order.asc("id"));
|
||||||
fooList = criteria.list();
|
final List<Foo> fooList = criteria.list();
|
||||||
assertEquals(1, fooList.get(0).getId());
|
|
||||||
assertEquals(100, fooList.get(fooList.toArray().length - 1).getId());
|
|
||||||
for (final Foo foo : fooList) {
|
for (final Foo foo : fooList) {
|
||||||
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
sess.getTransaction().commit();
|
sess.getTransaction().commit();
|
||||||
} catch (final Exception ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
} finally {
|
|
||||||
if (sess != null) {
|
|
||||||
sess.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenCriteriaSortingByMultipAttr_thenSortedResults() {
|
public final void whenHQLCriteriaSortingByMultipAttr_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");
|
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
||||||
criteria.addOrder(Order.asc("name"));
|
criteria.addOrder(Order.asc("name"));
|
||||||
criteria.addOrder(Order.asc("id"));
|
criteria.addOrder(Order.asc("id"));
|
||||||
fooList = criteria.list();
|
final List<Foo> fooList = criteria.list();
|
||||||
for (final Foo foo : fooList) {
|
for (final Foo foo : fooList) {
|
||||||
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
sess.getTransaction().commit();
|
sess.getTransaction().commit();
|
||||||
} catch (final Exception ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
} finally {
|
|
||||||
if (sess != null) {
|
|
||||||
sess.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() {
|
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");
|
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
||||||
criteria.addOrder(Order.asc("name").nulls(NullPrecedence.LAST));
|
criteria.addOrder(Order.asc("name").nulls(NullPrecedence.LAST));
|
||||||
fooList = criteria.list();
|
final List<Foo> fooList = criteria.list();
|
||||||
assertNull(fooList.get(fooList.toArray().length - 1).getName());
|
assertNull(fooList.get(fooList.toArray().length - 1).getName());
|
||||||
for (final Foo foo : fooList) {
|
for (final Foo foo : fooList) {
|
||||||
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
||||||
|
|
||||||
}
|
}
|
||||||
sess.getTransaction().commit();
|
sess.getTransaction().commit();
|
||||||
} catch (final Exception ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
} finally {
|
|
||||||
if (sess != null) {
|
|
||||||
sess.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() {
|
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");
|
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
||||||
criteria.addOrder(Order.desc("name").nulls(NullPrecedence.FIRST));
|
criteria.addOrder(Order.desc("name").nulls(NullPrecedence.FIRST));
|
||||||
fooList = criteria.list();
|
final List<Foo> fooList = criteria.list();
|
||||||
assertNull(fooList.get(0).getName());
|
assertNull(fooList.get(0).getName());
|
||||||
for (final Foo foo : fooList) {
|
for (final Foo foo : fooList) {
|
||||||
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
||||||
|
|
||||||
}
|
}
|
||||||
sess.getTransaction().commit();
|
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenSortingBars_thenBarsWithSortedFoos() {
|
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 String hql = "FROM Bar b ORDER BY b.id";
|
||||||
final Query query = sess.createQuery(hql);
|
final Query query = sess.createQuery(hql);
|
||||||
barList = query.list();
|
final List<Bar> barList = query.list();
|
||||||
|
|
||||||
for (final Bar bar : barList) {
|
for (final Bar bar : barList) {
|
||||||
|
final Set<Foo> fooSet = bar.getFooList();
|
||||||
System.out.println("Bar Id:" + bar.getId());
|
System.out.println("Bar Id:" + bar.getId());
|
||||||
for (final Foo foo : bar.getFooList()) {
|
for (final Foo foo : fooSet) {
|
||||||
System.out.println("FooName:" + foo.getName());
|
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<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();
|
sess.getTransaction().commit();
|
||||||
} catch (final Exception ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
} finally {
|
|
||||||
if (sess != null) {
|
|
||||||
sess.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
package org.baeldung.persistence.model;
|
package org.baeldung.persistence.model;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.GenerationType;
|
import javax.persistence.GenerationType;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.CascadeType;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
public class Bar implements Serializable {
|
public class Bar implements Serializable {
|
||||||
@ -19,7 +19,9 @@ public class Bar implements Serializable {
|
|||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
private List<Foo> foos;
|
@OneToMany(mappedBy = "bar", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
|
||||||
|
@OrderBy("name ASC")
|
||||||
|
List<Foo> fooList;
|
||||||
|
|
||||||
public Bar() {
|
public Bar() {
|
||||||
super();
|
super();
|
||||||
@ -50,7 +52,11 @@ public class Bar implements Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public List<Foo> getFooList() {
|
public List<Foo> getFooList() {
|
||||||
return foos;
|
return fooList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFooList(final List<Foo> fooList) {
|
||||||
|
this.fooList = fooList;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@ -83,7 +89,7 @@ public class Bar implements Serializable {
|
|||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
final StringBuilder builder = new StringBuilder();
|
final StringBuilder builder = new StringBuilder();
|
||||||
builder.append("Foo [name=").append(name).append("]");
|
builder.append("Bar [name=").append(name).append("]");
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,11 +1,7 @@
|
|||||||
package org.baeldung.persistence.service;
|
package org.baeldung.persistence.service;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.*;
|
||||||
import static org.junit.Assert.assertNull;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
import javax.persistence.EntityManagerFactory;
|
import javax.persistence.EntityManagerFactory;
|
||||||
import javax.persistence.EntityTransaction;
|
import javax.persistence.EntityTransaction;
|
||||||
@ -15,82 +11,79 @@ import javax.persistence.TypedQuery;
|
|||||||
import javax.persistence.criteria.CriteriaBuilder;
|
import javax.persistence.criteria.CriteriaBuilder;
|
||||||
import javax.persistence.criteria.CriteriaQuery;
|
import javax.persistence.criteria.CriteriaQuery;
|
||||||
import javax.persistence.criteria.Root;
|
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.After;
|
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import com.cc.jpa.example.Foo;
|
||||||
|
import com.cc.jpa.example.Bar;
|
||||||
|
|
||||||
public class FooServiceSortingTests {
|
public class FooServiceSortingTests {
|
||||||
private EntityManager entityManager;
|
private static EntityManager entityManager;
|
||||||
|
private static EntityManagerFactory emf;
|
||||||
|
private static EntityTransaction entityTransaction;
|
||||||
|
private static CriteriaBuilder criteriaBuilder;
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void before() {
|
public static void before() {
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
@After
|
emf = Persistence.createEntityManagerFactory("punit");
|
||||||
public final void after() {
|
entityManager = emf.createEntityManager();
|
||||||
//
|
entityTransaction = entityManager.getTransaction();
|
||||||
|
entityTransaction.begin();
|
||||||
|
criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenSortingByOneAttributeDefaultOrder_thenPrintSortedResult() {
|
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 String jql = "Select f from Foo as f order by f.id";
|
||||||
final Query sortQuery = entityManager.createQuery(jql);
|
final Query sortQuery = entityManager.createQuery(jql);
|
||||||
final List<Foo> fooList = sortQuery.getResultList();
|
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) {
|
for (final Foo foo : fooList) {
|
||||||
System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId());
|
System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenSortingByOneAttributeSetOrder_thenSortedPrintResult() {
|
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 String jql = "Select f from Foo as f order by f.id desc";
|
||||||
final Query sortQuery = entityManager.createQuery(jql);
|
final Query sortQuery = entityManager.createQuery(jql);
|
||||||
final List<Foo> fooList = sortQuery.getResultList();
|
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) {
|
for (final Foo foo : fooList) {
|
||||||
System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId());
|
System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenSortingByTwoAttributes_thenPrintSortedResult() {
|
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 String jql = "Select f from Foo as f order by f.name asc, f.id desc";
|
||||||
final Query sortQuery = entityManager.createQuery(jql);
|
final Query sortQuery = entityManager.createQuery(jql);
|
||||||
final List<Foo> fooList = sortQuery.getResultList();
|
final List<Foo> fooList = sortQuery.getResultList();
|
||||||
for (final Foo foo : fooList) {
|
for (final Foo foo : fooList) {
|
||||||
System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId());
|
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
|
@Test
|
||||||
public final void whenSortinfBar_thenPrintBarsSortedWithFoos() {
|
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 Query barQuery = entityManager.createQuery(jql);
|
||||||
final List<Bar> barList = barQuery.getResultList();
|
final List<Bar> barList = barQuery.getResultList();
|
||||||
for (final Bar bar : barList) {
|
for (final Bar bar : barList) {
|
||||||
@ -99,15 +92,12 @@ public class FooServiceSortingTests {
|
|||||||
System.out.println("FooName:" + foo.getName());
|
System.out.println("FooName:" + foo.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenSortingByStringNullLast_thenLastNull() {
|
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 String jql = "Select f from Foo as f order by f.name desc NULLS LAST";
|
||||||
final Query sortQuery = entityManager.createQuery(jql);
|
final Query sortQuery = entityManager.createQuery(jql);
|
||||||
final List<Foo> fooList = sortQuery.getResultList();
|
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
|
@Test
|
||||||
public final void whenSortingByIntNull_thenException() {
|
public final void whenSortingByStringNullFirst_thenFirstNull() {
|
||||||
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
|
||||||
final EntityManager entityManager = emf.createEntityManager();
|
final Foo nullNameFoo = new Foo();
|
||||||
final EntityTransaction entityTransaction = entityManager.getTransaction();
|
nullNameFoo.setName(null);
|
||||||
entityTransaction.begin();
|
|
||||||
final String jql = "Select f from Foo as f order by f.test_Null desc NULLS FIRST";
|
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);
|
final Query sortQuery = entityManager.createQuery(jql);
|
||||||
boolean thrown = false;
|
|
||||||
try {
|
|
||||||
final List<Foo> fooList = sortQuery.getResultList();
|
final List<Foo> fooList = sortQuery.getResultList();
|
||||||
} catch (final javax.persistence.PersistenceException e) {
|
assertNull(fooList.get(0).getName());
|
||||||
thrown = true;
|
for (final Foo foo : fooList) {
|
||||||
|
System.out.println("Name:" + foo.getName());
|
||||||
}
|
}
|
||||||
assertTrue(thrown);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenSortingFooWithCriteria_thenPrintSortedFoos() {
|
public final void whenSortingFooWithCriteria_thenPrintSortedFoos() {
|
||||||
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
|
||||||
final EntityManager entityManager = emf.createEntityManager();
|
criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||||
final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
|
||||||
final CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class);
|
final CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class);
|
||||||
final Root<Foo> from = criteriaQuery.from(Foo.class);
|
final Root<Foo> from = criteriaQuery.from(Foo.class);
|
||||||
final CriteriaQuery<Foo> select = criteriaQuery.select(from);
|
final CriteriaQuery<Foo> select = criteriaQuery.select(from);
|
||||||
@ -168,9 +149,8 @@ public class FooServiceSortingTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenSortingFooWithCriteriaAndMultipleAttributes_thenPrintSortedFoos() {
|
public final void whenSortingFooWithCriteriaAndMultipleAttributes_thenPrintSortedFoos() {
|
||||||
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
|
||||||
final EntityManager entityManager = emf.createEntityManager();
|
criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||||
final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
|
||||||
final CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class);
|
final CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class);
|
||||||
final Root<Foo> from = criteriaQuery.from(Foo.class);
|
final Root<Foo> from = criteriaQuery.from(Foo.class);
|
||||||
final CriteriaQuery<Foo> select = criteriaQuery.select(from);
|
final CriteriaQuery<Foo> select = criteriaQuery.select(from);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user