small cleanup work

This commit is contained in:
eugenp 2014-05-11 16:17:22 +03:00
parent 1e9eefc833
commit a9738345e8
2 changed files with 127 additions and 137 deletions

View File

@ -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,10 +42,10 @@ 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);
@ -47,7 +53,6 @@ public class FooSortingPersistenceServiceTest {
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
@ -55,11 +60,11 @@ public class FooSortingPersistenceServiceTest {
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()); assertNull(fooList.get(fooList.toArray().length - 1).getName());
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
@ -72,7 +77,6 @@ public class FooSortingPersistenceServiceTest {
System.out.println("Name:" + foo.getName()); System.out.println("Name:" + foo.getName());
} }
sess.getTransaction().commit();
} }
@Test @Test
@ -81,11 +85,8 @@ public class FooSortingPersistenceServiceTest {
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
@ -94,11 +95,8 @@ public class FooSortingPersistenceServiceTest {
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
@ -109,7 +107,6 @@ public class FooSortingPersistenceServiceTest {
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
@ -120,7 +117,6 @@ public class FooSortingPersistenceServiceTest {
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
@ -132,7 +128,6 @@ public class FooSortingPersistenceServiceTest {
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
@ -144,7 +139,6 @@ public class FooSortingPersistenceServiceTest {
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
@ -155,10 +149,8 @@ public class FooSortingPersistenceServiceTest {
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(); }
}*/
@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();
} }
} }