diff --git a/spring-hibernate5/pom.xml b/spring-hibernate5/pom.xml
index 81f8084d74..752cbdba1d 100644
--- a/spring-hibernate5/pom.xml
+++ b/spring-hibernate5/pom.xml
@@ -148,7 +148,6 @@
org.hsqldb
hsqldb
${hsqldb.version}
- test
diff --git a/spring-hibernate5/src/main/java/com/baeldung/hibernate/immutable/util/HibernateUtil.java b/spring-hibernate5/src/main/java/com/baeldung/hibernate/immutable/util/HibernateUtil.java
index 0918ff105a..4ad7c32450 100644
--- a/spring-hibernate5/src/main/java/com/baeldung/hibernate/immutable/util/HibernateUtil.java
+++ b/spring-hibernate5/src/main/java/com/baeldung/hibernate/immutable/util/HibernateUtil.java
@@ -1,5 +1,6 @@
package com.baeldung.hibernate.immutable.util;
+import com.baeldung.hibernate.immutable.entities.Event;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
@@ -12,10 +13,11 @@ public class HibernateUtil {
try {
// Create a session factory from immutable.cfg.xml
Configuration configuration = new Configuration();
+ configuration.addAnnotatedClass(Event.class);
configuration.configure("immutable.cfg.xml");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
- .applySettings(configuration.getProperties())
- .build();
+ .applySettings(configuration.getProperties())
+ .build();
return configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
System.out.println("Initial SessionFactory creation failed." + ex);
diff --git a/spring-hibernate5/src/main/resources/immutable.cfg.xml b/spring-hibernate5/src/main/resources/immutable.cfg.xml
new file mode 100644
index 0000000000..a572ebeac2
--- /dev/null
+++ b/spring-hibernate5/src/main/resources/immutable.cfg.xml
@@ -0,0 +1,32 @@
+
+
+
+
+
+
+
+
+ org.hsqldb.jdbcDriver
+ jdbc:hsqldb:mem:test
+ sa
+
+
+
+ 1
+
+
+ org.hibernate.dialect.HSQLDialect
+
+
+ thread
+
+
+ true
+
+
+ update
+
+
+
\ No newline at end of file
diff --git a/spring-hibernate5/src/test/java/com/baeldung/hibernate/immutable/HibernateImmutableIntegrationTest.java b/spring-hibernate5/src/test/java/com/baeldung/hibernate/immutable/HibernateImmutableIntegrationTest.java
index 1a7aa8cb1d..fb7653d9f4 100644
--- a/spring-hibernate5/src/test/java/com/baeldung/hibernate/immutable/HibernateImmutableIntegrationTest.java
+++ b/spring-hibernate5/src/test/java/com/baeldung/hibernate/immutable/HibernateImmutableIntegrationTest.java
@@ -2,30 +2,36 @@ package com.baeldung.hibernate.immutable;
import com.baeldung.hibernate.immutable.entities.Event;
import com.baeldung.hibernate.immutable.util.HibernateUtil;
-import org.hibernate.HibernateException;
+import com.google.common.collect.Sets;
+import org.hibernate.CacheMode;
import org.hibernate.Session;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
+import org.junit.*;
import org.junit.rules.ExpectedException;
+import javax.persistence.PersistenceException;
+
public class HibernateImmutableIntegrationTest {
- private Session session;
+ private static Session session;
@Rule
public final ExpectedException exception = ExpectedException.none();
@Before
- public void setup() {
+ public void before() {
session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
createEvent();
+ session.setCacheMode(CacheMode.REFRESH);
}
- @After
- public void teardown() {
+ @BeforeClass
+ public static void setup() {
+ session = HibernateUtil.getSessionFactory().getCurrentSession();
+ }
+
+ @AfterClass
+ public static void teardown() {
HibernateUtil.getSessionFactory().close();
}
@@ -40,7 +46,7 @@ public class HibernateImmutableIntegrationTest {
@Test
public void updateEvent() {
Event event = (Event) session.createQuery(
- "FROM Event WHERE title='My Event'").list().get(0);
+ "FROM Event WHERE title='New Event'").list().get(0);
event.setTitle("Private Event");
session.saveOrUpdate(event);
session.getTransaction().commit();
@@ -49,7 +55,7 @@ public class HibernateImmutableIntegrationTest {
@Test
public void deleteEvent() {
Event event = (Event) session.createQuery(
- "FROM Event WHERE title='My Event'").list().get(0);
+ "FROM Event WHERE title='New Event'").list().get(0);
session.delete(event);
session.getTransaction().commit();
}
@@ -61,7 +67,7 @@ public class HibernateImmutableIntegrationTest {
String newGuest = "Sara";
event.getGuestList().add(newGuest);
- exception.expect(HibernateException.class);
+ exception.expect(PersistenceException.class);
session.save(event);
session.getTransaction().commit();
}
@@ -73,14 +79,15 @@ public class HibernateImmutableIntegrationTest {
String guest = event.getGuestList().iterator().next();
event.getGuestList().remove(guest);
- exception.expect(HibernateException.class);
+ exception.expect(PersistenceException.class);
session.saveOrUpdate(event);
session.getTransaction().commit();
}
- public void createEvent() {
+ public static void createEvent() {
Event event = new Event();
event.setTitle("New Event");
+ event.setGuestList(Sets.newHashSet("guest"));
session.save(event);
}
}