cascade type module (#7021)
* Added cascading type mudule * fix compile error * updated dependency version in pom
This commit is contained in:
parent
a14dc0d889
commit
f2c6eb9c10
|
@ -0,0 +1,71 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<relativePath>../../</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>jpa-hibernate-cascade-type</artifactId>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hibernate</groupId>
|
||||||
|
<artifactId>hibernate-core</artifactId>
|
||||||
|
<version>${hibernate.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>${assertj-core.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.h2database</groupId>
|
||||||
|
<artifactId>h2</artifactId>
|
||||||
|
<version>${h2.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- validation -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hibernate</groupId>
|
||||||
|
<artifactId>hibernate-validator</artifactId>
|
||||||
|
<version>${hibernate-validator.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.el</groupId>
|
||||||
|
<artifactId>javax.el-api</artifactId>
|
||||||
|
<version>${javax.el-api.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.glassfish</groupId>
|
||||||
|
<artifactId>javax.el</artifactId>
|
||||||
|
<version>${org.glassfish.javax.el.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>jpa-hibernate-cascade-type</finalName>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/test/resources</directory>
|
||||||
|
<filtering>true</filtering>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<hibernate.version>5.4.3.Final</hibernate.version>
|
||||||
|
<assertj-core.version>3.12.2</assertj-core.version>
|
||||||
|
<hibernate-validator.version>6.0.17.Final</hibernate-validator.version>
|
||||||
|
<javax.el-api.version>3.0.0</javax.el-api.version>
|
||||||
|
<org.glassfish.javax.el.version>3.0.1-b11</org.glassfish.javax.el.version>
|
||||||
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,45 @@
|
||||||
|
package com.baeldung.cascading;
|
||||||
|
|
||||||
|
import com.baeldung.cascading.domain.Address;
|
||||||
|
import com.baeldung.cascading.domain.Person;
|
||||||
|
import org.hibernate.SessionFactory;
|
||||||
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
|
import org.hibernate.cfg.Configuration;
|
||||||
|
import org.hibernate.service.ServiceRegistry;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
public class HibernateUtil {
|
||||||
|
private static SessionFactory sessionFactory;
|
||||||
|
|
||||||
|
public static SessionFactory getSessionFactory() {
|
||||||
|
try {
|
||||||
|
Properties properties = getProperties();
|
||||||
|
Configuration configuration = new Configuration();
|
||||||
|
configuration.setProperties(properties);
|
||||||
|
configuration.addAnnotatedClass(Person.class);
|
||||||
|
configuration.addAnnotatedClass(Address.class);
|
||||||
|
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
|
||||||
|
.applySettings(configuration.getProperties()).build();
|
||||||
|
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
|
||||||
|
return sessionFactory;
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return sessionFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Properties getProperties() throws IOException {
|
||||||
|
Properties properties = new Properties();
|
||||||
|
URL propertiesURL = Thread.currentThread()
|
||||||
|
.getContextClassLoader()
|
||||||
|
.getResource("hibernate.properties");
|
||||||
|
try (FileInputStream inputStream = new FileInputStream(propertiesURL.getFile())) {
|
||||||
|
properties.load(inputStream);
|
||||||
|
}
|
||||||
|
return properties;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,64 @@
|
||||||
|
package com.baeldung.cascading.domain;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Address {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private int id;
|
||||||
|
private String street;
|
||||||
|
private int houseNumber;
|
||||||
|
private String city;
|
||||||
|
private int zipCode;
|
||||||
|
@ManyToOne(fetch = FetchType.LAZY)
|
||||||
|
private Person person;
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Person getPerson() {
|
||||||
|
return person;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPerson(Person person) {
|
||||||
|
this.person = person;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getStreet() {
|
||||||
|
return street;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getHouseNumber() {
|
||||||
|
return houseNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHouseNumber(int houseNumber) {
|
||||||
|
this.houseNumber = houseNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCity() {
|
||||||
|
return city;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCity(String city) {
|
||||||
|
this.city = city;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getZipCode() {
|
||||||
|
return zipCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setZipCode(int zipCode) {
|
||||||
|
this.zipCode = zipCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStreet(String street) {
|
||||||
|
this.street = street;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
package com.baeldung.cascading.domain;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Person {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private int id;
|
||||||
|
private String name;
|
||||||
|
@OneToMany(mappedBy = "person", cascade = CascadeType.ALL)
|
||||||
|
private List<Address> addresses;
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Address> getAddresses() {
|
||||||
|
return addresses;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAddresses(List<Address> addresses) {
|
||||||
|
this.addresses = addresses;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,169 @@
|
||||||
|
package com.baeldung.cascading;
|
||||||
|
|
||||||
|
import com.baeldung.cascading.domain.Address;
|
||||||
|
import com.baeldung.cascading.domain.Person;
|
||||||
|
import org.assertj.core.api.Assertions;
|
||||||
|
import org.hibernate.*;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class CasCadeTypeUnitTest {
|
||||||
|
private static SessionFactory sessionFactory;
|
||||||
|
private Session session;
|
||||||
|
private Transaction transaction;
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void beforeTests() {
|
||||||
|
sessionFactory = HibernateUtil.getSessionFactory();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
session = sessionFactory.openSession();
|
||||||
|
transaction = session.beginTransaction();
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
transaction.rollback();
|
||||||
|
session.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPersist() {
|
||||||
|
Person person = new Person();
|
||||||
|
Address address = new Address();
|
||||||
|
address.setPerson(person);
|
||||||
|
person.setAddresses(Arrays.asList(address));
|
||||||
|
session.persist(person);
|
||||||
|
session.flush();
|
||||||
|
session.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMerge() {
|
||||||
|
int addressId;
|
||||||
|
Person person = buildPerson("devender");
|
||||||
|
Address address = buildAddress(person);
|
||||||
|
person.setAddresses(Arrays.asList(address));
|
||||||
|
session.persist(person);
|
||||||
|
session.flush();
|
||||||
|
addressId = address.getId();
|
||||||
|
session.clear();
|
||||||
|
|
||||||
|
Address savedAddressEntity = session.find(Address.class, addressId);
|
||||||
|
Person savedPersonEntity = savedAddressEntity.getPerson();
|
||||||
|
savedPersonEntity.setName("devender kumar");
|
||||||
|
savedAddressEntity.setHouseNumber(24);
|
||||||
|
session.merge(savedPersonEntity);
|
||||||
|
session.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRemove() {
|
||||||
|
int personId;
|
||||||
|
Person person = buildPerson("devender");
|
||||||
|
Address address = buildAddress(person);
|
||||||
|
person.setAddresses(Arrays.asList(address));
|
||||||
|
session.persist(person);
|
||||||
|
session.flush();
|
||||||
|
personId = person.getId();
|
||||||
|
session.clear();
|
||||||
|
|
||||||
|
Person savedPersonEntity = session.find(Person.class, personId);
|
||||||
|
session.remove(savedPersonEntity);
|
||||||
|
session.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDetach() {
|
||||||
|
Person person = buildPerson("devender");
|
||||||
|
Address address = buildAddress(person);
|
||||||
|
person.setAddresses(Arrays.asList(address));
|
||||||
|
session.persist(person);
|
||||||
|
session.flush();
|
||||||
|
Assertions.assertThat(session.contains(person)).isTrue();
|
||||||
|
Assertions.assertThat(session.contains(address)).isTrue();
|
||||||
|
|
||||||
|
session.detach(person);
|
||||||
|
Assertions.assertThat(session.contains(person)).isFalse();
|
||||||
|
Assertions.assertThat(session.contains(address)).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLock() {
|
||||||
|
Person person = buildPerson("devender");
|
||||||
|
Address address = buildAddress(person);
|
||||||
|
person.setAddresses(Arrays.asList(address));
|
||||||
|
session.persist(person);
|
||||||
|
session.flush();
|
||||||
|
Assertions.assertThat(session.contains(person)).isTrue();
|
||||||
|
Assertions.assertThat(session.contains(address)).isTrue();
|
||||||
|
|
||||||
|
session.detach(person);
|
||||||
|
Assertions.assertThat(session.contains(person)).isFalse();
|
||||||
|
Assertions.assertThat(session.contains(address)).isFalse();
|
||||||
|
session.unwrap(Session.class)
|
||||||
|
.buildLockRequest(new LockOptions(LockMode.NONE))
|
||||||
|
.lock(person);
|
||||||
|
|
||||||
|
Assertions.assertThat(session.contains(person)).isTrue();
|
||||||
|
Assertions.assertThat(session.contains(address)).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRefresh() {
|
||||||
|
Person person = buildPerson("devender");
|
||||||
|
Address address = buildAddress(person);
|
||||||
|
person.setAddresses(Arrays.asList(address));
|
||||||
|
session.persist(person);
|
||||||
|
session.flush();
|
||||||
|
person.setName("Devender Kumar");
|
||||||
|
address.setHouseNumber(24);
|
||||||
|
session.refresh(person);
|
||||||
|
Assertions.assertThat(person.getName()).isEqualTo("devender");
|
||||||
|
Assertions.assertThat(address.getHouseNumber()).isEqualTo(23);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReplicate() {
|
||||||
|
Person person = buildPerson("devender");
|
||||||
|
person.setId(2);
|
||||||
|
Address address = buildAddress(person);
|
||||||
|
address.setId(2);
|
||||||
|
person.setAddresses(Arrays.asList(address));
|
||||||
|
session.unwrap(Session.class).replicate(person, ReplicationMode.OVERWRITE);
|
||||||
|
session.flush();
|
||||||
|
Assertions.assertThat(person.getId()).isEqualTo(2);
|
||||||
|
Assertions.assertThat(address.getId()).isEqualTo(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSaveOrUpdate() {
|
||||||
|
Person person = buildPerson("devender");
|
||||||
|
Address address = buildAddress(person);
|
||||||
|
person.setAddresses(Arrays.asList(address));
|
||||||
|
session.saveOrUpdate(person);
|
||||||
|
session.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Address buildAddress(Person person) {
|
||||||
|
Address address = new Address();
|
||||||
|
address.setCity("Berlin");
|
||||||
|
address.setHouseNumber(23);
|
||||||
|
address.setStreet("Zeughofstraße");
|
||||||
|
address.setZipCode(123001);
|
||||||
|
address.setPerson(person);
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Person buildPerson(String name) {
|
||||||
|
Person person = new Person();
|
||||||
|
person.setName(name);
|
||||||
|
return person;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
hibernate.connection.driver_class=org.h2.Driver
|
||||||
|
hibernate.connection.url=jdbc:h2:mem:mydb1;DB_CLOSE_DELAY=-1
|
||||||
|
hibernate.connection.username=sa
|
||||||
|
hibernate.connection.autocommit=true
|
||||||
|
jdbc.password=
|
||||||
|
|
||||||
|
hibernate.dialect=org.hibernate.dialect.H2Dialect
|
||||||
|
hibernate.show_sql=true
|
||||||
|
hibernate.hbm2ddl.auto=create-drop
|
||||||
|
|
|
@ -57,5 +57,6 @@
|
||||||
<module>spring-hibernate4</module>
|
<module>spring-hibernate4</module>
|
||||||
<module>spring-jpa</module>
|
<module>spring-jpa</module>
|
||||||
<module>spring-persistence-simple</module>
|
<module>spring-persistence-simple</module>
|
||||||
|
<module>jpa-hibernate-cascade-type</module>
|
||||||
</modules>
|
</modules>
|
||||||
</project>
|
</project>
|
||||||
|
|
Loading…
Reference in New Issue