Update FooSortingServiceTest.java
This commit is contained in:
parent
87b25ea4d7
commit
730ebd57af
|
@ -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 String hql = "FROM Foo f ORDER BY f.name";
|
||||||
final SessionFactory sf = new Configuration().configure().buildSessionFactory();
|
final Query query = sess.createQuery(hql);
|
||||||
sess = sf.openSession();
|
final List<Foo> fooList = query.list();
|
||||||
final String hql = "FROM Foo f ORDER BY f.name";
|
for (final Foo foo : fooList) {
|
||||||
final Query query = sess.createQuery(hql);
|
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
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
|
@Test
|
||||||
public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSortedResults() {
|
public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSortedResults() {
|
||||||
Session sess = null;
|
|
||||||
List<Foo> fooList = Lists.newArrayList();
|
|
||||||
|
|
||||||
try {
|
final String hql = "FROM Foo f ORDER BY f.name ASC";
|
||||||
final SessionFactory sf = new Configuration().configure().buildSessionFactory();
|
final Query query = sess.createQuery(hql);
|
||||||
sess = sf.openSession();
|
final List<Foo> fooList = query.list();
|
||||||
final String hql = "FROM Foo f ORDER BY f.name ASC";
|
for (final Foo foo : fooList) {
|
||||||
final Query query = sess.createQuery(hql);
|
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
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 String hql = "FROM Foo f ORDER BY f.name, f.id";
|
||||||
final SessionFactory sf = new Configuration().configure().buildSessionFactory();
|
final Query query = sess.createQuery(hql);
|
||||||
sess = sf.openSession();
|
final List<Foo> fooList = query.list();
|
||||||
final String hql = "FROM Foo f ORDER BY f.name, f.id";
|
for (final Foo foo : fooList) {
|
||||||
final Query query = sess.createQuery(hql);
|
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
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 String hql = "FROM Foo f ORDER BY f.name DESC, f.id ASC";
|
||||||
final SessionFactory sf = new Configuration().configure().buildSessionFactory();
|
final Query query = sess.createQuery(hql);
|
||||||
sess = sf.openSession();
|
final List<Foo> fooList = query.list();
|
||||||
final String hql = "FROM Foo f ORDER BY f.name DESC, f.id ASC";
|
for (final Foo foo : fooList) {
|
||||||
final Query query = sess.createQuery(hql);
|
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
sess.getTransaction().commit();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@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");
|
|
||||||
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();
|
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
||||||
} catch (final Exception ex) {
|
criteria.addOrder(Order.asc("id"));
|
||||||
ex.printStackTrace();
|
final List<Foo> fooList = criteria.list();
|
||||||
} finally {
|
for (final Foo foo : fooList) {
|
||||||
if (sess != null) {
|
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
||||||
sess.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
sess.getTransaction().commit();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@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");
|
|
||||||
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();
|
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
||||||
} catch (final Exception ex) {
|
criteria.addOrder(Order.asc("name"));
|
||||||
ex.printStackTrace();
|
criteria.addOrder(Order.asc("id"));
|
||||||
} finally {
|
final List<Foo> fooList = criteria.list();
|
||||||
if (sess != null) {
|
for (final Foo foo : fooList) {
|
||||||
sess.close();
|
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
sess.getTransaction().commit();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() {
|
public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() {
|
||||||
Session sess = null;
|
|
||||||
final SessionFactory sf = new Configuration().configure().buildSessionFactory();
|
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
||||||
sess = sf.openSession();
|
criteria.addOrder(Order.asc("name").nulls(NullPrecedence.LAST));
|
||||||
List<Foo> fooList = Lists.newArrayList();
|
final List<Foo> fooList = criteria.list();
|
||||||
try {
|
assertNull(fooList.get(fooList.toArray().length - 1).getName());
|
||||||
sess.beginTransaction();
|
for (final Foo foo : fooList) {
|
||||||
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
sess.getTransaction().commit();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() {
|
public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() {
|
||||||
Session sess = null;
|
|
||||||
final SessionFactory sf = new Configuration().configure().buildSessionFactory();
|
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
||||||
sess = sf.openSession();
|
criteria.addOrder(Order.desc("name").nulls(NullPrecedence.FIRST));
|
||||||
List<Foo> fooList = Lists.newArrayList();
|
final List<Foo> fooList = criteria.list();
|
||||||
try {
|
assertNull(fooList.get(0).getName());
|
||||||
sess.beginTransaction();
|
for (final Foo foo : fooList) {
|
||||||
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
sess.getTransaction().commit();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@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 String hql = "FROM Bar b ORDER BY b.id";
|
||||||
final SessionFactory sf = new Configuration().configure().buildSessionFactory();
|
final Query query = sess.createQuery(hql);
|
||||||
sess = sf.openSession();
|
final List<Bar> barList = query.list();
|
||||||
final String hql = "FROM Bar b ORDER BY b.id";
|
for (final Bar bar : barList) {
|
||||||
final Query query = sess.createQuery(hql);
|
final Set<Foo> fooSet = bar.getFooList();
|
||||||
barList = query.list();
|
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue