persistence cleanup work

This commit is contained in:
eugenp 2014-07-12 12:47:24 +03:00
parent 092b3213ea
commit 120da93492
4 changed files with 34 additions and 69 deletions

View File

@ -8,26 +8,21 @@ import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import com.google.common.collect.Lists;
public class FooSortingPersistenceServiceData {
private static ServiceRegistry serviceRegistry;
private static SessionFactory sessionFactory;
private static Configuration configuration;
private static StandardServiceRegistryBuilder builder;
public class FooFixtures {
private SessionFactory sessionFactory;
public FooSortingPersistenceServiceData() {
public FooFixtures(final SessionFactory sessionFactory) {
super();
this.sessionFactory = sessionFactory;
}
public void createBars() {
// API
configWork();
public void createBars() {
Session session = null;
Transaction tx = null;
session = sessionFactory.openSession();
@ -66,8 +61,6 @@ public class FooSortingPersistenceServiceData {
}
public void createFoos() {
configWork();
Session session = null;
Transaction tx = null;
session = sessionFactory.openSession();
@ -105,15 +98,4 @@ public class FooSortingPersistenceServiceData {
}
}
public void configWork() {
configuration = new Configuration();
configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
configuration.setProperty("dialect", "org.hibernate.dialect.MySQLDialect");
configuration.setProperty(AvailableSettings.DRIVER, "com.mysql.jdbc.Driver");
configuration.setProperty(AvailableSettings.URL, "jdbc:mysql://localhost:3306/HIBERTEST2_TEST");
configuration.setProperty(AvailableSettings.USER, "root");
configuration.setProperty(AvailableSettings.PASS, "");
builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
sessionFactory = configuration.addPackage("com.cc.example.hibernate").addAnnotatedClass(Foo.class).addAnnotatedClass(Bar.class).configure().buildSessionFactory(builder.build());
}
}

View File

@ -34,10 +34,10 @@ import com.google.common.collect.Lists;
public class FooPaginationPersistenceIntegrationTest {
@Autowired
private SessionFactory sessionFactory;
private IFooService fooService;
@Autowired
private IFooService fooService;
private SessionFactory sessionFactory;
private Session session;
@ -140,8 +140,9 @@ public class FooPaginationPersistenceIntegrationTest {
int i = 0;
while (pageSize > i++) {
fooPage.add((Foo) resultScroll.get(0));
if (!resultScroll.next())
if (!resultScroll.next()) {
break;
}
}
assertThat(fooPage, hasSize(lessThan(10 + 1)));

View File

@ -5,8 +5,6 @@ import static org.junit.Assert.assertNull;
import java.util.List;
import java.util.Set;
import javax.imageio.spi.ServiceRegistry;
import org.baeldung.persistence.model.Bar;
import org.baeldung.persistence.model.Foo;
import org.baeldung.spring.PersistenceConfig;
@ -15,14 +13,12 @@ import org.hibernate.NullPrecedence;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Order;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@ -31,40 +27,32 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader;
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
@SuppressWarnings("unchecked")
public class FooSortingPersistenceServiceTest {
private SessionFactory sf;
private Session sess;
private static ServiceRegistry serviceRegistry;
private static Configuration configuration;
private static StandardServiceRegistryBuilder builder;
@Autowired
private SessionFactory sessionFactory;
private Session session;
@Before
public void before() {
session = sessionFactory.openSession();
final FooSortingPersistenceServiceData fooData = new FooSortingPersistenceServiceData();
session.beginTransaction();
final FooFixtures fooData = new FooFixtures(sessionFactory);
fooData.createBars();
configuration = new Configuration();
configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
configuration.setProperty("dialect", "org.hibernate.dialect.MySQLDialect");
configuration.setProperty(AvailableSettings.DRIVER, "com.mysql.jdbc.Driver");
configuration.setProperty(AvailableSettings.URL, "jdbc:mysql://localhost:3306/HIBERTEST2_TEST");
configuration.setProperty(AvailableSettings.USER, "root");
configuration.setProperty(AvailableSettings.PASS, "");
configuration.setProperty("hibernate.show_sql", "true");
builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
sf = configuration.addPackage("org.baeldung.persistence.model").addAnnotatedClass(Foo.class).addAnnotatedClass(Bar.class).configure().buildSessionFactory(builder.build());
sess = sf.openSession();
sess.beginTransaction();
}
@After
public void after() {
sess.getTransaction().commit();
session.getTransaction().commit();
session.close();
}
@Test
public final void whenHQlSortingByOneAttribute_thenPrintSortedResults() {
final String hql = "FROM Foo f ORDER BY f.name";
final Query query = sess.createQuery(hql);
final Query query = session.createQuery(hql);
final List<Foo> fooList = query.list();
for (final Foo foo : fooList) {
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
@ -74,7 +62,7 @@ public class FooSortingPersistenceServiceTest {
@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 Query query = session.createQuery(hql);
final List<Foo> fooList = query.list();
assertNull(fooList.get(fooList.toArray().length - 1).getName());
@ -86,7 +74,7 @@ public class FooSortingPersistenceServiceTest {
@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 Query query = session.createQuery(hql);
final List<Foo> fooList = query.list();
assertNull(fooList.get(0).getName());
for (final Foo foo : fooList) {
@ -98,7 +86,7 @@ public class FooSortingPersistenceServiceTest {
@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 Query query = session.createQuery(hql);
final List<Foo> fooList = query.list();
for (final Foo foo : fooList) {
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
@ -108,7 +96,7 @@ public class FooSortingPersistenceServiceTest {
@Test
public final void whenHQlSortingByMultipleAttributes_thenSortedResults() {
final String hql = "FROM Foo f ORDER BY f.name, f.id";
final Query query = sess.createQuery(hql);
final Query query = session.createQuery(hql);
final List<Foo> fooList = query.list();
for (final Foo foo : fooList) {
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
@ -118,7 +106,7 @@ public class FooSortingPersistenceServiceTest {
@Test
public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrintSortedResults() {
final String hql = "FROM Foo f ORDER BY f.name DESC, f.id ASC";
final Query query = sess.createQuery(hql);
final Query query = session.createQuery(hql);
final List<Foo> fooList = query.list();
for (final Foo foo : fooList) {
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
@ -127,7 +115,7 @@ public class FooSortingPersistenceServiceTest {
@Test
public final void whenHQLCriteriaSortingByOneAttr_thenPrintSortedResults() {
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
final Criteria criteria = session.createCriteria(Foo.class, "FOO");
criteria.addOrder(Order.asc("id"));
final List<Foo> fooList = criteria.list();
for (final Foo foo : fooList) {
@ -137,7 +125,7 @@ public class FooSortingPersistenceServiceTest {
@Test
public final void whenHQLCriteriaSortingByMultipAttr_thenSortedResults() {
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
final Criteria criteria = session.createCriteria(Foo.class, "FOO");
criteria.addOrder(Order.asc("name"));
criteria.addOrder(Order.asc("id"));
final List<Foo> fooList = criteria.list();
@ -148,7 +136,7 @@ public class FooSortingPersistenceServiceTest {
@Test
public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() {
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
final Criteria criteria = session.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());
@ -159,7 +147,7 @@ public class FooSortingPersistenceServiceTest {
@Test
public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() {
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
final Criteria criteria = session.createCriteria(Foo.class, "FOO");
criteria.addOrder(Order.desc("name").nulls(NullPrecedence.FIRST));
final List<Foo> fooList = criteria.list();
assertNull(fooList.get(0).getName());
@ -171,7 +159,7 @@ public class FooSortingPersistenceServiceTest {
@Test
public final void whenSortingBars_thenBarsWithSortedFoos() {
final String hql = "FROM Bar b ORDER BY b.id";
final Query query = sess.createQuery(hql);
final Query query = session.createQuery(hql);
final List<Bar> barList = query.list();
for (final Bar bar : barList) {
final Set<Foo> fooSet = bar.getFooSet();

View File

@ -2,10 +2,7 @@ package org.baeldung.persistence.service;
import org.baeldung.persistence.model.Child;
import org.baeldung.persistence.model.Parent;
import org.baeldung.persistence.service.IChildService;
import org.baeldung.persistence.service.IParentService;
import org.baeldung.spring.PersistenceConfig;
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@ -24,9 +21,6 @@ public class ParentServicePersistenceIntegrationTest {
@Autowired
private IChildService childService;
@Autowired
private SessionFactory sessionFactory;
// tests
@Test