Refactor Hibernate5 examples (#1362)
This commit is contained in:
parent
e0d59494c1
commit
ffb24cf312
|
@ -148,7 +148,6 @@
|
||||||
<groupId>org.hsqldb</groupId>
|
<groupId>org.hsqldb</groupId>
|
||||||
<artifactId>hsqldb</artifactId>
|
<artifactId>hsqldb</artifactId>
|
||||||
<version>${hsqldb.version}</version>
|
<version>${hsqldb.version}</version>
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package com.baeldung.hibernate.immutable.util;
|
package com.baeldung.hibernate.immutable.util;
|
||||||
|
|
||||||
|
import com.baeldung.hibernate.immutable.entities.Event;
|
||||||
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;
|
||||||
|
@ -12,6 +13,7 @@ public class HibernateUtil {
|
||||||
try {
|
try {
|
||||||
// Create a session factory from immutable.cfg.xml
|
// Create a session factory from immutable.cfg.xml
|
||||||
Configuration configuration = new Configuration();
|
Configuration configuration = new Configuration();
|
||||||
|
configuration.addAnnotatedClass(Event.class);
|
||||||
configuration.configure("immutable.cfg.xml");
|
configuration.configure("immutable.cfg.xml");
|
||||||
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
|
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
|
||||||
.applySettings(configuration.getProperties())
|
.applySettings(configuration.getProperties())
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
<?xml version='1.0' encoding='utf-8'?>
|
||||||
|
<!DOCTYPE hibernate-configuration PUBLIC
|
||||||
|
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
|
||||||
|
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
||||||
|
|
||||||
|
<hibernate-configuration>
|
||||||
|
|
||||||
|
<session-factory>
|
||||||
|
|
||||||
|
<!-- Database connection settings -->
|
||||||
|
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
|
||||||
|
<property name="connection.url">jdbc:hsqldb:mem:test</property>
|
||||||
|
<property name="connection.username">sa</property>
|
||||||
|
<property name="connection.password"></property>
|
||||||
|
|
||||||
|
<!-- JDBC connection pool (use the built-in) -->
|
||||||
|
<property name="connection.pool_size">1</property>
|
||||||
|
|
||||||
|
<!-- SQL dialect -->
|
||||||
|
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
|
||||||
|
|
||||||
|
<!-- Enable Hibernate's automatic session context management -->
|
||||||
|
<property name="current_session_context_class">thread</property>
|
||||||
|
|
||||||
|
<!-- Echo all executed SQL to stdout -->
|
||||||
|
<property name="show_sql">true</property>
|
||||||
|
|
||||||
|
<!-- Drop and re-create the database schema on startup -->
|
||||||
|
<property name="hbm2ddl.auto">update</property>
|
||||||
|
</session-factory>
|
||||||
|
|
||||||
|
</hibernate-configuration>
|
|
@ -2,30 +2,36 @@ package com.baeldung.hibernate.immutable;
|
||||||
|
|
||||||
import com.baeldung.hibernate.immutable.entities.Event;
|
import com.baeldung.hibernate.immutable.entities.Event;
|
||||||
import com.baeldung.hibernate.immutable.util.HibernateUtil;
|
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.hibernate.Session;
|
||||||
import org.junit.After;
|
import org.junit.*;
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Rule;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.rules.ExpectedException;
|
import org.junit.rules.ExpectedException;
|
||||||
|
|
||||||
|
import javax.persistence.PersistenceException;
|
||||||
|
|
||||||
public class HibernateImmutableIntegrationTest {
|
public class HibernateImmutableIntegrationTest {
|
||||||
|
|
||||||
private Session session;
|
private static Session session;
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public final ExpectedException exception = ExpectedException.none();
|
public final ExpectedException exception = ExpectedException.none();
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void before() {
|
||||||
session = HibernateUtil.getSessionFactory().getCurrentSession();
|
session = HibernateUtil.getSessionFactory().getCurrentSession();
|
||||||
session.beginTransaction();
|
session.beginTransaction();
|
||||||
createEvent();
|
createEvent();
|
||||||
|
session.setCacheMode(CacheMode.REFRESH);
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@BeforeClass
|
||||||
public void teardown() {
|
public static void setup() {
|
||||||
|
session = HibernateUtil.getSessionFactory().getCurrentSession();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterClass
|
||||||
|
public static void teardown() {
|
||||||
HibernateUtil.getSessionFactory().close();
|
HibernateUtil.getSessionFactory().close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,7 +46,7 @@ public class HibernateImmutableIntegrationTest {
|
||||||
@Test
|
@Test
|
||||||
public void updateEvent() {
|
public void updateEvent() {
|
||||||
Event event = (Event) session.createQuery(
|
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");
|
event.setTitle("Private Event");
|
||||||
session.saveOrUpdate(event);
|
session.saveOrUpdate(event);
|
||||||
session.getTransaction().commit();
|
session.getTransaction().commit();
|
||||||
|
@ -49,7 +55,7 @@ public class HibernateImmutableIntegrationTest {
|
||||||
@Test
|
@Test
|
||||||
public void deleteEvent() {
|
public void deleteEvent() {
|
||||||
Event event = (Event) session.createQuery(
|
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.delete(event);
|
||||||
session.getTransaction().commit();
|
session.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
@ -61,7 +67,7 @@ public class HibernateImmutableIntegrationTest {
|
||||||
String newGuest = "Sara";
|
String newGuest = "Sara";
|
||||||
event.getGuestList().add(newGuest);
|
event.getGuestList().add(newGuest);
|
||||||
|
|
||||||
exception.expect(HibernateException.class);
|
exception.expect(PersistenceException.class);
|
||||||
session.save(event);
|
session.save(event);
|
||||||
session.getTransaction().commit();
|
session.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
@ -73,14 +79,15 @@ public class HibernateImmutableIntegrationTest {
|
||||||
String guest = event.getGuestList().iterator().next();
|
String guest = event.getGuestList().iterator().next();
|
||||||
event.getGuestList().remove(guest);
|
event.getGuestList().remove(guest);
|
||||||
|
|
||||||
exception.expect(HibernateException.class);
|
exception.expect(PersistenceException.class);
|
||||||
session.saveOrUpdate(event);
|
session.saveOrUpdate(event);
|
||||||
session.getTransaction().commit();
|
session.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void createEvent() {
|
public static void createEvent() {
|
||||||
Event event = new Event();
|
Event event = new Event();
|
||||||
event.setTitle("New Event");
|
event.setTitle("New Event");
|
||||||
|
event.setGuestList(Sets.newHashSet("guest"));
|
||||||
session.save(event);
|
session.save(event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue