Skip to content

Commit 6fbf90f

Browse files
Walter Gómezpivovarit
authored andcommitted
[BAEL-694] Hibernate immutable annotation (eugenp#1302)
* Add project for hibernate immutable article Add Event entity Add hibernate configuration file Add hibernateutil for configuration Add test to match snippets from article * Create README.md * Update README.md
1 parent 623b2b9 commit 6fbf90f

6 files changed

Lines changed: 229 additions & 0 deletions

File tree

‎hibernate-immutable/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Article
2+
- [Guide to @Immutable Annotation in Hibernate](http://inprogress.baeldung.com/?p=35824&preview=true)

‎hibernate-immutable/pom.xml‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>hibernate-immutable</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>org.hibernate</groupId>
14+
<artifactId>hibernate-core</artifactId>
15+
<version>4.3.10.Final</version>
16+
</dependency>
17+
<dependency>
18+
<groupId>org.hsqldb</groupId>
19+
<artifactId>hsqldb</artifactId>
20+
<version>2.2.9</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>junit</groupId>
24+
<artifactId>junit</artifactId>
25+
<version>4.11</version>
26+
<scope>test</scope>
27+
</dependency>
28+
</dependencies>
29+
30+
</project>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.entities;
2+
3+
import org.hibernate.annotations.*;
4+
import org.hibernate.annotations.CascadeType;
5+
6+
import javax.persistence.*;
7+
import javax.persistence.Entity;
8+
import javax.persistence.Table;
9+
import java.util.Set;
10+
11+
@Entity
12+
@Immutable
13+
@Table(name = "events")
14+
public class Event {
15+
@Id
16+
@Column(name = "event_id")
17+
@GeneratedValue(generator = "increment")
18+
@GenericGenerator(name = "increment", strategy = "increment")
19+
private Long id;
20+
21+
@Column(name = "title")
22+
private String title;
23+
24+
@ElementCollection
25+
@Immutable
26+
private Set<String> guestList;
27+
28+
public Long getId() {
29+
return id;
30+
}
31+
32+
public void setId(Long id) {
33+
this.id = id;
34+
}
35+
36+
public String getTitle() {
37+
return title;
38+
}
39+
40+
public void setTitle(String title) {
41+
this.title = title;
42+
}
43+
44+
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
45+
public Set<String> getGuestList() {
46+
return guestList;
47+
}
48+
49+
public void setGuestList(Set<String> guestList) {
50+
this.guestList = guestList;
51+
}
52+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.util;
2+
3+
import org.hibernate.SessionFactory;
4+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
5+
import org.hibernate.cfg.Configuration;
6+
import org.hibernate.service.ServiceRegistry;
7+
8+
public class HibernateUtil {
9+
private static final SessionFactory sessionFactory = buildSessionFactory();
10+
11+
private static SessionFactory buildSessionFactory() {
12+
try {
13+
// Create a session factory from hibernate.cfg.xml
14+
Configuration configuration = new Configuration();
15+
configuration.configure("hibernate.cfg.xml");
16+
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
17+
.applySettings(configuration.getProperties())
18+
.build();
19+
return configuration.buildSessionFactory(serviceRegistry);
20+
} catch (Throwable ex) {
21+
System.out.println("Initial SessionFactory creation failed." + ex);
22+
throw new ExceptionInInitializerError(ex);
23+
}
24+
}
25+
26+
public static SessionFactory getSessionFactory() {
27+
return sessionFactory;
28+
}
29+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version='1.0' encoding='utf-8'?>
2+
<!DOCTYPE hibernate-configuration PUBLIC
3+
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4+
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
5+
6+
<hibernate-configuration>
7+
8+
<session-factory>
9+
10+
<!-- Database connection settings -->
11+
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
12+
<property name="connection.url">jdbc:hsqldb:hsql://localhost/xdb</property>
13+
<property name="connection.username">sa</property>
14+
<property name="connection.password"></property>
15+
16+
<!-- JDBC connection pool (use the built-in) -->
17+
<property name="connection.pool_size">1</property>
18+
19+
<!-- SQL dialect -->
20+
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
21+
22+
<!-- Enable Hibernate's automatic session context management -->
23+
<property name="current_session_context_class">thread</property>
24+
25+
<!-- Disable the second-level cache -->
26+
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
27+
28+
<!-- Echo all executed SQL to stdout -->
29+
<property name="show_sql">true</property>
30+
31+
<!-- Drop and re-create the database schema on startup -->
32+
<property name="hbm2ddl.auto">update</property>
33+
34+
<mapping class="com.baeldung.entities.Event"/>
35+
36+
</session-factory>
37+
38+
</hibernate-configuration>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import com.baeldung.entities.Event;
2+
import com.baeldung.util.HibernateUtil;
3+
import org.hibernate.HibernateException;
4+
import org.hibernate.Session;
5+
import org.junit.After;
6+
import org.junit.Before;
7+
import org.junit.Rule;
8+
import org.junit.Test;
9+
import org.junit.rules.ExpectedException;
10+
11+
import javax.persistence.Table;
12+
13+
public class EventTest {
14+
15+
private Session session;
16+
@Rule
17+
public final ExpectedException exception = ExpectedException.none();
18+
19+
@Before
20+
public void setup() {
21+
session = HibernateUtil.getSessionFactory().getCurrentSession();
22+
session.beginTransaction();
23+
}
24+
25+
@After
26+
public void teardown() {
27+
HibernateUtil.getSessionFactory().close();
28+
}
29+
30+
@Test
31+
public void addEvent() {
32+
Event event = new Event();
33+
event.setTitle("Public Event");
34+
session.save(event);
35+
session.getTransaction().commit();
36+
}
37+
38+
@Test
39+
public void updateEvent() {
40+
Event event = (Event) session.createQuery(
41+
"FROM Event WHERE title='My Event'").list().get(0);
42+
event.setTitle("Public Event");
43+
session.saveOrUpdate(event);
44+
session.getTransaction().commit();
45+
}
46+
47+
@Test
48+
public void deleteEvent() {
49+
Event event = (Event) session.createQuery(
50+
"FROM Event WHERE title='My Event'").list().get(0);
51+
session.delete(event);
52+
session.getTransaction().commit();
53+
}
54+
55+
@Test
56+
public void addGuest() {
57+
Event event = (Event) session.createQuery(
58+
"FROM Event WHERE title='Public Event'").list().get(0);
59+
String newGuest = "Sara";
60+
event.getGuestList().add(newGuest);
61+
62+
exception.expect(HibernateException.class);
63+
session.save(event);
64+
session.getTransaction().commit();
65+
}
66+
67+
@Test
68+
public void deleteCascade() {
69+
Event event = (Event) session.createQuery(
70+
"FROM Event WHERE title='Public Event'").list().get(0);
71+
String guest = event.getGuestList().iterator().next();
72+
event.getGuestList().remove(guest);
73+
74+
exception.expect(HibernateException.class);
75+
session.saveOrUpdate(event);
76+
session.getTransaction().commit();
77+
}
78+
}

0 commit comments

Comments
 (0)