diff --git a/persistence-modules/jpa-hibernate-cascade-type/pom.xml b/persistence-modules/jpa-hibernate-cascade-type/pom.xml new file mode 100644 index 0000000000..8cfc2a5fa2 --- /dev/null +++ b/persistence-modules/jpa-hibernate-cascade-type/pom.xml @@ -0,0 +1,71 @@ + + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../ + + + jpa-hibernate-cascade-type + 4.0.0 + 1.0.0-SNAPSHOT + + + + org.hibernate + hibernate-core + ${hibernate.version} + + + org.assertj + assertj-core + ${assertj-core.version} + test + + + com.h2database + h2 + ${h2.version} + + + + org.hibernate + hibernate-validator + ${hibernate-validator.version} + + + javax.el + javax.el-api + ${javax.el-api.version} + + + org.glassfish + javax.el + ${org.glassfish.javax.el.version} + + + + + jpa-hibernate-cascade-type + + + src/test/resources + true + + + + + + 5.4.3.Final + 3.12.2 + 6.0.17.Final + 3.0.0 + 3.0.1-b11 + 1.8 + 1.8 + + + \ No newline at end of file diff --git a/persistence-modules/jpa-hibernate-cascade-type/src/main/java/com/baeldung/cascading/HibernateUtil.java b/persistence-modules/jpa-hibernate-cascade-type/src/main/java/com/baeldung/cascading/HibernateUtil.java new file mode 100644 index 0000000000..700c289e38 --- /dev/null +++ b/persistence-modules/jpa-hibernate-cascade-type/src/main/java/com/baeldung/cascading/HibernateUtil.java @@ -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; + } +} diff --git a/persistence-modules/jpa-hibernate-cascade-type/src/main/java/com/baeldung/cascading/domain/Address.java b/persistence-modules/jpa-hibernate-cascade-type/src/main/java/com/baeldung/cascading/domain/Address.java new file mode 100644 index 0000000000..a16b0f81b6 --- /dev/null +++ b/persistence-modules/jpa-hibernate-cascade-type/src/main/java/com/baeldung/cascading/domain/Address.java @@ -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; + } +} diff --git a/persistence-modules/jpa-hibernate-cascade-type/src/main/java/com/baeldung/cascading/domain/Person.java b/persistence-modules/jpa-hibernate-cascade-type/src/main/java/com/baeldung/cascading/domain/Person.java new file mode 100644 index 0000000000..9ca61323b3 --- /dev/null +++ b/persistence-modules/jpa-hibernate-cascade-type/src/main/java/com/baeldung/cascading/domain/Person.java @@ -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
addresses; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public List
getAddresses() { + return addresses; + } + + public void setAddresses(List
addresses) { + this.addresses = addresses; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/persistence-modules/jpa-hibernate-cascade-type/src/test/java/com/baeldung/cascading/CasCadeTypeUnitTest.java b/persistence-modules/jpa-hibernate-cascade-type/src/test/java/com/baeldung/cascading/CasCadeTypeUnitTest.java new file mode 100644 index 0000000000..a196bdac12 --- /dev/null +++ b/persistence-modules/jpa-hibernate-cascade-type/src/test/java/com/baeldung/cascading/CasCadeTypeUnitTest.java @@ -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; + } +} diff --git a/persistence-modules/jpa-hibernate-cascade-type/src/test/resources/hibernate.properties b/persistence-modules/jpa-hibernate-cascade-type/src/test/resources/hibernate.properties new file mode 100644 index 0000000000..c22da2496b --- /dev/null +++ b/persistence-modules/jpa-hibernate-cascade-type/src/test/resources/hibernate.properties @@ -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 + diff --git a/persistence-modules/pom.xml b/persistence-modules/pom.xml index e6d883334f..390bcc9d51 100644 --- a/persistence-modules/pom.xml +++ b/persistence-modules/pom.xml @@ -57,5 +57,6 @@ spring-hibernate4 spring-jpa spring-persistence-simple + jpa-hibernate-cascade-type