diff --git a/hibernate-immutable/README.md b/hibernate-immutable/README.md new file mode 100644 index 0000000000..f564d8fc3f --- /dev/null +++ b/hibernate-immutable/README.md @@ -0,0 +1,2 @@ +Article +- [Guide to @Immutable Annotation in Hibernate](http://inprogress.baeldung.com/?p=35824&preview=true) diff --git a/hibernate-immutable/pom.xml b/hibernate-immutable/pom.xml new file mode 100644 index 0000000000..f4ed2d3cf4 --- /dev/null +++ b/hibernate-immutable/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + + com.baeldung + hibernate-immutable + 1.0-SNAPSHOT + + + + org.hibernate + hibernate-core + 4.3.10.Final + + + org.hsqldb + hsqldb + 2.2.9 + + + junit + junit + 4.11 + test + + + + \ No newline at end of file diff --git a/hibernate-immutable/src/main/java/com/baeldung/entities/Event.java b/hibernate-immutable/src/main/java/com/baeldung/entities/Event.java new file mode 100644 index 0000000000..a8690a8959 --- /dev/null +++ b/hibernate-immutable/src/main/java/com/baeldung/entities/Event.java @@ -0,0 +1,52 @@ +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 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 getGuestList() { + return guestList; + } + + public void setGuestList(Set guestList) { + this.guestList = guestList; + } +} diff --git a/hibernate-immutable/src/main/java/com/baeldung/util/HibernateUtil.java b/hibernate-immutable/src/main/java/com/baeldung/util/HibernateUtil.java new file mode 100644 index 0000000000..f802342b86 --- /dev/null +++ b/hibernate-immutable/src/main/java/com/baeldung/util/HibernateUtil.java @@ -0,0 +1,29 @@ +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; + } +} diff --git a/hibernate-immutable/src/main/resources/hibernate.cfg.xml b/hibernate-immutable/src/main/resources/hibernate.cfg.xml new file mode 100644 index 0000000000..756191a9e2 --- /dev/null +++ b/hibernate-immutable/src/main/resources/hibernate.cfg.xml @@ -0,0 +1,38 @@ + + + + + + + + + org.hsqldb.jdbcDriver + jdbc:hsqldb:hsql://localhost/xdb + sa + + + + 1 + + + org.hibernate.dialect.HSQLDialect + + + thread + + + org.hibernate.cache.NoCacheProvider + + + true + + + update + + + + + + \ No newline at end of file diff --git a/hibernate-immutable/src/test/java/EventTest.java b/hibernate-immutable/src/test/java/EventTest.java new file mode 100644 index 0000000000..f05aaff5f5 --- /dev/null +++ b/hibernate-immutable/src/test/java/EventTest.java @@ -0,0 +1,78 @@ +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(); + } +}