From 9fa14697bfda4ea6c0a3744fddffe97547e0bbae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Walter=20G=C3=B3mez?= Date: Thu, 28 Sep 2017 05:22:40 -0600 Subject: [PATCH] Fix/hibernate immutable tests (#2682) * Add project for hibernate immutable article Add Event entity Add hibernate configuration file Add hibernateutil for configuration Add test to match snippets from article * Update master * Remove generated annotation to event class due to immutable annotation Update test to demonstrate silent error on update read-only entities * Add User entity with GeneratedValue annotation Add integration test to validate annotation behavior * Update method names * Update tests to validate Id * Change class name --- .../hibernate/immutable/entities/Event.java | 18 +++--- .../immutable/entities/EventGeneratedId.java | 55 ++++++++++++++++++ .../immutable/util/HibernateUtil.java | 2 + .../src/main/resources/hibernate5Config.xml | 2 +- .../main/resources/persistence-h2.properties | 2 +- .../resources/persistence-mysql.properties | 2 +- ...utableEventGeneratedIdIntegrationTest.java | 56 +++++++++++++++++++ .../HibernateImmutableIntegrationTest.java | 18 ++++-- 8 files changed, 137 insertions(+), 18 deletions(-) create mode 100644 spring-hibernate5/src/main/java/com/baeldung/hibernate/immutable/entities/EventGeneratedId.java create mode 100644 spring-hibernate5/src/test/java/com/baeldung/hibernate/immutable/HibernateImmutableEventGeneratedIdIntegrationTest.java diff --git a/spring-hibernate5/src/main/java/com/baeldung/hibernate/immutable/entities/Event.java b/spring-hibernate5/src/main/java/com/baeldung/hibernate/immutable/entities/Event.java index 2928ffe981..ec88d629a6 100644 --- a/spring-hibernate5/src/main/java/com/baeldung/hibernate/immutable/entities/Event.java +++ b/spring-hibernate5/src/main/java/com/baeldung/hibernate/immutable/entities/Event.java @@ -2,15 +2,9 @@ package com.baeldung.hibernate.immutable.entities; import org.hibernate.annotations.Cascade; import org.hibernate.annotations.CascadeType; -import org.hibernate.annotations.GenericGenerator; import org.hibernate.annotations.Immutable; -import javax.persistence.Column; -import javax.persistence.ElementCollection; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.Table; +import javax.persistence.*; import java.util.Set; @Entity @@ -20,8 +14,6 @@ public class Event { @Id @Column(name = "event_id") - @GeneratedValue(generator = "increment") - @GenericGenerator(name = "increment", strategy = "increment") private Long id; @Column(name = "title") @@ -31,6 +23,14 @@ public class Event { @Immutable private Set guestList; + public Event() {} + + public Event(Long id, String title, Set guestList) { + this.id = id; + this.title = title; + this.guestList = guestList; + } + public Long getId() { return id; } diff --git a/spring-hibernate5/src/main/java/com/baeldung/hibernate/immutable/entities/EventGeneratedId.java b/spring-hibernate5/src/main/java/com/baeldung/hibernate/immutable/entities/EventGeneratedId.java new file mode 100644 index 0000000000..33af9313ae --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/hibernate/immutable/entities/EventGeneratedId.java @@ -0,0 +1,55 @@ +package com.baeldung.hibernate.immutable.entities; + +import org.hibernate.annotations.GenericGenerator; +import org.hibernate.annotations.Immutable; + +import javax.persistence.*; + +@Entity +@Immutable +@Table(name = "events_generated") +public class EventGeneratedId { + + @Id + @Column(name = "event_generated_id") + @GeneratedValue(generator = "increment") + @GenericGenerator(name = "increment", strategy = "increment") + private Long id; + + @Column(name = "name") + private String name; + @Column(name = "description") + private String description; + + public EventGeneratedId() { + } + + public EventGeneratedId(String name, String description) { + this.name = name; + this.description = description; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } +} \ No newline at end of file diff --git a/spring-hibernate5/src/main/java/com/baeldung/hibernate/immutable/util/HibernateUtil.java b/spring-hibernate5/src/main/java/com/baeldung/hibernate/immutable/util/HibernateUtil.java index e4a2319c37..722f0251d1 100644 --- a/spring-hibernate5/src/main/java/com/baeldung/hibernate/immutable/util/HibernateUtil.java +++ b/spring-hibernate5/src/main/java/com/baeldung/hibernate/immutable/util/HibernateUtil.java @@ -1,6 +1,7 @@ package com.baeldung.hibernate.immutable.util; import com.baeldung.hibernate.immutable.entities.Event; +import com.baeldung.hibernate.immutable.entities.EventGeneratedId; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; @@ -14,6 +15,7 @@ public class HibernateUtil { // Create a session factory from immutable.cfg.xml Configuration configuration = new Configuration(); configuration.addAnnotatedClass(Event.class); + configuration.addAnnotatedClass(EventGeneratedId.class); configuration.configure("immutable.cfg.xml"); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); return configuration.buildSessionFactory(serviceRegistry); diff --git a/spring-hibernate5/src/main/resources/hibernate5Config.xml b/spring-hibernate5/src/main/resources/hibernate5Config.xml index 55546a862a..bbb61cb3e0 100644 --- a/spring-hibernate5/src/main/resources/hibernate5Config.xml +++ b/spring-hibernate5/src/main/resources/hibernate5Config.xml @@ -21,7 +21,7 @@ - + diff --git a/spring-hibernate5/src/main/resources/persistence-h2.properties b/spring-hibernate5/src/main/resources/persistence-h2.properties index 537626bc2a..696e805cff 100644 --- a/spring-hibernate5/src/main/resources/persistence-h2.properties +++ b/spring-hibernate5/src/main/resources/persistence-h2.properties @@ -1,7 +1,7 @@ # jdbc.X jdbc.driverClassName=org.h2.Driver jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1 -jdbc.user=sa +jdbc.eventGeneratedId=sa jdbc.pass=sa # hibernate.X diff --git a/spring-hibernate5/src/main/resources/persistence-mysql.properties b/spring-hibernate5/src/main/resources/persistence-mysql.properties index 1180929b30..b3cfd31f46 100644 --- a/spring-hibernate5/src/main/resources/persistence-mysql.properties +++ b/spring-hibernate5/src/main/resources/persistence-mysql.properties @@ -1,7 +1,7 @@ # jdbc.X jdbc.driverClassName=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/spring_hibernate5_01?createDatabaseIfNotExist=true -jdbc.user=tutorialuser +jdbc.eventGeneratedId=tutorialuser jdbc.pass=tutorialmy5ql # hibernate.X diff --git a/spring-hibernate5/src/test/java/com/baeldung/hibernate/immutable/HibernateImmutableEventGeneratedIdIntegrationTest.java b/spring-hibernate5/src/test/java/com/baeldung/hibernate/immutable/HibernateImmutableEventGeneratedIdIntegrationTest.java new file mode 100644 index 0000000000..a0bd9fc950 --- /dev/null +++ b/spring-hibernate5/src/test/java/com/baeldung/hibernate/immutable/HibernateImmutableEventGeneratedIdIntegrationTest.java @@ -0,0 +1,56 @@ +package com.baeldung.hibernate.immutable; + +import com.baeldung.hibernate.immutable.entities.EventGeneratedId; +import com.baeldung.hibernate.immutable.util.HibernateUtil; +import org.hibernate.CacheMode; +import org.hibernate.Session; +import org.junit.*; +import org.junit.rules.ExpectedException; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.IsEqual.equalTo; + +public class HibernateImmutableEventGeneratedIdIntegrationTest { + + private static Session session; + + @Rule + public final ExpectedException exception = ExpectedException.none(); + + @Before + public void before() { + session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.beginTransaction(); + createEventGenerated(); + session.setCacheMode(CacheMode.REFRESH); + } + + @BeforeClass + public static void setup() { + session = HibernateUtil.getSessionFactory().getCurrentSession(); + } + + @AfterClass + public static void teardown() { + HibernateUtil.getSessionFactory().close(); + } + + @Test + public void updateEventGenerated() { + EventGeneratedId eventGeneratedId = (EventGeneratedId) session + .createQuery("FROM EventGeneratedId WHERE name LIKE '%John%'").list().get(0); + eventGeneratedId.setName("Mike"); + session.update(eventGeneratedId); + session.flush(); + session.refresh(eventGeneratedId); + + assertThat(eventGeneratedId.getName(), equalTo("John")); + assertThat(eventGeneratedId.getId(), equalTo(1L)); + } + + private static void createEventGenerated() { + EventGeneratedId eventGeneratedId = new EventGeneratedId("John", "Doe"); + eventGeneratedId.setId(4L); + session.save(eventGeneratedId); + } +} diff --git a/spring-hibernate5/src/test/java/com/baeldung/hibernate/immutable/HibernateImmutableIntegrationTest.java b/spring-hibernate5/src/test/java/com/baeldung/hibernate/immutable/HibernateImmutableIntegrationTest.java index 801ddcdb45..d6c238488f 100644 --- a/spring-hibernate5/src/test/java/com/baeldung/hibernate/immutable/HibernateImmutableIntegrationTest.java +++ b/spring-hibernate5/src/test/java/com/baeldung/hibernate/immutable/HibernateImmutableIntegrationTest.java @@ -10,6 +10,9 @@ import org.junit.rules.ExpectedException; import javax.persistence.PersistenceException; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.IsEqual.equalTo; + public class HibernateImmutableIntegrationTest { private static Session session; @@ -38,6 +41,7 @@ public class HibernateImmutableIntegrationTest { @Test public void addEvent() { Event event = new Event(); + event.setId(2L); event.setTitle("Public Event"); session.save(event); session.getTransaction().commit(); @@ -47,8 +51,12 @@ public class HibernateImmutableIntegrationTest { public void updateEvent() { Event event = (Event) session.createQuery("FROM Event WHERE title='New Event'").list().get(0); event.setTitle("Private Event"); - session.saveOrUpdate(event); - session.getTransaction().commit(); + session.update(event); + session.flush(); + session.refresh(event); + + assertThat(event.getTitle(), equalTo("New Event")); + assertThat(event.getId(), equalTo(5L)); } @Test @@ -80,10 +88,8 @@ public class HibernateImmutableIntegrationTest { session.getTransaction().commit(); } - public static void createEvent() { - Event event = new Event(); - event.setTitle("New Event"); - event.setGuestList(Sets.newHashSet("guest")); + private static void createEvent() { + Event event = new Event(5L, "New Event", Sets.newHashSet("guest")); session.save(event); } }