persistence cleanup work
This commit is contained in:
parent
092b3213ea
commit
120da93492
@ -8,26 +8,21 @@ import org.hibernate.HibernateException;
|
|||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
import org.hibernate.Transaction;
|
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;
|
import com.google.common.collect.Lists;
|
||||||
|
|
||||||
public class FooSortingPersistenceServiceData {
|
public class FooFixtures {
|
||||||
private static ServiceRegistry serviceRegistry;
|
private SessionFactory sessionFactory;
|
||||||
private static SessionFactory sessionFactory;
|
|
||||||
private static Configuration configuration;
|
|
||||||
private static StandardServiceRegistryBuilder builder;
|
|
||||||
|
|
||||||
public FooSortingPersistenceServiceData() {
|
public FooFixtures(final SessionFactory sessionFactory) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
|
this.sessionFactory = sessionFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void createBars() {
|
// API
|
||||||
|
|
||||||
configWork();
|
public void createBars() {
|
||||||
Session session = null;
|
Session session = null;
|
||||||
Transaction tx = null;
|
Transaction tx = null;
|
||||||
session = sessionFactory.openSession();
|
session = sessionFactory.openSession();
|
||||||
@ -66,8 +61,6 @@ public class FooSortingPersistenceServiceData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void createFoos() {
|
public void createFoos() {
|
||||||
|
|
||||||
configWork();
|
|
||||||
Session session = null;
|
Session session = null;
|
||||||
Transaction tx = null;
|
Transaction tx = null;
|
||||||
session = sessionFactory.openSession();
|
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());
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -34,10 +34,10 @@ import com.google.common.collect.Lists;
|
|||||||
public class FooPaginationPersistenceIntegrationTest {
|
public class FooPaginationPersistenceIntegrationTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private SessionFactory sessionFactory;
|
private IFooService fooService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private IFooService fooService;
|
private SessionFactory sessionFactory;
|
||||||
|
|
||||||
private Session session;
|
private Session session;
|
||||||
|
|
||||||
@ -140,9 +140,10 @@ public class FooPaginationPersistenceIntegrationTest {
|
|||||||
int i = 0;
|
int i = 0;
|
||||||
while (pageSize > i++) {
|
while (pageSize > i++) {
|
||||||
fooPage.add((Foo) resultScroll.get(0));
|
fooPage.add((Foo) resultScroll.get(0));
|
||||||
if (!resultScroll.next())
|
if (!resultScroll.next()) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
assertThat(fooPage, hasSize(lessThan(10 + 1)));
|
assertThat(fooPage, hasSize(lessThan(10 + 1)));
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,6 @@ import static org.junit.Assert.assertNull;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.imageio.spi.ServiceRegistry;
|
|
||||||
|
|
||||||
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;
|
||||||
@ -15,14 +13,12 @@ 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.cfg.AvailableSettings;
|
|
||||||
import org.hibernate.cfg.Configuration;
|
|
||||||
import org.hibernate.criterion.Order;
|
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;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
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)
|
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public class FooSortingPersistenceServiceTest {
|
public class FooSortingPersistenceServiceTest {
|
||||||
private SessionFactory sf;
|
|
||||||
private Session sess;
|
@Autowired
|
||||||
private static ServiceRegistry serviceRegistry;
|
private SessionFactory sessionFactory;
|
||||||
private static Configuration configuration;
|
|
||||||
private static StandardServiceRegistryBuilder builder;
|
private Session session;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void before() {
|
public void before() {
|
||||||
|
session = sessionFactory.openSession();
|
||||||
|
|
||||||
final FooSortingPersistenceServiceData fooData = new FooSortingPersistenceServiceData();
|
session.beginTransaction();
|
||||||
|
|
||||||
|
final FooFixtures fooData = new FooFixtures(sessionFactory);
|
||||||
fooData.createBars();
|
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
|
@After
|
||||||
public void after() {
|
public void after() {
|
||||||
sess.getTransaction().commit();
|
session.getTransaction().commit();
|
||||||
|
session.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@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 = session.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());
|
||||||
@ -74,7 +62,7 @@ public class FooSortingPersistenceServiceTest {
|
|||||||
@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 = session.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());
|
||||||
@ -86,7 +74,7 @@ public class FooSortingPersistenceServiceTest {
|
|||||||
@Test
|
@Test
|
||||||
public final void whenSortingByStringNullsFirst_thenReturnNullsFirst() {
|
public final void whenSortingByStringNullsFirst_thenReturnNullsFirst() {
|
||||||
final String hql = "FROM Foo f ORDER BY f.name NULLS FIRST";
|
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();
|
final List<Foo> fooList = query.list();
|
||||||
assertNull(fooList.get(0).getName());
|
assertNull(fooList.get(0).getName());
|
||||||
for (final Foo foo : fooList) {
|
for (final Foo foo : fooList) {
|
||||||
@ -98,7 +86,7 @@ public class FooSortingPersistenceServiceTest {
|
|||||||
@Test
|
@Test
|
||||||
public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSortedResults() {
|
public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSortedResults() {
|
||||||
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 = session.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());
|
||||||
@ -108,7 +96,7 @@ public class FooSortingPersistenceServiceTest {
|
|||||||
@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 = session.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());
|
||||||
@ -118,7 +106,7 @@ public class FooSortingPersistenceServiceTest {
|
|||||||
@Test
|
@Test
|
||||||
public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrintSortedResults() {
|
public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrintSortedResults() {
|
||||||
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 = session.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());
|
||||||
@ -127,7 +115,7 @@ public class FooSortingPersistenceServiceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenHQLCriteriaSortingByOneAttr_thenPrintSortedResults() {
|
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"));
|
criteria.addOrder(Order.asc("id"));
|
||||||
final List<Foo> fooList = criteria.list();
|
final List<Foo> fooList = criteria.list();
|
||||||
for (final Foo foo : fooList) {
|
for (final Foo foo : fooList) {
|
||||||
@ -137,7 +125,7 @@ public class FooSortingPersistenceServiceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenHQLCriteriaSortingByMultipAttr_thenSortedResults() {
|
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("name"));
|
||||||
criteria.addOrder(Order.asc("id"));
|
criteria.addOrder(Order.asc("id"));
|
||||||
final List<Foo> fooList = criteria.list();
|
final List<Foo> fooList = criteria.list();
|
||||||
@ -148,7 +136,7 @@ public class FooSortingPersistenceServiceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() {
|
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));
|
criteria.addOrder(Order.asc("name").nulls(NullPrecedence.LAST));
|
||||||
final List<Foo> 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());
|
||||||
@ -159,7 +147,7 @@ public class FooSortingPersistenceServiceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() {
|
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));
|
criteria.addOrder(Order.desc("name").nulls(NullPrecedence.FIRST));
|
||||||
final List<Foo> fooList = criteria.list();
|
final List<Foo> fooList = criteria.list();
|
||||||
assertNull(fooList.get(0).getName());
|
assertNull(fooList.get(0).getName());
|
||||||
@ -171,7 +159,7 @@ public class FooSortingPersistenceServiceTest {
|
|||||||
@Test
|
@Test
|
||||||
public final void whenSortingBars_thenBarsWithSortedFoos() {
|
public final void whenSortingBars_thenBarsWithSortedFoos() {
|
||||||
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 = session.createQuery(hql);
|
||||||
final List<Bar> barList = query.list();
|
final List<Bar> barList = query.list();
|
||||||
for (final Bar bar : barList) {
|
for (final Bar bar : barList) {
|
||||||
final Set<Foo> fooSet = bar.getFooSet();
|
final Set<Foo> fooSet = bar.getFooSet();
|
||||||
|
@ -2,10 +2,7 @@ package org.baeldung.persistence.service;
|
|||||||
|
|
||||||
import org.baeldung.persistence.model.Child;
|
import org.baeldung.persistence.model.Child;
|
||||||
import org.baeldung.persistence.model.Parent;
|
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.baeldung.spring.PersistenceConfig;
|
||||||
import org.hibernate.SessionFactory;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -24,9 +21,6 @@ public class ParentServicePersistenceIntegrationTest {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private IChildService childService;
|
private IChildService childService;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private SessionFactory sessionFactory;
|
|
||||||
|
|
||||||
// tests
|
// tests
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user