JAVA-10082 Create new module hibernate-annotations-2 from spring-hibernate-5 module
This commit is contained in:
@@ -10,5 +10,4 @@ This module contains articles about Hibernate 5 with Spring.
|
||||
- [Hibernate Second-Level Cache](http://www.baeldung.com/hibernate-second-level-cache)
|
||||
- [Deleting Objects with Hibernate](http://www.baeldung.com/delete-with-hibernate)
|
||||
- [Spring, Hibernate and a JNDI Datasource](http://www.baeldung.com/spring-persistence-jpa-jndi-datasource)
|
||||
- [@Immutable in Hibernate](http://www.baeldung.com/hibernate-immutable)
|
||||
- [Bootstrapping Hibernate 5 with Spring](https://www.baeldung.com/hibernate-5-spring)
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
package com.baeldung.hibernate.immutable.entities;
|
||||
|
||||
import org.hibernate.annotations.Cascade;
|
||||
import org.hibernate.annotations.CascadeType;
|
||||
import org.hibernate.annotations.Immutable;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.Set;
|
||||
|
||||
@Entity
|
||||
@Immutable
|
||||
@Table(name = "events")
|
||||
public class Event {
|
||||
|
||||
@Id
|
||||
@Column(name = "event_id")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "title")
|
||||
private String title;
|
||||
|
||||
@ElementCollection
|
||||
@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;
|
||||
}
|
||||
|
||||
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<String> getGuestList() {
|
||||
return guestList;
|
||||
}
|
||||
|
||||
public void setGuestList(Set<String> guestList) {
|
||||
this.guestList = guestList;
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
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,35 +0,0 @@
|
||||
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;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class HibernateUtil {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(HibernateUtil.class);
|
||||
private static final SessionFactory sessionFactory = buildSessionFactory();
|
||||
|
||||
private static SessionFactory buildSessionFactory() {
|
||||
try {
|
||||
// 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);
|
||||
} catch (Throwable ex) {
|
||||
LOGGER.debug("Initial SessionFactory creation failed.", ex);
|
||||
throw new ExceptionInInitializerError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static SessionFactory getSessionFactory() {
|
||||
return sessionFactory;
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<!DOCTYPE hibernate-configuration PUBLIC
|
||||
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
|
||||
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
||||
|
||||
<hibernate-configuration>
|
||||
|
||||
<session-factory>
|
||||
|
||||
<!-- Database connection settings -->
|
||||
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
|
||||
<property name="connection.url">jdbc:hsqldb:mem:test</property>
|
||||
<property name="connection.username">sa</property>
|
||||
<property name="connection.password"></property>
|
||||
|
||||
<!-- JDBC connection pool (use the built-in) -->
|
||||
<property name="connection.pool_size">1</property>
|
||||
|
||||
<!-- SQL dialect -->
|
||||
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
|
||||
|
||||
<!-- Enable Hibernate's automatic session context management -->
|
||||
<property name="current_session_context_class">thread</property>
|
||||
|
||||
<!-- Echo all executed SQL to stdout -->
|
||||
<property name="show_sql">false</property>
|
||||
|
||||
<!-- Drop and re-create the database schema on startup -->
|
||||
<property name="hbm2ddl.auto">update</property>
|
||||
</session-factory>
|
||||
|
||||
</hibernate-configuration>
|
||||
@@ -1,124 +0,0 @@
|
||||
package com.baeldung.hibernate.immutable;
|
||||
|
||||
import com.baeldung.hibernate.immutable.entities.Event;
|
||||
import com.baeldung.hibernate.immutable.entities.EventGeneratedId;
|
||||
import com.baeldung.hibernate.immutable.util.HibernateUtil;
|
||||
import com.google.common.collect.Sets;
|
||||
import org.hibernate.Session;
|
||||
import org.junit.*;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
|
||||
/**
|
||||
* Configured in: immutable.cfg.xml
|
||||
*/
|
||||
public class HibernateImmutableIntegrationTest {
|
||||
|
||||
private static Session session;
|
||||
|
||||
@Rule
|
||||
public final ExpectedException exception = ExpectedException.none();
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
session = HibernateUtil.getSessionFactory().openSession();
|
||||
session.beginTransaction();
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() {
|
||||
session = HibernateUtil.getSessionFactory().getCurrentSession();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void teardown() {
|
||||
HibernateUtil.getSessionFactory().close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addEvent() {
|
||||
Event event = new Event();
|
||||
event.setId(2L);
|
||||
event.setTitle("Public Event");
|
||||
session.save(event);
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateEvent() {
|
||||
createEvent();
|
||||
Event event = (Event) session.createQuery("FROM Event WHERE title='New Event'").list().get(0);
|
||||
event.setTitle("Private Event");
|
||||
session.update(event);
|
||||
session.flush();
|
||||
session.refresh(event);
|
||||
session.close();
|
||||
|
||||
assertThat(event.getTitle(), equalTo("New Event"));
|
||||
assertThat(event.getId(), equalTo(5L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteEvent() {
|
||||
createEvent();
|
||||
Event event = (Event) session.createQuery("FROM Event WHERE title='New Event'").list().get(0);
|
||||
session.delete(event);
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addGuest() {
|
||||
createEvent();
|
||||
Event event = (Event) session.createQuery("FROM Event WHERE title='New Event'").list().get(0);
|
||||
String newGuest = "Sara";
|
||||
event.getGuestList().add(newGuest);
|
||||
|
||||
exception.expect(PersistenceException.class);
|
||||
session.save(event);
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteCascade() {
|
||||
Event event = (Event) session.createQuery("FROM Event WHERE title='New Event'").list().get(0);
|
||||
String guest = event.getGuestList().iterator().next();
|
||||
event.getGuestList().remove(guest);
|
||||
|
||||
exception.expect(PersistenceException.class);
|
||||
session.saveOrUpdate(event);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateEventGenerated() {
|
||||
createEventGenerated();
|
||||
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);
|
||||
session.close();
|
||||
|
||||
assertThat(eventGeneratedId.getName(), equalTo("John"));
|
||||
assertThat(eventGeneratedId.getId(), equalTo(1L));
|
||||
}
|
||||
|
||||
private static void createEvent() {
|
||||
Event event = new Event(5L, "New Event", Sets.newHashSet("guest"));
|
||||
session.save(event);
|
||||
}
|
||||
|
||||
private static void createEventGenerated() {
|
||||
EventGeneratedId eventGeneratedId = new EventGeneratedId("John", "Doe");
|
||||
session.save(eventGeneratedId);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user