HHH-9663 Added test cases.

This commit is contained in:
Chris Cranford 2017-04-07 12:33:37 -04:00 committed by Gail Badner
parent 19e585bf9d
commit 85732409ee
8 changed files with 1638 additions and 0 deletions

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}
}