From a2c98ab37f314e609d1cb6159fd0a1ac24e5b78d Mon Sep 17 00:00:00 2001 From: eugenp Date: Sun, 11 May 2014 10:35:35 +0300 Subject: [PATCH] minor cleanup --- .../org/baeldung/persistence/model/Bar.java | 21 ++++---- .../baeldung/persistence/model/Foo.hbm.xml | 2 - .../org/baeldung/persistence/model/Foo.java | 22 ++++---- .../src/test/java/hibernate.cfg.xml | 8 +-- ...erviceBasicPersistenceIntegrationTest.java | 54 +++++++++++++++++++ .../service/FooSortingServiceTest.java | 37 ++++--------- 6 files changed, 90 insertions(+), 54 deletions(-) create mode 100644 spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServiceBasicPersistenceIntegrationTest.java diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.java b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.java index b6cedbd772..beca727200 100644 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.java +++ b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.java @@ -20,6 +20,16 @@ import com.google.common.collect.Sets; @NamedQuery(name = "Bar.findAll", query = "SELECT b FROM Bar b") public class Bar implements Serializable { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private int id; + + private String name; + + @OneToMany(mappedBy = "bar", cascade = CascadeType.ALL, fetch = FetchType.EAGER) + @OrderBy(clause = "NAME DESC") + private Set fooSet = Sets.newHashSet(); + public Bar() { super(); } @@ -30,17 +40,6 @@ public class Bar implements Serializable { this.name = name; } - private static final long serialVersionUID = 1L; - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private int id; - - private String name; - - @OneToMany(mappedBy = "bar", cascade = CascadeType.ALL, fetch = FetchType.EAGER) - @OrderBy(clause = "NAME DESC") - Set fooSet = Sets.newHashSet(); - // API public Set getFooSet() { diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.hbm.xml b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.hbm.xml index 09922c8783..db7ec3010b 100644 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.hbm.xml +++ b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.hbm.xml @@ -15,7 +15,5 @@ - - diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java index 1f4ec7a544..f6280c0363 100644 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java +++ b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java @@ -2,6 +2,7 @@ package org.baeldung.persistence.model; import java.io.Serializable; +import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; @@ -15,7 +16,17 @@ import org.hibernate.annotations.FetchMode; @Entity public class Foo implements Serializable { - private static final long serialVersionUID = 1L; + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long id; + + @Column(nullable = false) + private String name; + + @ManyToOne(targetEntity = Bar.class) + @JoinColumn(name = "BAR_ID") + @Fetch(FetchMode.JOIN) + private Bar bar = new Bar(); public Foo() { super(); @@ -27,14 +38,7 @@ public class Foo implements Serializable { this.name = name; } - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private long id; - private String name; - @ManyToOne(targetEntity = Bar.class) - @JoinColumn(name = "BAR_ID") - @Fetch(FetchMode.JOIN) - private Bar bar = new Bar(); + // public Bar getBar() { return bar; diff --git a/spring-hibernate4/src/test/java/hibernate.cfg.xml b/spring-hibernate4/src/test/java/hibernate.cfg.xml index 915295111e..3c4e53efb8 100644 --- a/spring-hibernate4/src/test/java/hibernate.cfg.xml +++ b/spring-hibernate4/src/test/java/hibernate.cfg.xml @@ -9,9 +9,9 @@ com.mysql.jdbc.Driver -jdbc:mysql://localhost:3306/HIBERTEST2_TEST -root - +jdbc:mysql://localhost:3306/spring_hibernate4_01?createDatabaseIfNotExist=true +tutorialuser +tutorialmy5ql 1 @@ -33,5 +33,5 @@ - + diff --git a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServiceBasicPersistenceIntegrationTest.java b/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServiceBasicPersistenceIntegrationTest.java new file mode 100644 index 0000000000..457f5cc519 --- /dev/null +++ b/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServiceBasicPersistenceIntegrationTest.java @@ -0,0 +1,54 @@ +package org.baeldung.persistence.service; + +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; + +import org.baeldung.persistence.model.Foo; +import org.baeldung.spring.PersistenceConfig; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +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; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) +public class FooServiceBasicPersistenceIntegrationTest { + + @Autowired + private SessionFactory sessionFactory; + + @Autowired + private IFooService fooService; + + private Session session; + + // tests + + @Before + public final void before() { + session = sessionFactory.openSession(); + } + + @After + public final void after() { + session.close(); + } + + // tests + + @Test + public final void whenContextIsBootstrapped_thenNoExceptions() { + // + } + + @Test + public final void whenEntityIsCreated_thenNoExceptions() { + fooService.create(new Foo(randomAlphabetic(6))); + } + +} diff --git a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java b/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java index 103d87b1f3..b75e2b2f3b 100644 --- a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java +++ b/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java @@ -12,7 +12,7 @@ import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.junit.After; -import org.junit.BeforeClass; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; @@ -21,32 +21,26 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) - - - public class FooSortingServiceTest { - private static Configuration configuration; - private static StandardServiceRegistryBuilder builder; - private static SessionFactory sf; - private static Session sess; + private SessionFactory sf; + private Session sess; - @BeforeClass - public static void before() { - - configuration = new Configuration().configure(); - builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()); + @Before + public void before() { + final Configuration configuration = new Configuration().configure(); + final StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()); sf = configuration.buildSessionFactory(builder.build()); sess = sf.openSession(); sess.beginTransaction(); } + @After public void after() { - + // } /* @Test public final void whenHQlSortingByOneAttribute_thenPrintSortedResults() { - final String hql = "FROM Foo f ORDER BY f.name"; final Query query = sess.createQuery(hql); final List fooList = query.list(); @@ -54,12 +48,10 @@ public class FooSortingServiceTest { System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); } 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 fooList = query.list(); @@ -72,7 +64,6 @@ public class FooSortingServiceTest { @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 fooList = query.list(); @@ -86,7 +77,6 @@ public class FooSortingServiceTest { @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 fooList = query.list(); @@ -100,7 +90,6 @@ public class FooSortingServiceTest { @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 List fooList = query.list(); @@ -114,7 +103,6 @@ public class FooSortingServiceTest { @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 List fooList = query.list(); @@ -126,7 +114,6 @@ public class FooSortingServiceTest { @Test public final void whenHQLCriteriaSortingByOneAttr_thenPrintSortedResults() { - final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); criteria.addOrder(Order.asc("id")); final List fooList = criteria.list(); @@ -138,7 +125,6 @@ public class FooSortingServiceTest { @Test public final void whenHQLCriteriaSortingByMultipAttr_thenSortedResults() { - final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); criteria.addOrder(Order.asc("name")); criteria.addOrder(Order.asc("id")); @@ -151,7 +137,6 @@ public class FooSortingServiceTest { @Test public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() { - final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); criteria.addOrder(Order.asc("name").nulls(NullPrecedence.LAST)); final List fooList = criteria.list(); @@ -164,7 +149,6 @@ public class FooSortingServiceTest { @Test public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() { - final Criteria criteria = sess.createCriteria(Foo.class, "FOO"); criteria.addOrder(Order.desc("name").nulls(NullPrecedence.FIRST)); final List fooList = criteria.list(); @@ -174,12 +158,10 @@ public class FooSortingServiceTest { } sess.getTransaction().commit(); - }*/ @Test public final void whenSortingBars_thenBarsWithSortedFoos() { - final String hql = "FROM Bar b ORDER BY b.id"; final Query query = sess.createQuery(hql); final List barList = query.list(); @@ -192,7 +174,6 @@ public class FooSortingServiceTest { } } sess.getTransaction().commit(); - } }