BAEL-4643: JPA CascadeType.REMOVE vs orphanRemoval

* Reformats code using the provided intelliJ-formatter.xml
This commit is contained in:
Emmanuel Yasa 2020-11-03 19:11:44 +08:00
parent 06c800c526
commit 3139be9468
6 changed files with 62 additions and 55 deletions

View File

@ -1,11 +1,11 @@
package com.baeldung.jpa.removal; package com.baeldung.jpa.removal;
import java.util.Objects;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType; import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import java.util.Objects;
@Entity @Entity
public class LineItem { public class LineItem {
@ -25,8 +25,10 @@ public class LineItem {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o)
if (o == null || getClass() != o.getClass()) return false; return true;
if (o == null || getClass() != o.getClass())
return false;
LineItem lineItem = (LineItem) o; LineItem lineItem = (LineItem) o;
@ -38,5 +40,6 @@ public class LineItem {
return id != null ? id.hashCode() : 0; return id != null ? id.hashCode() : 0;
} }
protected LineItem() {} protected LineItem() {
}
} }

View File

@ -1,6 +1,5 @@
package com.baeldung.jpa.removal; package com.baeldung.jpa.removal;
import java.util.List;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
@ -8,6 +7,7 @@ import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
import javax.persistence.OneToOne; import javax.persistence.OneToOne;
import java.util.List;
@Entity @Entity
public class OrderRequest { public class OrderRequest {
@ -34,5 +34,6 @@ public class OrderRequest {
lineItems.remove(lineItem); lineItems.remove(lineItem);
} }
protected OrderRequest() {} protected OrderRequest() {
}
} }

View File

@ -18,5 +18,6 @@ public class ShipmentInfo {
this.name = name; this.name = name;
} }
protected ShipmentInfo() {} protected ShipmentInfo() {
}
} }

View File

@ -1,43 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd" http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"
version="2.2"> version="2.2">
<persistence-unit name="jpa-h2-equality"> <persistence-unit name="jpa-h2-equality">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.baeldung.jpa.equality.EqualByJavaDefault</class> <class>com.baeldung.jpa.equality.EqualByJavaDefault</class>
<class>com.baeldung.jpa.equality.EqualById</class> <class>com.baeldung.jpa.equality.EqualById</class>
<class>com.baeldung.jpa.equality.EqualByBusinessKey</class> <class>com.baeldung.jpa.equality.EqualByBusinessKey</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes> <exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties> <properties>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" /> <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test" /> <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test"/>
<property name="javax.persistence.jdbc.user" value="sa" /> <property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value="" /> <property name="javax.persistence.jdbc.password" value=""/>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" /> <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" /> <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop" /> <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="show_sql" value="false" /> <property name="show_sql" value="false"/>
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false" /> <property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
</properties> </properties>
</persistence-unit> </persistence-unit>
<persistence-unit name="jpa-h2-removal"> <persistence-unit name="jpa-h2-removal">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.baeldung.jpa.removal.ShipmentInfo</class> <class>com.baeldung.jpa.removal.ShipmentInfo</class>
<class>com.baeldung.jpa.removal.LineItem</class> <class>com.baeldung.jpa.removal.LineItem</class>
<class>com.baeldung.jpa.removal.OrderRequest</class> <class>com.baeldung.jpa.removal.OrderRequest</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes> <exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties> <properties>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" /> <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test" /> <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test"/>
<property name="javax.persistence.jdbc.user" value="sa" /> <property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value="" /> <property name="javax.persistence.jdbc.password" value=""/>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" /> <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" /> <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop" /> <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="show_sql" value="false" /> <property name="show_sql" value="false"/>
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false" /> <property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
</properties> </properties>
</persistence-unit> </persistence-unit>
</persistence> </persistence>

View File

@ -1,6 +1,9 @@
package com.baeldung.jpa.removal; package com.baeldung.jpa.removal;
import java.util.List; import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence; import javax.persistence.Persistence;
@ -8,9 +11,7 @@ import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root; import javax.persistence.criteria.Root;
import org.junit.Assert; import java.util.List;
import org.junit.BeforeClass;
import org.junit.Test;
public class CascadeTypeRemoveIntegrationTest { public class CascadeTypeRemoveIntegrationTest {

View File

@ -1,7 +1,9 @@
package com.baeldung.jpa.removal; package com.baeldung.jpa.removal;
import java.util.ArrayList; import org.junit.Assert;
import java.util.List; import org.junit.BeforeClass;
import org.junit.Test;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence; import javax.persistence.Persistence;
@ -9,9 +11,8 @@ import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root; import javax.persistence.criteria.Root;
import org.junit.Assert; import java.util.ArrayList;
import org.junit.BeforeClass; import java.util.List;
import org.junit.Test;
public class OrphanRemovalIntegrationTest { public class OrphanRemovalIntegrationTest {