From 85732409ee6335aacb4b5db8b1fea7380d19fa90 Mon Sep 17 00:00:00 2001 From: Chris Cranford Date: Fri, 7 Apr 2017 12:33:37 -0400 Subject: [PATCH] HHH-9663 Added test cases. --- ...oOneEagerNonOptionalOrphanRemovalTest.java | 207 +++++++++++++++++ .../OneToOneEagerOrphanRemovalTest.java | 202 +++++++++++++++++ ...ToOneLazyNonOptionalOrphanRemovalTest.java | 208 ++++++++++++++++++ .../OneToOneLazyOrphanRemovalTest.java | 202 +++++++++++++++++ ...oOneEagerNonOptionalOrphanRemovalTest.java | 207 +++++++++++++++++ .../OneToOneEagerOrphanRemovalTest.java | 202 +++++++++++++++++ ...ToOneLazyNonOptionalOrphanRemovalTest.java | 208 ++++++++++++++++++ .../OneToOneLazyOrphanRemovalTest.java | 202 +++++++++++++++++ 8 files changed, 1638 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/jpa/orphan/one2one/OneToOneEagerNonOptionalOrphanRemovalTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/jpa/orphan/one2one/OneToOneEagerOrphanRemovalTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/jpa/orphan/one2one/OneToOneLazyNonOptionalOrphanRemovalTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/jpa/orphan/one2one/OneToOneLazyOrphanRemovalTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneEagerNonOptionalOrphanRemovalTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneEagerOrphanRemovalTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneLazyNonOptionalOrphanRemovalTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneLazyOrphanRemovalTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/jpa/orphan/one2one/OneToOneEagerNonOptionalOrphanRemovalTest.java b/hibernate-core/src/test/java/org/hibernate/test/jpa/orphan/one2one/OneToOneEagerNonOptionalOrphanRemovalTest.java new file mode 100644 index 0000000000..1a0c2d86b0 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/jpa/orphan/one2one/OneToOneEagerNonOptionalOrphanRemovalTest.java @@ -0,0 +1,207 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.jpa.orphan.one2one; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; +import org.junit.Test; + +import org.hibernate.testing.TestForIssue; + +import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +/** + * @author Chris Cranford + */ +@TestForIssue( jiraKey = "HHH-9663" ) +public class OneToOneEagerNonOptionalOrphanRemovalTest extends BaseEntityManagerFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Car.class, PaintColor.class, Engine.class }; + } + + @Test + public void testOneToOneLazyNonOptionalOrphanRemoval() { + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Initialize the data + doInJPA( this::entityManagerFactory, entityManager -> { + final PaintColor color = new PaintColor( 1, "Red" ); + final Engine engine1 = new Engine( 1, 275 ); + final Engine engine2 = new Engine( 2, 295 ); + final Car car = new Car( 1, engine1, color ); + + entityManager.persist( engine1 ); + entityManager.persist( engine2 ); + entityManager.persist( color ); + entityManager.persist( car ); + } ); + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Test orphan removal for unidirectional relationship + doInJPA( this::entityManagerFactory, entityManager -> { + final Car car = entityManager.find( Car.class, 1 ); + final Engine engine = entityManager.find( Engine.class, 2 ); + car.setEngine( engine ); + entityManager.merge( car ); + } ); + + doInJPA( this::entityManagerFactory, entityManager -> { + final Car car = entityManager.find( Car.class, 1 ); + assertNotNull( car.getEngine() ); + + final Engine engine = entityManager.find( Engine.class, 1 ); + assertNull( engine ); + } ); + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Test orphan removal for bidirectional relationship + doInJPA( this::entityManagerFactory, entityManager -> { + final PaintColor color = new PaintColor( 2, "Blue" ); + final Car car = entityManager.find( Car.class, 1 ); + car.setPaintColor( color ); + entityManager.persist( color ); + entityManager.merge( car ); + } ); + + doInJPA( this::entityManagerFactory, entityManager -> { + final Car car = entityManager.find( Car.class, 1 ); + assertNotNull( car.getPaintColor() ); + + final PaintColor color = entityManager.find( PaintColor.class, 1 ); + assertNull( color ); + } ); + } + + @Entity(name = "Car") + public static class Car { + @Id + private Integer id; + + // represents a bidirectional one-to-one + @OneToOne(orphanRemoval = true, optional = false) + private PaintColor paintColor; + + // represents a unidirectional one-to-one + @OneToOne(orphanRemoval = true, optional = false) + private Engine engine; + + Car() { + // Required by JPA + } + + Car(Integer id, Engine engine, PaintColor paintColor) { + this.id = id; + this.engine = engine; + this.paintColor = paintColor; + paintColor.setCar( this ); + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public PaintColor getPaintColor() { + return paintColor; + } + + public void setPaintColor(PaintColor paintColor) { + this.paintColor = paintColor; + } + + public Engine getEngine() { + return engine; + } + + public void setEngine(Engine engine) { + this.engine = engine; + } + } + + @Entity(name = "Engine") + public static class Engine { + @Id + private Integer id; + private Integer horsePower; + + Engine() { + // Required by JPA + } + + Engine(Integer id, int horsePower) { + this.id = id; + this.horsePower = horsePower; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Integer getHorsePower() { + return horsePower; + } + + public void setHorsePower(Integer horsePower) { + this.horsePower = horsePower; + } + } + + @Entity(name = "PaintColor") + public static class PaintColor { + @Id + private Integer id; + private String color; + + @OneToOne(mappedBy = "paintColor") + private Car car; + + PaintColor() { + // Required by JPA + } + + PaintColor(Integer id, String color) { + this.id = id; + this.color = color; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public Car getCar() { + return car; + } + + public void setCar(Car car) { + this.car = car; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/jpa/orphan/one2one/OneToOneEagerOrphanRemovalTest.java b/hibernate-core/src/test/java/org/hibernate/test/jpa/orphan/one2one/OneToOneEagerOrphanRemovalTest.java new file mode 100644 index 0000000000..f1c4b9d729 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/jpa/orphan/one2one/OneToOneEagerOrphanRemovalTest.java @@ -0,0 +1,202 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.jpa.orphan.one2one; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; +import org.junit.Test; + +import org.hibernate.testing.TestForIssue; + +import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; +import static org.junit.Assert.assertNull; + +/** + * @author Chris Cranford + */ +@TestForIssue( jiraKey = "HHH-9663" ) +public class OneToOneEagerOrphanRemovalTest extends BaseEntityManagerFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Car.class, PaintColor.class, Engine.class }; + } + + @Test + public void testOneToOneEagerOrphanRemoval() { + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Initialize the data + doInJPA( this::entityManagerFactory, entityManager -> { + final PaintColor color = new PaintColor( 1, "Red" ); + final Engine engine = new Engine( 1, 275 ); + final Car car = new Car( 1, engine, color ); + + entityManager.persist( engine ); + entityManager.persist( color ); + entityManager.persist( car ); + } ); + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Test orphan removal for unidirectional relationship + doInJPA( this::entityManagerFactory, entityManager -> { + final Car car = entityManager.find( Car.class, 1 ); + car.setEngine( null ); + entityManager.merge( car ); + } ); + + doInJPA( this::entityManagerFactory, entityManager -> { + final Car car = entityManager.find( Car.class, 1 ); + assertNull( car.getEngine() ); + + final Engine engine = entityManager.find( Engine.class, 1 ); + assertNull( engine ); + } ); + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Test orphan removal for bidirectional relationship + doInJPA( this::entityManagerFactory, entityManager -> { + final Car car = entityManager.find( Car.class, 1 ); + car.setPaintColor( null ); + entityManager.merge( car ); + } ); + + doInJPA( this::entityManagerFactory, entityManager -> { + final Car car = entityManager.find( Car.class, 1 ); + assertNull( car.getPaintColor() ); + + final PaintColor color = entityManager.find( PaintColor.class, 1 ); + assertNull( color ); + } ); + } + + @Entity(name = "Car") + public static class Car { + @Id + private Integer id; + + // represents a bidirectional one-to-one + @OneToOne(orphanRemoval = true, fetch = FetchType.EAGER) + private PaintColor paintColor; + + // represents a unidirectional one-to-one + @OneToOne(orphanRemoval = true, fetch = FetchType.EAGER) + private Engine engine; + + Car() { + // Required by JPA + } + + Car(Integer id, Engine engine, PaintColor paintColor) { + this.id = id; + this.engine = engine; + this.paintColor = paintColor; + paintColor.setCar( this ); + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public PaintColor getPaintColor() { + return paintColor; + } + + public void setPaintColor(PaintColor paintColor) { + this.paintColor = paintColor; + } + + public Engine getEngine() { + return engine; + } + + public void setEngine(Engine engine) { + this.engine = engine; + } + } + + @Entity(name = "Engine") + public static class Engine { + @Id + private Integer id; + private Integer horsePower; + + Engine() { + // Required by JPA + } + + Engine(Integer id, int horsePower) { + this.id = id; + this.horsePower = horsePower; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Integer getHorsePower() { + return horsePower; + } + + public void setHorsePower(Integer horsePower) { + this.horsePower = horsePower; + } + } + + @Entity(name = "PaintColor") + public static class PaintColor { + @Id + private Integer id; + private String color; + + @OneToOne(mappedBy = "paintColor") + private Car car; + + PaintColor() { + // Required by JPA + } + + PaintColor(Integer id, String color) { + this.id = id; + this.color = color; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public Car getCar() { + return car; + } + + public void setCar(Car car) { + this.car = car; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/jpa/orphan/one2one/OneToOneLazyNonOptionalOrphanRemovalTest.java b/hibernate-core/src/test/java/org/hibernate/test/jpa/orphan/one2one/OneToOneLazyNonOptionalOrphanRemovalTest.java new file mode 100644 index 0000000000..43729ae5d7 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/jpa/orphan/one2one/OneToOneLazyNonOptionalOrphanRemovalTest.java @@ -0,0 +1,208 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.jpa.orphan.one2one; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; +import org.junit.Test; + +import org.hibernate.testing.TestForIssue; + +import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +/** + * @author Chris Cranford + */ +@TestForIssue( jiraKey = "HHH-9663" ) +public class OneToOneLazyNonOptionalOrphanRemovalTest extends BaseEntityManagerFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Car.class, PaintColor.class, Engine.class }; + } + + @Test + public void testOneToOneLazyNonOptionalOrphanRemoval() { + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Initialize the data + doInJPA( this::entityManagerFactory, entityManager -> { + final PaintColor color = new PaintColor( 1, "Red" ); + final Engine engine1 = new Engine( 1, 275 ); + final Engine engine2 = new Engine( 2, 295 ); + final Car car = new Car( 1, engine1, color ); + + entityManager.persist( engine1 ); + entityManager.persist( engine2 ); + entityManager.persist( color ); + entityManager.persist( car ); + } ); + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Test orphan removal for unidirectional relationship + doInJPA( this::entityManagerFactory, entityManager -> { + final Car car = entityManager.find( Car.class, 1 ); + final Engine engine = entityManager.find( Engine.class, 2 ); + car.setEngine( engine ); + entityManager.merge( car ); + } ); + + doInJPA( this::entityManagerFactory, entityManager -> { + final Car car = entityManager.find( Car.class, 1 ); + assertNotNull( car.getEngine() ); + + final Engine engine = entityManager.find( Engine.class, 1 ); + assertNull( engine ); + } ); + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Test orphan removal for bidirectional relationship + doInJPA( this::entityManagerFactory, entityManager -> { + final PaintColor color = new PaintColor( 2, "Blue" ); + final Car car = entityManager.find( Car.class, 1 ); + car.setPaintColor( color ); + entityManager.persist( color ); + entityManager.merge( car ); + } ); + + doInJPA( this::entityManagerFactory, entityManager -> { + final Car car = entityManager.find( Car.class, 1 ); + assertNotNull( car.getPaintColor() ); + + final PaintColor color = entityManager.find( PaintColor.class, 1 ); + assertNull( color ); + } ); + } + + @Entity(name = "Car") + public static class Car { + @Id + private Integer id; + + // represents a bidirectional one-to-one + @OneToOne(orphanRemoval = true, fetch = FetchType.LAZY, optional = false) + private PaintColor paintColor; + + // represents a unidirectional one-to-one + @OneToOne(orphanRemoval = true, fetch = FetchType.LAZY, optional = false) + private Engine engine; + + Car() { + // Required by JPA + } + + Car(Integer id, Engine engine, PaintColor paintColor) { + this.id = id; + this.engine = engine; + this.paintColor = paintColor; + paintColor.setCar( this ); + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public PaintColor getPaintColor() { + return paintColor; + } + + public void setPaintColor(PaintColor paintColor) { + this.paintColor = paintColor; + } + + public Engine getEngine() { + return engine; + } + + public void setEngine(Engine engine) { + this.engine = engine; + } + } + + @Entity(name = "Engine") + public static class Engine { + @Id + private Integer id; + private Integer horsePower; + + Engine() { + // Required by JPA + } + + Engine(Integer id, int horsePower) { + this.id = id; + this.horsePower = horsePower; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Integer getHorsePower() { + return horsePower; + } + + public void setHorsePower(Integer horsePower) { + this.horsePower = horsePower; + } + } + + @Entity(name = "PaintColor") + public static class PaintColor { + @Id + private Integer id; + private String color; + + @OneToOne(mappedBy = "paintColor") + private Car car; + + PaintColor() { + // Required by JPA + } + + PaintColor(Integer id, String color) { + this.id = id; + this.color = color; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public Car getCar() { + return car; + } + + public void setCar(Car car) { + this.car = car; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/jpa/orphan/one2one/OneToOneLazyOrphanRemovalTest.java b/hibernate-core/src/test/java/org/hibernate/test/jpa/orphan/one2one/OneToOneLazyOrphanRemovalTest.java new file mode 100644 index 0000000000..6b56ad55b8 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/jpa/orphan/one2one/OneToOneLazyOrphanRemovalTest.java @@ -0,0 +1,202 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.jpa.orphan.one2one; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; +import org.junit.Test; + +import org.hibernate.testing.TestForIssue; + +import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; +import static org.junit.Assert.assertNull; + +/** + * @author Chris Cranford + */ +@TestForIssue( jiraKey = "HHH-9663" ) +public class OneToOneLazyOrphanRemovalTest extends BaseEntityManagerFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Car.class, PaintColor.class, Engine.class }; + } + + @Test + public void testOneToOneLazyOrphanRemoval() { + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Initialize the data + doInJPA( this::entityManagerFactory, entityManager -> { + final PaintColor color = new PaintColor( 1, "Red" ); + final Engine engine = new Engine( 1, 275 ); + final Car car = new Car( 1, engine, color ); + + entityManager.persist( engine ); + entityManager.persist( color ); + entityManager.persist( car ); + } ); + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Test orphan removal for unidirectional relationship + doInJPA( this::entityManagerFactory, entityManager -> { + final Car car = entityManager.find( Car.class, 1 ); + car.setEngine( null ); + entityManager.merge( car ); + } ); + + doInJPA( this::entityManagerFactory, entityManager -> { + final Car car = entityManager.find( Car.class, 1 ); + assertNull( car.getEngine() ); + + final Engine engine = entityManager.find( Engine.class, 1 ); + assertNull( engine ); + } ); + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Test orphan removal for bidirectional relationship + doInJPA( this::entityManagerFactory, entityManager -> { + final Car car = entityManager.find( Car.class, 1 ); + car.setPaintColor( null ); + entityManager.merge( car ); + } ); + + doInJPA( this::entityManagerFactory, entityManager -> { + final Car car = entityManager.find( Car.class, 1 ); + assertNull( car.getPaintColor() ); + + final PaintColor color = entityManager.find( PaintColor.class, 1 ); + assertNull( color ); + } ); + } + + @Entity(name = "Car") + public static class Car { + @Id + private Integer id; + + // represents a bidirectional one-to-one + @OneToOne(orphanRemoval = true, fetch = FetchType.LAZY) + private PaintColor paintColor; + + // represents a unidirectional one-to-one + @OneToOne(orphanRemoval = true, fetch = FetchType.LAZY) + private Engine engine; + + Car() { + // Required by JPA + } + + Car(Integer id, Engine engine, PaintColor paintColor) { + this.id = id; + this.engine = engine; + this.paintColor = paintColor; + paintColor.setCar( this ); + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public PaintColor getPaintColor() { + return paintColor; + } + + public void setPaintColor(PaintColor paintColor) { + this.paintColor = paintColor; + } + + public Engine getEngine() { + return engine; + } + + public void setEngine(Engine engine) { + this.engine = engine; + } + } + + @Entity(name = "Engine") + public static class Engine { + @Id + private Integer id; + private Integer horsePower; + + Engine() { + // Required by JPA + } + + Engine(Integer id, int horsePower) { + this.id = id; + this.horsePower = horsePower; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Integer getHorsePower() { + return horsePower; + } + + public void setHorsePower(Integer horsePower) { + this.horsePower = horsePower; + } + } + + @Entity(name = "PaintColor") + public static class PaintColor { + @Id + private Integer id; + private String color; + + @OneToOne(mappedBy = "paintColor") + private Car car; + + PaintColor() { + // Required by JPA + } + + PaintColor(Integer id, String color) { + this.id = id; + this.color = color; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public Car getCar() { + return car; + } + + public void setCar(Car car) { + this.car = car; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneEagerNonOptionalOrphanRemovalTest.java b/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneEagerNonOptionalOrphanRemovalTest.java new file mode 100644 index 0000000000..76aca1e2e5 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneEagerNonOptionalOrphanRemovalTest.java @@ -0,0 +1,207 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.orphan.one2one; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +import org.junit.Test; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +/** + * @author Chris Cranford + */ +@TestForIssue( jiraKey = "HHH-9663" ) +public class OneToOneEagerNonOptionalOrphanRemovalTest extends BaseCoreFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Car.class, PaintColor.class, Engine.class }; + } + + @Test + public void testOneToOneLazyNonOptionalOrphanRemoval() { + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Initialize the data + doInHibernate( this::sessionFactory, session -> { + final PaintColor color = new PaintColor( 1, "Red" ); + final Engine engine1 = new Engine( 1, 275 ); + final Engine engine2 = new Engine( 2, 295 ); + final Car car = new Car( 1, engine1, color ); + + session.save( engine1 ); + session.save( engine2 ); + session.save( color ); + session.save( car ); + } ); + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Test orphan removal for unidirectional relationship + doInHibernate( this::sessionFactory, session -> { + final Car car = session.find( Car.class, 1 ); + final Engine engine = session.find( Engine.class, 2 ); + car.setEngine( engine ); + session.update( car ); + } ); + + doInHibernate( this::sessionFactory, session -> { + final Car car = session.find( Car.class, 1 ); + assertNotNull( car.getEngine() ); + + final Engine engine = session.find( Engine.class, 1 ); + assertNull( engine ); + } ); + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Test orphan removal for bidirectional relationship + doInHibernate( this::sessionFactory, session -> { + final PaintColor color = new PaintColor( 2, "Blue" ); + final Car car = session.find( Car.class, 1 ); + car.setPaintColor( color ); + session.save( color ); + session.update( car ); + } ); + + doInHibernate( this::sessionFactory, session -> { + final Car car = session.find( Car.class, 1 ); + assertNotNull( car.getPaintColor() ); + + final PaintColor color = session.find( PaintColor.class, 1 ); + assertNull( color ); + } ); + } + + @Entity(name = "Car") + public static class Car { + @Id + private Integer id; + + // represents a bidirectional one-to-one + @OneToOne(orphanRemoval = true, optional = false) + private PaintColor paintColor; + + // represents a unidirectional one-to-one + @OneToOne(orphanRemoval = true, optional = false) + private Engine engine; + + Car() { + // Required by JPA + } + + Car(Integer id, Engine engine, PaintColor paintColor) { + this.id = id; + this.engine = engine; + this.paintColor = paintColor; + paintColor.setCar( this ); + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public PaintColor getPaintColor() { + return paintColor; + } + + public void setPaintColor(PaintColor paintColor) { + this.paintColor = paintColor; + } + + public Engine getEngine() { + return engine; + } + + public void setEngine(Engine engine) { + this.engine = engine; + } + } + + @Entity(name = "Engine") + public static class Engine { + @Id + private Integer id; + private Integer horsePower; + + Engine() { + // Required by JPA + } + + Engine(Integer id, int horsePower) { + this.id = id; + this.horsePower = horsePower; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Integer getHorsePower() { + return horsePower; + } + + public void setHorsePower(Integer horsePower) { + this.horsePower = horsePower; + } + } + + @Entity(name = "PaintColor") + public static class PaintColor { + @Id + private Integer id; + private String color; + + @OneToOne(mappedBy = "paintColor") + private Car car; + + PaintColor() { + // Required by JPA + } + + PaintColor(Integer id, String color) { + this.id = id; + this.color = color; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public Car getCar() { + return car; + } + + public void setCar(Car car) { + this.car = car; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneEagerOrphanRemovalTest.java b/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneEagerOrphanRemovalTest.java new file mode 100644 index 0000000000..ee491de683 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneEagerOrphanRemovalTest.java @@ -0,0 +1,202 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.orphan.one2one; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +import org.junit.Test; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; +import static org.junit.Assert.assertNull; + +/** + * @author Chris Cranford + */ +@TestForIssue( jiraKey = "HHH-9663" ) +public class OneToOneEagerOrphanRemovalTest extends BaseCoreFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Car.class, PaintColor.class, Engine.class }; + } + + @Test + public void testOneToOneEagerOrphanRemoval() { + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Initialize the data + doInHibernate( this::sessionFactory, session -> { + final PaintColor color = new PaintColor( 1, "Red" ); + final Engine engine = new Engine( 1, 275 ); + final Car car = new Car( 1, engine, color ); + + session.save( engine ); + session.save( color ); + session.save( car ); + } ); + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Test orphan removal for unidirectional relationship + doInHibernate( this::sessionFactory, session -> { + final Car car = session.find( Car.class, 1 ); + car.setEngine( null ); + session.update( car ); + } ); + + doInHibernate( this::sessionFactory, session -> { + final Car car = session.find( Car.class, 1 ); + assertNull( car.getEngine() ); + + final Engine engine = session.find( Engine.class, 1 ); + assertNull( engine ); + } ); + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Test orphan removal for bidirectional relationship + doInHibernate( this::sessionFactory, session -> { + final Car car = session.find( Car.class, 1 ); + car.setPaintColor( null ); + session.update( car ); + } ); + + doInHibernate( this::sessionFactory, session -> { + final Car car = session.find( Car.class, 1 ); + assertNull( car.getPaintColor() ); + + final PaintColor color = session.find( PaintColor.class, 1 ); + assertNull( color ); + } ); + } + + @Entity(name = "Car") + public static class Car { + @Id + private Integer id; + + // represents a bidirectional one-to-one + @OneToOne(orphanRemoval = true, fetch = FetchType.EAGER) + private PaintColor paintColor; + + // represents a unidirectional one-to-one + @OneToOne(orphanRemoval = true, fetch = FetchType.EAGER) + private Engine engine; + + Car() { + // Required by JPA + } + + Car(Integer id, Engine engine, PaintColor paintColor) { + this.id = id; + this.engine = engine; + this.paintColor = paintColor; + paintColor.setCar( this ); + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public PaintColor getPaintColor() { + return paintColor; + } + + public void setPaintColor(PaintColor paintColor) { + this.paintColor = paintColor; + } + + public Engine getEngine() { + return engine; + } + + public void setEngine(Engine engine) { + this.engine = engine; + } + } + + @Entity(name = "Engine") + public static class Engine { + @Id + private Integer id; + private Integer horsePower; + + Engine() { + // Required by JPA + } + + Engine(Integer id, int horsePower) { + this.id = id; + this.horsePower = horsePower; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Integer getHorsePower() { + return horsePower; + } + + public void setHorsePower(Integer horsePower) { + this.horsePower = horsePower; + } + } + + @Entity(name = "PaintColor") + public static class PaintColor { + @Id + private Integer id; + private String color; + + @OneToOne(mappedBy = "paintColor") + private Car car; + + PaintColor() { + // Required by JPA + } + + PaintColor(Integer id, String color) { + this.id = id; + this.color = color; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public Car getCar() { + return car; + } + + public void setCar(Car car) { + this.car = car; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneLazyNonOptionalOrphanRemovalTest.java b/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneLazyNonOptionalOrphanRemovalTest.java new file mode 100644 index 0000000000..3c8ef6e94b --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneLazyNonOptionalOrphanRemovalTest.java @@ -0,0 +1,208 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.orphan.one2one; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +import org.junit.Test; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +/** + * @author Chris Cranford + */ +@TestForIssue( jiraKey = "HHH-9663" ) +public class OneToOneLazyNonOptionalOrphanRemovalTest extends BaseCoreFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Car.class, PaintColor.class, Engine.class }; + } + + @Test + public void testOneToOneLazyNonOptionalOrphanRemoval() { + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Initialize the data + doInHibernate( this::sessionFactory, session -> { + final PaintColor color = new PaintColor( 1, "Red" ); + final Engine engine1 = new Engine( 1, 275 ); + final Engine engine2 = new Engine( 2, 295 ); + final Car car = new Car( 1, engine1, color ); + + session.save( engine1 ); + session.save( engine2 ); + session.save( color ); + session.save( car ); + } ); + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Test orphan removal for unidirectional relationship + doInHibernate( this::sessionFactory, session -> { + final Car car = session.find( Car.class, 1 ); + final Engine engine = session.find( Engine.class, 2 ); + car.setEngine( engine ); + session.update( car ); + } ); + + doInHibernate( this::sessionFactory, session -> { + final Car car = session.find( Car.class, 1 ); + assertNotNull( car.getEngine() ); + + final Engine engine = session.find( Engine.class, 1 ); + assertNull( engine ); + } ); + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Test orphan removal for bidirectional relationship + doInHibernate( this::sessionFactory, session -> { + final PaintColor color = new PaintColor( 2, "Blue" ); + final Car car = session.find( Car.class, 1 ); + car.setPaintColor( color ); + session.save( color ); + session.update( car ); + } ); + + doInHibernate( this::sessionFactory, session -> { + final Car car = session.find( Car.class, 1 ); + assertNotNull( car.getPaintColor() ); + + final PaintColor color = session.find( PaintColor.class, 1 ); + assertNull( color ); + } ); + } + + @Entity(name = "Car") + public static class Car { + @Id + private Integer id; + + // represents a bidirectional one-to-one + @OneToOne(orphanRemoval = true, fetch = FetchType.LAZY, optional = false) + private PaintColor paintColor; + + // represents a unidirectional one-to-one + @OneToOne(orphanRemoval = true, fetch = FetchType.LAZY, optional = false) + private Engine engine; + + Car() { + // Required by JPA + } + + Car(Integer id, Engine engine, PaintColor paintColor) { + this.id = id; + this.engine = engine; + this.paintColor = paintColor; + paintColor.setCar( this ); + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public PaintColor getPaintColor() { + return paintColor; + } + + public void setPaintColor(PaintColor paintColor) { + this.paintColor = paintColor; + } + + public Engine getEngine() { + return engine; + } + + public void setEngine(Engine engine) { + this.engine = engine; + } + } + + @Entity(name = "Engine") + public static class Engine { + @Id + private Integer id; + private Integer horsePower; + + Engine() { + // Required by JPA + } + + Engine(Integer id, int horsePower) { + this.id = id; + this.horsePower = horsePower; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Integer getHorsePower() { + return horsePower; + } + + public void setHorsePower(Integer horsePower) { + this.horsePower = horsePower; + } + } + + @Entity(name = "PaintColor") + public static class PaintColor { + @Id + private Integer id; + private String color; + + @OneToOne(mappedBy = "paintColor") + private Car car; + + PaintColor() { + // Required by JPA + } + + PaintColor(Integer id, String color) { + this.id = id; + this.color = color; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public Car getCar() { + return car; + } + + public void setCar(Car car) { + this.car = car; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneLazyOrphanRemovalTest.java b/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneLazyOrphanRemovalTest.java new file mode 100644 index 0000000000..cbfa1b9e14 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneLazyOrphanRemovalTest.java @@ -0,0 +1,202 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.orphan.one2one; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +import org.junit.Test; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; +import static org.junit.Assert.assertNull; + +/** + * @author Chris Cranford + */ +@TestForIssue( jiraKey = "HHH-9663" ) +public class OneToOneLazyOrphanRemovalTest extends BaseCoreFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Car.class, PaintColor.class, Engine.class }; + } + + @Test + public void testOneToOneLazyOrphanRemoval() { + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Initialize the data + doInHibernate( this::sessionFactory, session -> { + final PaintColor color = new PaintColor( 1, "Red" ); + final Engine engine = new Engine( 1, 275 ); + final Car car = new Car( 1, engine, color ); + + session.save( engine ); + session.save( color ); + session.save( car ); + } ); + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Test orphan removal for unidirectional relationship + doInHibernate( this::sessionFactory, session -> { + final Car car = session.find( Car.class, 1 ); + car.setEngine( null ); + session.update( car ); + } ); + + doInHibernate( this::sessionFactory, session -> { + final Car car = session.find( Car.class, 1 ); + assertNull( car.getEngine() ); + + final Engine engine = session.find( Engine.class, 1 ); + assertNull( engine ); + } ); + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Test orphan removal for bidirectional relationship + doInHibernate( this::sessionFactory, session -> { + final Car car = session.find( Car.class, 1 ); + car.setPaintColor( null ); + session.update( car ); + } ); + + doInHibernate( this::sessionFactory, session -> { + final Car car = session.find( Car.class, 1 ); + assertNull( car.getPaintColor() ); + + final PaintColor color = session.find( PaintColor.class, 1 ); + assertNull( color ); + } ); + } + + @Entity(name = "Car") + public static class Car { + @Id + private Integer id; + + // represents a bidirectional one-to-one + @OneToOne(orphanRemoval = true, fetch = FetchType.LAZY) + private PaintColor paintColor; + + // represents a unidirectional one-to-one + @OneToOne(orphanRemoval = true, fetch = FetchType.LAZY) + private Engine engine; + + Car() { + // Required by JPA + } + + Car(Integer id, Engine engine, PaintColor paintColor) { + this.id = id; + this.engine = engine; + this.paintColor = paintColor; + paintColor.setCar( this ); + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public PaintColor getPaintColor() { + return paintColor; + } + + public void setPaintColor(PaintColor paintColor) { + this.paintColor = paintColor; + } + + public Engine getEngine() { + return engine; + } + + public void setEngine(Engine engine) { + this.engine = engine; + } + } + + @Entity(name = "Engine") + public static class Engine { + @Id + private Integer id; + private Integer horsePower; + + Engine() { + // Required by JPA + } + + Engine(Integer id, int horsePower) { + this.id = id; + this.horsePower = horsePower; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Integer getHorsePower() { + return horsePower; + } + + public void setHorsePower(Integer horsePower) { + this.horsePower = horsePower; + } + } + + @Entity(name = "PaintColor") + public static class PaintColor { + @Id + private Integer id; + private String color; + + @OneToOne(mappedBy = "paintColor") + private Car car; + + PaintColor() { + // Required by JPA + } + + PaintColor(Integer id, String color) { + this.id = id; + this.color = color; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public Car getCar() { + return car; + } + + public void setCar(Car car) { + this.car = car; + } + } +}