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
This commit is contained in:
Walter Gómez 2017-09-28 05:22:40 -06:00 committed by Eugen
parent 692fe31575
commit 9fa14697bf
8 changed files with 137 additions and 18 deletions

View File

@ -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<String> guestList;
public Event() {}
public Event(Long id, String title, Set<String> guestList) {
this.id = id;
this.title = title;
this.guestList = guestList;
}
public Long getId() {
return id;
}

View File

@ -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;
}
}

View File

@ -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);

View File

@ -21,7 +21,7 @@
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="username" value="${jdbc.eventGeneratedId}"/>
<property name="password" value="${jdbc.pass}"/>
</bean>

View File

@ -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

View File

@ -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

View File

@ -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);
}
}

View File

@ -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);
}
}