Refactor Hibernate5 examples (#1362)
This commit is contained in:
parent
e0d59494c1
commit
ffb24cf312
|
@ -148,7 +148,6 @@
|
|||
<groupId>org.hsqldb</groupId>
|
||||
<artifactId>hsqldb</artifactId>
|
||||
<version>${hsqldb.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
|
|
@ -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,6 +13,7 @@ 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())
|
||||
|
|
|
@ -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.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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue