small cleanup work
This commit is contained in:
parent
1e9eefc833
commit
a9738345e8
@ -7,31 +7,31 @@
|
|||||||
|
|
||||||
<hibernate-configuration>
|
<hibernate-configuration>
|
||||||
<session-factory>
|
<session-factory>
|
||||||
<!-- Database connection settings -->
|
<!-- Database connection settings -->
|
||||||
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
|
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
|
||||||
<property name="connection.url">jdbc:mysql://localhost:3306/spring_hibernate4_01?createDatabaseIfNotExist=true</property>
|
<property name="connection.url">jdbc:mysql://localhost:3306/spring_hibernate4_01?createDatabaseIfNotExist=true</property>
|
||||||
<property name="connection.username">tutorialuser</property>
|
<property name="connection.username">tutorialuser</property>
|
||||||
<property name="connection.password">tutorialmy5ql</property>
|
<property name="connection.password">tutorialmy5ql</property>
|
||||||
|
|
||||||
<!-- JDBC connection pool (use the built-in) -->
|
|
||||||
<property name="connection.pool_size">1</property>
|
|
||||||
|
|
||||||
<!-- SQL dialect -->
|
|
||||||
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
|
|
||||||
|
|
||||||
<!-- Enable Hibernate's automatic session context management -->
|
|
||||||
<property name="current_session_context_class">thread</property>
|
|
||||||
|
|
||||||
<!-- Disable the second-level cache -->
|
|
||||||
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
|
|
||||||
|
|
||||||
<!-- Echo all executed SQL to stdout -->
|
|
||||||
<property name="show_sql">true</property>
|
|
||||||
|
|
||||||
<!-- Drop and re-create the database schema on startup -->
|
<!-- JDBC connection pool (use the built-in) -->
|
||||||
|
<property name="connection.pool_size">1</property>
|
||||||
|
|
||||||
<mapping resource="org//baeldung//persistence//model//Foo.hbm.xml"/>
|
<!-- SQL dialect -->
|
||||||
<mapping resource="org//baeldung//persistence//model//Bar.hbm.xml"/>
|
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
|
||||||
</session-factory>
|
|
||||||
|
<!-- Enable Hibernate's automatic session context management -->
|
||||||
|
<property name="current_session_context_class">thread</property>
|
||||||
|
|
||||||
|
<!-- Disable the second-level cache -->
|
||||||
|
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
|
||||||
|
|
||||||
|
<!-- Echo all executed SQL to stdout -->
|
||||||
|
<property name="show_sql">true</property>
|
||||||
|
|
||||||
|
<!-- Drop and re-create the database schema on startup -->
|
||||||
|
|
||||||
|
<mapping resource="org//baeldung//persistence//model//Foo.hbm.xml" />
|
||||||
|
<mapping resource="org//baeldung//persistence//model//Bar.hbm.xml" />
|
||||||
|
</session-factory>
|
||||||
|
|
||||||
</hibernate-configuration>
|
</hibernate-configuration>
|
||||||
|
@ -1,16 +1,21 @@
|
|||||||
package org.baeldung.persistence.hibernate;
|
package org.baeldung.persistence.hibernate;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.baeldung.persistence.model.Bar;
|
import org.baeldung.persistence.model.Bar;
|
||||||
import org.baeldung.persistence.model.Foo;
|
import org.baeldung.persistence.model.Foo;
|
||||||
import org.baeldung.spring.PersistenceConfig;
|
import org.baeldung.spring.PersistenceConfig;
|
||||||
|
import org.hibernate.Criteria;
|
||||||
|
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.boot.registry.StandardServiceRegistryBuilder;
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
import org.hibernate.cfg.Configuration;
|
import org.hibernate.cfg.Configuration;
|
||||||
|
import org.hibernate.criterion.Order;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -21,12 +26,13 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
|||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public class FooSortingPersistenceServiceTest {
|
public class FooSortingPersistenceServiceTest {
|
||||||
private SessionFactory sf;
|
private SessionFactory sf;
|
||||||
private Session sess;
|
private Session sess;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void before() {
|
public final void before() {
|
||||||
final Configuration configuration = new Configuration().configure();
|
final Configuration configuration = new Configuration().configure();
|
||||||
final StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
|
final StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
|
||||||
sf = configuration.buildSessionFactory(builder.build());
|
sf = configuration.buildSessionFactory(builder.build());
|
||||||
@ -36,129 +42,115 @@ public class FooSortingPersistenceServiceTest {
|
|||||||
|
|
||||||
@After
|
@After
|
||||||
public void after() {
|
public void after() {
|
||||||
//
|
sess.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* @Test
|
@Test
|
||||||
public final void whenHQlSortingByOneAttribute_thenPrintSortedResults() {
|
public final void whenHQlSortingByOneAttribute_thenPrintSortedResults() {
|
||||||
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);
|
||||||
final List<Foo> 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());
|
||||||
}
|
}
|
||||||
sess.getTransaction().commit();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenHQlSortingByStringNullLast_thenLastNull() {
|
public final void whenHQlSortingByStringNullLast_thenLastNull() {
|
||||||
final String hql = "FROM Foo f ORDER BY f.name NULLS LAST";
|
final String hql = "FROM Foo f ORDER BY f.name NULLS LAST";
|
||||||
final Query query = sess.createQuery(hql);
|
final Query query = sess.createQuery(hql);
|
||||||
final List<Foo> fooList = query.list();
|
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
|
assertNull(fooList.get(fooList.toArray().length - 1).getName());
|
||||||
public final void whenSortingByStringNullsFirst_thenReturnNullsFirst() {
|
for (final Foo foo : fooList) {
|
||||||
final String hql = "FROM Foo f ORDER BY f.name NULLS FIRST";
|
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
|
||||||
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());
|
|
||||||
|
|
||||||
}
|
@Test
|
||||||
sess.getTransaction().commit();
|
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());
|
||||||
|
|
||||||
@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<Foo> fooList = query.list();
|
|
||||||
for (final Foo foo : fooList) {
|
|
||||||
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()
|
|
||||||
|
|
||||||
);
|
@Test
|
||||||
}
|
public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSortedResults() {
|
||||||
sess.getTransaction().commit();
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenHQlSortingByMultipleAttributes_thenSortedResults() {
|
public final void whenHQlSortingByMultipleAttributes_thenSortedResults() {
|
||||||
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);
|
||||||
final List<Foo> 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
);
|
@Test
|
||||||
}
|
public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrintSortedResults() {
|
||||||
sess.getTransaction().commit();
|
final String hql = "FROM Foo f ORDER BY f.name DESC, f.id ASC";
|
||||||
}
|
final Query query = sess.createQuery(hql);
|
||||||
|
final List<Foo> fooList = query.list();
|
||||||
|
for (final Foo foo : fooList) {
|
||||||
|
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrintSortedResults() {
|
public final void whenHQLCriteriaSortingByOneAttr_thenPrintSortedResults() {
|
||||||
final String hql = "FROM Foo f ORDER BY f.name DESC, f.id ASC";
|
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
||||||
final Query query = sess.createQuery(hql);
|
criteria.addOrder(Order.asc("id"));
|
||||||
final List<Foo> fooList = query.list();
|
final List<Foo> fooList = criteria.list();
|
||||||
for (final Foo foo : fooList) {
|
for (final Foo foo : fooList) {
|
||||||
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
|
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
||||||
}
|
}
|
||||||
sess.getTransaction().commit();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenHQLCriteriaSortingByOneAttr_thenPrintSortedResults() {
|
public final void whenHQLCriteriaSortingByMultipAttr_thenSortedResults() {
|
||||||
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("name"));
|
||||||
final List<Foo> fooList = criteria.list();
|
criteria.addOrder(Order.asc("id"));
|
||||||
for (final Foo foo : fooList) {
|
final List<Foo> fooList = criteria.list();
|
||||||
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
for (final Foo foo : fooList) {
|
||||||
}
|
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
||||||
sess.getTransaction().commit();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenHQLCriteriaSortingByMultipAttr_thenSortedResults() {
|
public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() {
|
||||||
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").nulls(NullPrecedence.LAST));
|
||||||
criteria.addOrder(Order.asc("id"));
|
final List<Foo> fooList = criteria.list();
|
||||||
final List<Foo> fooList = criteria.list();
|
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();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() {
|
public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() {
|
||||||
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.desc("name").nulls(NullPrecedence.FIRST));
|
||||||
final List<Foo> fooList = criteria.list();
|
final List<Foo> fooList = criteria.list();
|
||||||
assertNull(fooList.get(fooList.toArray().length - 1).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();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() {
|
|
||||||
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
|
@Test
|
||||||
public final void whenSortingBars_thenBarsWithSortedFoos() {
|
public final void whenSortingBars_thenBarsWithSortedFoos() {
|
||||||
@ -170,10 +162,8 @@ public class FooSortingPersistenceServiceTest {
|
|||||||
System.out.println("Bar Id:" + bar.getId());
|
System.out.println("Bar Id:" + bar.getId());
|
||||||
for (final Foo foo : fooSet) {
|
for (final Foo foo : fooSet) {
|
||||||
System.out.println("FooName:" + foo.getName());
|
System.out.println("FooName:" + foo.getName());
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sess.getTransaction().commit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user