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:
parent
692fe31575
commit
9fa14697bf
|
@ -2,15 +2,9 @@ package com.baeldung.hibernate.immutable.entities;
|
||||||
|
|
||||||
import org.hibernate.annotations.Cascade;
|
import org.hibernate.annotations.Cascade;
|
||||||
import org.hibernate.annotations.CascadeType;
|
import org.hibernate.annotations.CascadeType;
|
||||||
import org.hibernate.annotations.GenericGenerator;
|
|
||||||
import org.hibernate.annotations.Immutable;
|
import org.hibernate.annotations.Immutable;
|
||||||
|
|
||||||
import javax.persistence.Column;
|
import javax.persistence.*;
|
||||||
import javax.persistence.ElementCollection;
|
|
||||||
import javax.persistence.Entity;
|
|
||||||
import javax.persistence.GeneratedValue;
|
|
||||||
import javax.persistence.Id;
|
|
||||||
import javax.persistence.Table;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
|
@ -20,8 +14,6 @@ public class Event {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@Column(name = "event_id")
|
@Column(name = "event_id")
|
||||||
@GeneratedValue(generator = "increment")
|
|
||||||
@GenericGenerator(name = "increment", strategy = "increment")
|
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
@Column(name = "title")
|
@Column(name = "title")
|
||||||
|
@ -31,6 +23,14 @@ public class Event {
|
||||||
@Immutable
|
@Immutable
|
||||||
private Set<String> guestList;
|
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() {
|
public Long getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
package com.baeldung.hibernate.immutable.util;
|
package com.baeldung.hibernate.immutable.util;
|
||||||
|
|
||||||
import com.baeldung.hibernate.immutable.entities.Event;
|
import com.baeldung.hibernate.immutable.entities.Event;
|
||||||
|
import com.baeldung.hibernate.immutable.entities.EventGeneratedId;
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
import org.hibernate.cfg.Configuration;
|
import org.hibernate.cfg.Configuration;
|
||||||
|
@ -14,6 +15,7 @@ public class HibernateUtil {
|
||||||
// Create a session factory from immutable.cfg.xml
|
// Create a session factory from immutable.cfg.xml
|
||||||
Configuration configuration = new Configuration();
|
Configuration configuration = new Configuration();
|
||||||
configuration.addAnnotatedClass(Event.class);
|
configuration.addAnnotatedClass(Event.class);
|
||||||
|
configuration.addAnnotatedClass(EventGeneratedId.class);
|
||||||
configuration.configure("immutable.cfg.xml");
|
configuration.configure("immutable.cfg.xml");
|
||||||
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
|
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
|
||||||
return configuration.buildSessionFactory(serviceRegistry);
|
return configuration.buildSessionFactory(serviceRegistry);
|
||||||
|
|
|
@ -21,7 +21,7 @@
|
||||||
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource">
|
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource">
|
||||||
<property name="driverClassName" value="${jdbc.driverClassName}"/>
|
<property name="driverClassName" value="${jdbc.driverClassName}"/>
|
||||||
<property name="url" value="${jdbc.url}"/>
|
<property name="url" value="${jdbc.url}"/>
|
||||||
<property name="username" value="${jdbc.user}"/>
|
<property name="username" value="${jdbc.eventGeneratedId}"/>
|
||||||
<property name="password" value="${jdbc.pass}"/>
|
<property name="password" value="${jdbc.pass}"/>
|
||||||
</bean>
|
</bean>
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
# jdbc.X
|
# jdbc.X
|
||||||
jdbc.driverClassName=org.h2.Driver
|
jdbc.driverClassName=org.h2.Driver
|
||||||
jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
|
jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
|
||||||
jdbc.user=sa
|
jdbc.eventGeneratedId=sa
|
||||||
jdbc.pass=sa
|
jdbc.pass=sa
|
||||||
|
|
||||||
# hibernate.X
|
# hibernate.X
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
# jdbc.X
|
# jdbc.X
|
||||||
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
|
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
|
||||||
jdbc.url=jdbc:mysql://localhost:3306/spring_hibernate5_01?createDatabaseIfNotExist=true
|
jdbc.url=jdbc:mysql://localhost:3306/spring_hibernate5_01?createDatabaseIfNotExist=true
|
||||||
jdbc.user=tutorialuser
|
jdbc.eventGeneratedId=tutorialuser
|
||||||
jdbc.pass=tutorialmy5ql
|
jdbc.pass=tutorialmy5ql
|
||||||
|
|
||||||
# hibernate.X
|
# hibernate.X
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,6 +10,9 @@ import org.junit.rules.ExpectedException;
|
||||||
|
|
||||||
import javax.persistence.PersistenceException;
|
import javax.persistence.PersistenceException;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.core.IsEqual.equalTo;
|
||||||
|
|
||||||
public class HibernateImmutableIntegrationTest {
|
public class HibernateImmutableIntegrationTest {
|
||||||
|
|
||||||
private static Session session;
|
private static Session session;
|
||||||
|
@ -38,6 +41,7 @@ public class HibernateImmutableIntegrationTest {
|
||||||
@Test
|
@Test
|
||||||
public void addEvent() {
|
public void addEvent() {
|
||||||
Event event = new Event();
|
Event event = new Event();
|
||||||
|
event.setId(2L);
|
||||||
event.setTitle("Public Event");
|
event.setTitle("Public Event");
|
||||||
session.save(event);
|
session.save(event);
|
||||||
session.getTransaction().commit();
|
session.getTransaction().commit();
|
||||||
|
@ -47,8 +51,12 @@ public class HibernateImmutableIntegrationTest {
|
||||||
public void updateEvent() {
|
public void updateEvent() {
|
||||||
Event event = (Event) session.createQuery("FROM Event WHERE title='New Event'").list().get(0);
|
Event event = (Event) session.createQuery("FROM Event WHERE title='New Event'").list().get(0);
|
||||||
event.setTitle("Private Event");
|
event.setTitle("Private Event");
|
||||||
session.saveOrUpdate(event);
|
session.update(event);
|
||||||
session.getTransaction().commit();
|
session.flush();
|
||||||
|
session.refresh(event);
|
||||||
|
|
||||||
|
assertThat(event.getTitle(), equalTo("New Event"));
|
||||||
|
assertThat(event.getId(), equalTo(5L));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -80,10 +88,8 @@ public class HibernateImmutableIntegrationTest {
|
||||||
session.getTransaction().commit();
|
session.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void createEvent() {
|
private static void createEvent() {
|
||||||
Event event = new Event();
|
Event event = new Event(5L, "New Event", Sets.newHashSet("guest"));
|
||||||
event.setTitle("New Event");
|
|
||||||
event.setGuestList(Sets.newHashSet("guest"));
|
|
||||||
session.save(event);
|
session.save(event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue