This reverts commit 6fbf90fd27
.
This commit is contained in:
parent
a7bafc0775
commit
656afd6ff5
|
@ -1,2 +0,0 @@
|
||||||
Article
|
|
||||||
- [Guide to @Immutable Annotation in Hibernate](http://inprogress.baeldung.com/?p=35824&preview=true)
|
|
|
@ -1,30 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
|
|
||||||
<groupId>com.baeldung</groupId>
|
|
||||||
<artifactId>hibernate-immutable</artifactId>
|
|
||||||
<version>1.0-SNAPSHOT</version>
|
|
||||||
|
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.hibernate</groupId>
|
|
||||||
<artifactId>hibernate-core</artifactId>
|
|
||||||
<version>4.3.10.Final</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.hsqldb</groupId>
|
|
||||||
<artifactId>hsqldb</artifactId>
|
|
||||||
<version>2.2.9</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>junit</groupId>
|
|
||||||
<artifactId>junit</artifactId>
|
|
||||||
<version>4.11</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
|
|
||||||
</project>
|
|
|
@ -1,52 +0,0 @@
|
||||||
package com.baeldung.entities;
|
|
||||||
|
|
||||||
import org.hibernate.annotations.*;
|
|
||||||
import org.hibernate.annotations.CascadeType;
|
|
||||||
|
|
||||||
import javax.persistence.*;
|
|
||||||
import javax.persistence.Entity;
|
|
||||||
import javax.persistence.Table;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
@Entity
|
|
||||||
@Immutable
|
|
||||||
@Table(name = "events")
|
|
||||||
public class Event {
|
|
||||||
@Id
|
|
||||||
@Column(name = "event_id")
|
|
||||||
@GeneratedValue(generator = "increment")
|
|
||||||
@GenericGenerator(name = "increment", strategy = "increment")
|
|
||||||
private Long id;
|
|
||||||
|
|
||||||
@Column(name = "title")
|
|
||||||
private String title;
|
|
||||||
|
|
||||||
@ElementCollection
|
|
||||||
@Immutable
|
|
||||||
private Set<String> guestList;
|
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setId(Long id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getTitle() {
|
|
||||||
return title;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTitle(String title) {
|
|
||||||
this.title = title;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
|
|
||||||
public Set<String> getGuestList() {
|
|
||||||
return guestList;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setGuestList(Set<String> guestList) {
|
|
||||||
this.guestList = guestList;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,29 +0,0 @@
|
||||||
package com.baeldung.util;
|
|
||||||
|
|
||||||
import org.hibernate.SessionFactory;
|
|
||||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
|
||||||
import org.hibernate.cfg.Configuration;
|
|
||||||
import org.hibernate.service.ServiceRegistry;
|
|
||||||
|
|
||||||
public class HibernateUtil {
|
|
||||||
private static final SessionFactory sessionFactory = buildSessionFactory();
|
|
||||||
|
|
||||||
private static SessionFactory buildSessionFactory() {
|
|
||||||
try {
|
|
||||||
// Create a session factory from hibernate.cfg.xml
|
|
||||||
Configuration configuration = new Configuration();
|
|
||||||
configuration.configure("hibernate.cfg.xml");
|
|
||||||
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
|
|
||||||
.applySettings(configuration.getProperties())
|
|
||||||
.build();
|
|
||||||
return configuration.buildSessionFactory(serviceRegistry);
|
|
||||||
} catch (Throwable ex) {
|
|
||||||
System.out.println("Initial SessionFactory creation failed." + ex);
|
|
||||||
throw new ExceptionInInitializerError(ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static SessionFactory getSessionFactory() {
|
|
||||||
return sessionFactory;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,38 +0,0 @@
|
||||||
<?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:hsql://localhost/xdb</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>
|
|
||||||
|
|
||||||
<!-- Disable the second-level cache -->
|
|
||||||
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</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>
|
|
||||||
|
|
||||||
<mapping class="com.baeldung.entities.Event"/>
|
|
||||||
|
|
||||||
</session-factory>
|
|
||||||
|
|
||||||
</hibernate-configuration>
|
|
|
@ -1,78 +0,0 @@
|
||||||
import com.baeldung.entities.Event;
|
|
||||||
import com.baeldung.util.HibernateUtil;
|
|
||||||
import org.hibernate.HibernateException;
|
|
||||||
import org.hibernate.Session;
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Rule;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.rules.ExpectedException;
|
|
||||||
|
|
||||||
import javax.persistence.Table;
|
|
||||||
|
|
||||||
public class EventTest {
|
|
||||||
|
|
||||||
private Session session;
|
|
||||||
@Rule
|
|
||||||
public final ExpectedException exception = ExpectedException.none();
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setup() {
|
|
||||||
session = HibernateUtil.getSessionFactory().getCurrentSession();
|
|
||||||
session.beginTransaction();
|
|
||||||
}
|
|
||||||
|
|
||||||
@After
|
|
||||||
public void teardown() {
|
|
||||||
HibernateUtil.getSessionFactory().close();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void addEvent() {
|
|
||||||
Event event = new Event();
|
|
||||||
event.setTitle("Public Event");
|
|
||||||
session.save(event);
|
|
||||||
session.getTransaction().commit();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void updateEvent() {
|
|
||||||
Event event = (Event) session.createQuery(
|
|
||||||
"FROM Event WHERE title='My Event'").list().get(0);
|
|
||||||
event.setTitle("Public Event");
|
|
||||||
session.saveOrUpdate(event);
|
|
||||||
session.getTransaction().commit();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void deleteEvent() {
|
|
||||||
Event event = (Event) session.createQuery(
|
|
||||||
"FROM Event WHERE title='My Event'").list().get(0);
|
|
||||||
session.delete(event);
|
|
||||||
session.getTransaction().commit();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void addGuest() {
|
|
||||||
Event event = (Event) session.createQuery(
|
|
||||||
"FROM Event WHERE title='Public Event'").list().get(0);
|
|
||||||
String newGuest = "Sara";
|
|
||||||
event.getGuestList().add(newGuest);
|
|
||||||
|
|
||||||
exception.expect(HibernateException.class);
|
|
||||||
session.save(event);
|
|
||||||
session.getTransaction().commit();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void deleteCascade() {
|
|
||||||
Event event = (Event) session.createQuery(
|
|
||||||
"FROM Event WHERE title='Public Event'").list().get(0);
|
|
||||||
String guest = event.getGuestList().iterator().next();
|
|
||||||
event.getGuestList().remove(guest);
|
|
||||||
|
|
||||||
exception.expect(HibernateException.class);
|
|
||||||
session.saveOrUpdate(event);
|
|
||||||
session.getTransaction().commit();
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue