Re-enabled additional tests

This commit is contained in:
Andrea Boriero 2021-07-10 20:59:21 +02:00
parent 96f8273673
commit 96be013089
71 changed files with 1946 additions and 1832 deletions

View File

@ -0,0 +1,391 @@
/*
* 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.orm.test.orphan;
import org.hibernate.Hibernate;
import org.hibernate.LockMode;
import org.hibernate.internal.util.SerializationHelper;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author Gavin King
*/
@DomainModel(
xmlMappings = "org/hibernate/orm/test/orphan/Product.hbm.xml"
)
@SessionFactory
public class OrphanTest {
@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete from Part" ).executeUpdate();
session.createQuery( "delete from Product" ).executeUpdate();
}
);
}
@Test
@SuppressWarnings({ "unchecked" })
public void testOrphanDeleteOnDelete(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Product prod = new Product();
prod.setName( "Widget" );
Part part = new Part();
part.setName( "Widge" );
part.setDescription( "part if a Widget" );
prod.getParts().add( part );
Part part2 = new Part();
part2.setName( "Get" );
part2.setDescription( "another part if a Widget" );
prod.getParts().add( part2 );
session.persist( prod );
session.flush();
prod.getParts().remove( part );
session.delete( prod );
}
);
scope.inTransaction(
session -> {
assertNull( session.get( Part.class, "Widge" ) );
assertNull( session.get( Part.class, "Get" ) );
assertNull( session.get( Product.class, "Widget" ) );
}
);
}
@Test
@SuppressWarnings({ "unchecked" })
public void testOrphanDeleteAfterPersist(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Product prod = new Product();
prod.setName( "Widget" );
Part part = new Part();
part.setName( "Widge" );
part.setDescription( "part if a Widget" );
prod.getParts().add( part );
Part part2 = new Part();
part2.setName( "Get" );
part2.setDescription( "another part if a Widget" );
prod.getParts().add( part2 );
session.persist( prod );
prod.getParts().remove( part );
}
);
scope.inTransaction(
session -> {
assertNull( session.get( Part.class, "Widge" ) );
assertNotNull( session.get( Part.class, "Get" ) );
session.delete( session.get( Product.class, "Widget" ) );
}
);
}
@Test
@SuppressWarnings({ "unchecked" })
public void testOrphanDeleteAfterPersistAndFlush(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Product prod = new Product();
prod.setName( "Widget" );
Part part = new Part();
part.setName( "Widge" );
part.setDescription( "part if a Widget" );
prod.getParts().add( part );
Part part2 = new Part();
part2.setName( "Get" );
part2.setDescription( "another part if a Widget" );
prod.getParts().add( part2 );
session.persist( prod );
session.flush();
prod.getParts().remove( part );
}
);
scope.inTransaction(
session -> {
assertNull( session.get( Part.class, "Widge" ) );
assertNotNull( session.get( Part.class, "Get" ) );
session.delete( session.get( Product.class, "Widget" ) );
}
);
}
@Test
@SuppressWarnings({ "unchecked" })
public void testOrphanDeleteAfterLock(SessionFactoryScope scope) {
Product prod = new Product();
Part part = new Part();
scope.inTransaction(
session -> {
prod.setName( "Widget" );
part.setName( "Widge" );
part.setDescription( "part if a Widget" );
prod.getParts().add( part );
Part part2 = new Part();
part2.setName( "Get" );
part2.setDescription( "another part if a Widget" );
prod.getParts().add( part2 );
session.persist( prod );
}
);
scope.inTransaction(
session -> {
session.lock( prod, LockMode.READ );
prod.getParts().remove( part );
}
);
scope.inTransaction(
session -> {
assertNull( session.get( Part.class, "Widge" ) );
assertNotNull( session.get( Part.class, "Get" ) );
session.delete( session.get( Product.class, "Widget" ) );
}
);
}
@Test
@SuppressWarnings({ "unchecked" })
public void testOrphanDeleteOnSaveOrUpdate(SessionFactoryScope scope) {
Product prod = new Product();
Part part = new Part();
scope.inTransaction(
session -> {
prod.setName( "Widget" );
part.setName( "Widge" );
part.setDescription( "part if a Widget" );
prod.getParts().add( part );
Part part2 = new Part();
part2.setName( "Get" );
part2.setDescription( "another part if a Widget" );
prod.getParts().add( part2 );
session.persist( prod );
}
);
prod.getParts().remove( part );
scope.inTransaction(
session ->
session.saveOrUpdate( prod )
);
scope.inTransaction(
session -> {
assertNull( session.get( Part.class, "Widge" ) );
assertNotNull( session.get( Part.class, "Get" ) );
session.delete( session.get( Product.class, "Widget" ) );
}
);
}
@Test
@SuppressWarnings({ "unchecked" })
public void testOrphanDeleteOnSaveOrUpdateAfterSerialization(SessionFactoryScope scope) {
Product prod = new Product();
Part part = new Part();
scope.inTransaction(
session -> {
prod.setName( "Widget" );
part.setName( "Widge" );
part.setDescription( "part if a Widget" );
prod.getParts().add( part );
Part part2 = new Part();
part2.setName( "Get" );
part2.setDescription( "another part if a Widget" );
prod.getParts().add( part2 );
session.persist( prod );
}
);
prod.getParts().remove( part );
Product cloned = (Product) SerializationHelper.clone( prod );
scope.inTransaction(
session ->
session.saveOrUpdate( cloned )
);
scope.inTransaction(
session -> {
assertNull( session.get( Part.class, "Widge" ) );
assertNotNull( session.get( Part.class, "Get" ) );
session.delete( session.get( Product.class, "Widget" ) );
}
);
}
@Test
@SuppressWarnings({ "unchecked" })
public void testOrphanDelete(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Product prod = new Product();
prod.setName( "Widget" );
Part part = new Part();
part.setName( "Widge" );
part.setDescription( "part if a Widget" );
prod.getParts().add( part );
Part part2 = new Part();
part2.setName( "Get" );
part2.setDescription( "another part if a Widget" );
prod.getParts().add( part2 );
session.persist( prod );
}
);
scope.getSessionFactory().getCache().evictEntityData( Product.class );
scope.getSessionFactory().getCache().evictEntityData( Part.class );
scope.inTransaction(
session -> {
Product prod = session.get( Product.class, "Widget" );
assertTrue( Hibernate.isInitialized( prod.getParts() ) );
Part part = session.get( Part.class, "Widge" );
prod.getParts().remove( part );
}
);
scope.inTransaction(
session -> {
assertNull( session.get( Part.class, "Widge" ) );
assertNotNull( session.get( Part.class, "Get" ) );
session.delete( session.get( Product.class, "Widget" ) );
}
);
}
@Test
@SuppressWarnings({ "unchecked" })
public void testOrphanDeleteOnMerge(SessionFactoryScope scope) {
Product prod = new Product();
Part part = new Part();
scope.inTransaction(
session -> {
prod.setName( "Widget" );
part.setName( "Widge" );
part.setDescription( "part if a Widget" );
prod.getParts().add( part );
Part part2 = new Part();
part2.setName( "Get" );
part2.setDescription( "another part if a Widget" );
prod.getParts().add( part2 );
session.persist( prod );
}
);
prod.getParts().remove( part );
scope.inTransaction(
session ->
session.merge( prod )
);
scope.inTransaction(
session -> {
assertNull( session.get( Part.class, "Widge" ) );
assertNotNull( session.get( Part.class, "Get" ) );
session.delete( session.get( Product.class, "Widget" ) );
}
);
}
@Test
@SuppressWarnings({ "unchecked" })
public void testOrphanDeleteOnMergeRemoveElementMerge(SessionFactoryScope scope) {
Product prod = new Product();
Part part = new Part();
scope.inTransaction(
session -> {
prod.setName( "Widget" );
part.setName( "Widge" );
part.setDescription( "part if a Widget" );
prod.getParts().add( part );
session.persist( prod );
}
);
scope.inTransaction(
session -> {
session.merge( prod );
prod.getParts().remove( part );
session.merge( prod );
}
);
scope.inTransaction(
session -> {
assertNull( session.get( Part.class, "Widge" ) );
session.delete( session.get( Product.class, "Widget" ) );
}
);
}
@Test
@SuppressWarnings({ "unchecked" })
@TestForIssue(jiraKey = "HHH-9171")
public void testOrphanDeleteOnAddElementMergeRemoveElementMerge(SessionFactoryScope scope) {
Product prod = new Product();
scope.inTransaction(
session -> {
prod.setName( "Widget" );
session.persist( prod );
}
);
Part part = new Part();
part.setName( "Widge" );
part.setDescription( "part if a Widget" );
prod.getParts().add( part );
scope.inTransaction(
session -> {
session.merge( prod );
// In Section 2.9, Entity Relationships, the JPA 2.1 spec says:
// "If the entity being orphaned is a detached, new, or removed entity,
// the semantics of orphanRemoval do not apply."
// In other words, since part is a new entity, it will not be deleted when removed
// from prod.parts, even though cascade for the association includes "delete-orphan".
prod.getParts().remove( part );
session.merge( prod );
}
);
scope.inTransaction(
session -> {
assertNotNull( session.get( Part.class, "Widge" ) );
session.delete( session.get( Product.class, "Widget" ) );
}
);
}
}

View File

@ -6,7 +6,7 @@
*/ */
//$Id: Part.java 5725 2005-02-14 12:10:15Z oneovthafew $ //$Id: Part.java 5725 2005-02-14 12:10:15Z oneovthafew $
package org.hibernate.test.orphan; package org.hibernate.orm.test.orphan;
import java.io.Serializable; import java.io.Serializable;
/** /**

View File

@ -14,7 +14,7 @@
--> -->
<hibernate-mapping package="org.hibernate.test.orphan"> <hibernate-mapping package="org.hibernate.orm.test.orphan">
<class name="Product" table="t_product"> <class name="Product" table="t_product">
<id name="name"/> <id name="name"/>

View File

@ -6,7 +6,8 @@
*/ */
//$Id: Product.java 5725 2005-02-14 12:10:15Z oneovthafew $ //$Id: Product.java 5725 2005-02-14 12:10:15Z oneovthafew $
package org.hibernate.test.orphan; package org.hibernate.orm.test.orphan;
import java.io.Serializable; import java.io.Serializable;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;

View File

@ -0,0 +1,58 @@
package org.hibernate.orm.test.orphan.elementcollection;
import java.util.Collections;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
@TestForIssue(jiraKey = "HHH-14597")
@DomainModel(
xmlMappings = "org/hibernate/orm/test/orphan/elementcollection/student.hbm.xml"
)
@SessionFactory
public class ElementCollectionOrphanTest {
@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete from Student" ).executeUpdate();
session.createQuery( "delete from EnrollableClass" ).executeUpdate();
session.createQuery( "delete from EnrolledClassSeat" ).executeUpdate();
}
);
}
@Test
public void setCompositeElementTest(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
EnrollableClass aClass = new EnrollableClass();
aClass.setId( "123" );
aClass.setName( "Math" );
session.save( aClass );
Student aStudent = new Student();
aStudent.setId( "s1" );
aStudent.setFirstName( "John" );
aStudent.setLastName( "Smith" );
EnrolledClassSeat seat = new EnrolledClassSeat();
seat.setId( "seat1" );
seat.setRow( 10 );
seat.setColumn( 5 );
StudentEnrolledClass enrClass = new StudentEnrolledClass();
enrClass.setEnrolledClass( aClass );
enrClass.setClassStartTime( 130 );
enrClass.setSeat( seat );
aStudent.setEnrolledClasses( Collections.singleton( enrClass ) );
session.save( aStudent );
}
);
}
}

View File

@ -1,4 +1,4 @@
package org.hibernate.test.orphan.elementcollection; package org.hibernate.orm.test.orphan.elementcollection;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;

View File

@ -1,4 +1,4 @@
package org.hibernate.test.orphan.elementcollection; package org.hibernate.orm.test.orphan.elementcollection;
public class EnrolledClassSeat { public class EnrolledClassSeat {
private String id; private String id;

View File

@ -1,4 +1,4 @@
package org.hibernate.test.orphan.elementcollection; package org.hibernate.orm.test.orphan.elementcollection;
import java.util.Set; import java.util.Set;

View File

@ -1,4 +1,4 @@
package org.hibernate.test.orphan.elementcollection; package org.hibernate.orm.test.orphan.elementcollection;
public class StudentEnrolledClass { public class StudentEnrolledClass {
private EnrollableClass enrolledClass; private EnrollableClass enrolledClass;

View File

@ -5,7 +5,7 @@
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping> <hibernate-mapping>
<class name="org.hibernate.test.orphan.elementcollection.Student" table="STUDENT" lazy="true" > <class name="org.hibernate.orm.test.orphan.elementcollection.Student" table="STUDENT" lazy="true" >
<id name="id" column="id"> <id name="id" column="id">
<generator class="assigned"/> <generator class="assigned"/>
@ -17,17 +17,17 @@
<set name="enrolledClasses" table="STUDENT_CLASS" lazy="true" fetch="subselect" cascade="all"> <set name="enrolledClasses" table="STUDENT_CLASS" lazy="true" fetch="subselect" cascade="all">
<key column="STUDENT_ID" /> <key column="STUDENT_ID" />
<composite-element class="org.hibernate.test.orphan.elementcollection.StudentEnrolledClass" > <composite-element class="org.hibernate.orm.test.orphan.elementcollection.StudentEnrolledClass" >
<many-to-one class="org.hibernate.test.orphan.elementcollection.EnrollableClass" name="enrolledClass" <many-to-one class="org.hibernate.orm.test.orphan.elementcollection.EnrollableClass" name="enrolledClass"
column="ENROLLED_CLASS_ID" not-null="true" lazy="false" unique="false" cascade="none" /> column="ENROLLED_CLASS_ID" not-null="true" lazy="false" unique="false" cascade="none" />
<property name="classStartTime" column="STARTTIME" /> <property name="classStartTime" column="STARTTIME" />
<many-to-one class="org.hibernate.test.orphan.elementcollection.EnrolledClassSeat" name="seat" <many-to-one class="org.hibernate.orm.test.orphan.elementcollection.EnrolledClassSeat" name="seat"
column="ENROLLED_CLASS_SEAT_ID" not-null="true" lazy="false" unique="true" cascade="all-delete-orphan" /> column="ENROLLED_CLASS_SEAT_ID" not-null="true" lazy="false" unique="true" cascade="all-delete-orphan" />
</composite-element> </composite-element>
</set> </set>
</class> </class>
<class name="org.hibernate.test.orphan.elementcollection.EnrollableClass" lazy="true" > <class name="org.hibernate.orm.test.orphan.elementcollection.EnrollableClass" lazy="true" >
<id name="id" column="id"> <id name="id" column="id">
<generator class="assigned"/> <generator class="assigned"/>
@ -35,7 +35,7 @@
<property name="name" column="NAME" /> <property name="name" column="NAME" />
</class> </class>
<class name="org.hibernate.test.orphan.elementcollection.EnrolledClassSeat" lazy="true" > <class name="org.hibernate.orm.test.orphan.elementcollection.EnrolledClassSeat" lazy="true" >
<id name="id" column="id"> <id name="id" column="id">
<generator class="assigned"/> <generator class="assigned"/>
</id> </id>

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.orphan.manytomany; package org.hibernate.orm.test.orphan.manytomany;
import java.io.Serializable; import java.io.Serializable;

View File

@ -4,29 +4,41 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.orphan.manytomany; package org.hibernate.orm.test.orphan.manytomany;
import java.util.List; import java.util.List;
import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.CriteriaQuery;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.orm.junit.DomainModel;
import org.junit.Test; import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
public class ManyToManyOrphanTest extends BaseCoreFunctionalTestCase { @DomainModel(
xmlMappings = "org/hibernate/orm/test/orphan/manytomany/UserGroup.hbm.xml"
)
@SessionFactory
public class ManyToManyOrphanTest {
@Override @AfterEach
protected String[] getMappings() { public void tearDown(SessionFactoryScope scope) {
return new String[] { "orphan/manytomany/UserGroup.hbm.xml" }; scope.inTransaction(
session -> {
session.createQuery( "delete from User" ).executeUpdate();
session.createQuery( "delete from Group" ).executeUpdate();
}
);
} }
@Test @Test
@TestForIssue(jiraKey = "HHH-8749") @TestForIssue(jiraKey = "HHH-8749")
public void testManyToManyWithCascadeDeleteOrphan() { public void testManyToManyWithCascadeDeleteOrphan(SessionFactoryScope scope) {
inTransaction( scope.inTransaction(
s -> { s -> {
User bob = new User( "bob", "jboss" ); User bob = new User( "bob", "jboss" );
Group seam = new Group( "seam", "jboss" ); Group seam = new Group( "seam", "jboss" );
@ -41,7 +53,7 @@ public class ManyToManyOrphanTest extends BaseCoreFunctionalTestCase {
} }
); );
inTransaction( scope.inTransaction(
s -> { s -> {
User b = s.get( User.class, "bob" ); User b = s.get( User.class, "bob" );
assertEquals( 2, b.getGroups().size() ); assertEquals( 2, b.getGroups().size() );
@ -52,7 +64,7 @@ public class ManyToManyOrphanTest extends BaseCoreFunctionalTestCase {
} }
); );
inTransaction( scope.inTransaction(
s -> { s -> {
User b = s.get( User.class, "bob" ); User b = s.get( User.class, "bob" );
assertEquals( 2, b.getGroups().size() ); assertEquals( 2, b.getGroups().size() );
@ -62,7 +74,7 @@ public class ManyToManyOrphanTest extends BaseCoreFunctionalTestCase {
} }
); );
inTransaction( scope.inTransaction(
s -> { s -> {
User b = s.get( User.class, "bob" ); User b = s.get( User.class, "bob" );
assertEquals( 1, b.getGroups().size() ); assertEquals( 1, b.getGroups().size() );
@ -70,7 +82,7 @@ public class ManyToManyOrphanTest extends BaseCoreFunctionalTestCase {
); );
// Verify orphan group was deleted // Verify orphan group was deleted
inTransaction( scope.inTransaction(
s -> { s -> {
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Group> criteria = criteriaBuilder.createQuery( Group.class ); CriteriaQuery<Group> criteria = criteriaBuilder.createQuery( Group.class );

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.orphan.manytomany; package org.hibernate.orm.test.orphan.manytomany;
import java.io.Serializable; import java.io.Serializable;
import java.util.HashMap; import java.util.HashMap;

View File

@ -9,7 +9,7 @@
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.test.orphan.manytomany"> <hibernate-mapping package="org.hibernate.orm.test.orphan.manytomany">
<class name="User" table="`User`"> <class name="User" table="`User`">
<id name="name" length="32" /> <id name="name" length="32" />

View File

@ -4,36 +4,52 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * 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; package org.hibernate.orm.test.orphan.one2one;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.OneToOne; import javax.persistence.OneToOne;
import org.junit.Test;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.Assert.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.Assert.assertNull;
/** /**
* @author Chris Cranford * @author Chris Cranford
*/ */
@TestForIssue(jiraKey = "HHH-9663") @TestForIssue(jiraKey = "HHH-9663")
public class OneToOneEagerNonOptionalOrphanRemovalTest extends BaseCoreFunctionalTestCase { @DomainModel(
@Override annotatedClasses = {
protected Class<?>[] getAnnotatedClasses() { OneToOneEagerNonOptionalOrphanRemovalTest.Car.class,
return new Class<?>[] { Car.class, PaintColor.class, Engine.class }; OneToOneEagerNonOptionalOrphanRemovalTest.PaintColor.class,
OneToOneEagerNonOptionalOrphanRemovalTest.Engine.class
}
)
@SessionFactory
public class OneToOneEagerNonOptionalOrphanRemovalTest {
@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete from Car" ).executeUpdate();
session.createQuery( "delete from Engine" ).executeUpdate();
session.createQuery( "delete from PaintColor" ).executeUpdate();
}
);
} }
@Test @Test
public void testOneToOneLazyNonOptionalOrphanRemoval() { public void testOneToOneLazyNonOptionalOrphanRemoval(SessionFactoryScope scope) {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Initialize the data // Initialize the data
doInHibernate( this::sessionFactory, session -> { scope.inTransaction( session -> {
final PaintColor color = new PaintColor( 1, "Red" ); final PaintColor color = new PaintColor( 1, "Red" );
final Engine engine1 = new Engine( 1, 275 ); final Engine engine1 = new Engine( 1, 275 );
final Engine engine2 = new Engine( 2, 295 ); final Engine engine2 = new Engine( 2, 295 );
@ -47,14 +63,14 @@ public class OneToOneEagerNonOptionalOrphanRemovalTest extends BaseCoreFunctiona
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Test orphan removal for unidirectional relationship // Test orphan removal for unidirectional relationship
doInHibernate( this::sessionFactory, session -> { scope.inTransaction( session -> {
final Car car = session.find( Car.class, 1 ); final Car car = session.find( Car.class, 1 );
final Engine engine = session.find( Engine.class, 2 ); final Engine engine = session.find( Engine.class, 2 );
car.setEngine( engine ); car.setEngine( engine );
session.update( car ); session.update( car );
} ); } );
doInHibernate( this::sessionFactory, session -> { scope.inTransaction( session -> {
final Car car = session.find( Car.class, 1 ); final Car car = session.find( Car.class, 1 );
assertNotNull( car.getEngine() ); assertNotNull( car.getEngine() );
@ -64,7 +80,7 @@ public class OneToOneEagerNonOptionalOrphanRemovalTest extends BaseCoreFunctiona
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Test orphan removal for bidirectional relationship // Test orphan removal for bidirectional relationship
doInHibernate( this::sessionFactory, session -> { scope.inTransaction( session -> {
final PaintColor color = new PaintColor( 2, "Blue" ); final PaintColor color = new PaintColor( 2, "Blue" );
final Car car = session.find( Car.class, 1 ); final Car car = session.find( Car.class, 1 );
car.setPaintColor( color ); car.setPaintColor( color );
@ -72,7 +88,7 @@ public class OneToOneEagerNonOptionalOrphanRemovalTest extends BaseCoreFunctiona
session.update( car ); session.update( car );
} ); } );
doInHibernate( this::sessionFactory, session -> { scope.inTransaction( session -> {
final Car car = session.find( Car.class, 1 ); final Car car = session.find( Car.class, 1 );
assertNotNull( car.getPaintColor() ); assertNotNull( car.getPaintColor() );
@ -86,6 +102,8 @@ public class OneToOneEagerNonOptionalOrphanRemovalTest extends BaseCoreFunctiona
@Id @Id
private Integer id; private Integer id;
private String model;
// represents a bidirectional one-to-one // represents a bidirectional one-to-one
@OneToOne(orphanRemoval = true, optional = false) @OneToOne(orphanRemoval = true, optional = false)
private PaintColor paintColor; private PaintColor paintColor;

View File

@ -4,36 +4,52 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * 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; package org.hibernate.orm.test.orphan.one2one;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType; import javax.persistence.FetchType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.OneToOne; import javax.persistence.OneToOne;
import org.junit.Test;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.Assert.assertNull;
/** /**
* @author Chris Cranford * @author Chris Cranford
*/ */
@TestForIssue(jiraKey = "HHH-9663") @TestForIssue(jiraKey = "HHH-9663")
public class OneToOneEagerOrphanRemovalTest extends BaseCoreFunctionalTestCase { @DomainModel(
@Override annotatedClasses = {
protected Class<?>[] getAnnotatedClasses() { OneToOneEagerOrphanRemovalTest.Car.class,
return new Class<?>[] { Car.class, PaintColor.class, Engine.class }; OneToOneEagerOrphanRemovalTest.PaintColor.class,
OneToOneEagerOrphanRemovalTest.Engine.class
}
)
@SessionFactory
public class OneToOneEagerOrphanRemovalTest {
@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete from Car" ).executeUpdate();
session.createQuery( "delete from Engine" ).executeUpdate();
session.createQuery( "delete from PaintColor" ).executeUpdate();
}
);
} }
@Test @Test
public void testOneToOneEagerOrphanRemoval() { public void testOneToOneEagerOrphanRemoval(SessionFactoryScope scope) {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Initialize the data // Initialize the data
doInHibernate( this::sessionFactory, session -> { scope.inTransaction( session -> {
final PaintColor color = new PaintColor( 1, "Red" ); final PaintColor color = new PaintColor( 1, "Red" );
final Engine engine = new Engine( 1, 275 ); final Engine engine = new Engine( 1, 275 );
final Car car = new Car( 1, engine, color ); final Car car = new Car( 1, engine, color );
@ -45,13 +61,13 @@ public class OneToOneEagerOrphanRemovalTest extends BaseCoreFunctionalTestCase {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Test orphan removal for unidirectional relationship // Test orphan removal for unidirectional relationship
doInHibernate( this::sessionFactory, session -> { scope.inTransaction( session -> {
final Car car = session.find( Car.class, 1 ); final Car car = session.find( Car.class, 1 );
car.setEngine( null ); car.setEngine( null );
session.update( car ); session.update( car );
} ); } );
doInHibernate( this::sessionFactory, session -> { scope.inTransaction( session -> {
final Car car = session.find( Car.class, 1 ); final Car car = session.find( Car.class, 1 );
assertNull( car.getEngine() ); assertNull( car.getEngine() );
@ -61,13 +77,13 @@ public class OneToOneEagerOrphanRemovalTest extends BaseCoreFunctionalTestCase {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Test orphan removal for bidirectional relationship // Test orphan removal for bidirectional relationship
doInHibernate( this::sessionFactory, session -> { scope.inTransaction( session -> {
final Car car = session.find( Car.class, 1 ); final Car car = session.find( Car.class, 1 );
car.setPaintColor( null ); car.setPaintColor( null );
session.update( car ); session.update( car );
} ); } );
doInHibernate( this::sessionFactory, session -> { scope.inTransaction( session -> {
final Car car = session.find( Car.class, 1 ); final Car car = session.find( Car.class, 1 );
assertNull( car.getPaintColor() ); assertNull( car.getPaintColor() );
@ -81,6 +97,8 @@ public class OneToOneEagerOrphanRemovalTest extends BaseCoreFunctionalTestCase {
@Id @Id
private Integer id; private Integer id;
private String model;
// represents a bidirectional one-to-one // represents a bidirectional one-to-one
@OneToOne(orphanRemoval = true, fetch = FetchType.EAGER) @OneToOne(orphanRemoval = true, fetch = FetchType.EAGER)
private PaintColor paintColor; private PaintColor paintColor;

View File

@ -4,37 +4,53 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * 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; package org.hibernate.orm.test.orphan.one2one;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType; import javax.persistence.FetchType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.OneToOne; import javax.persistence.OneToOne;
import org.junit.Test;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.Assert.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.Assert.assertNull;
/** /**
* @author Chris Cranford * @author Chris Cranford
*/ */
@TestForIssue(jiraKey = "HHH-9663") @TestForIssue(jiraKey = "HHH-9663")
public class OneToOneLazyNonOptionalOrphanRemovalTest extends BaseCoreFunctionalTestCase { @DomainModel(
@Override annotatedClasses = {
protected Class<?>[] getAnnotatedClasses() { OneToOneLazyNonOptionalOrphanRemovalTest.Car.class,
return new Class<?>[] { Car.class, PaintColor.class, Engine.class }; OneToOneLazyNonOptionalOrphanRemovalTest.PaintColor.class,
OneToOneLazyNonOptionalOrphanRemovalTest.Engine.class
}
)
@SessionFactory
public class OneToOneLazyNonOptionalOrphanRemovalTest {
@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete from Car" ).executeUpdate();
session.createQuery( "delete from Engine" ).executeUpdate();
session.createQuery( "delete from PaintColor" ).executeUpdate();
}
);
} }
@Test @Test
public void testOneToOneLazyNonOptionalOrphanRemoval() { public void testOneToOneLazyNonOptionalOrphanRemoval(SessionFactoryScope scope) {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Initialize the data // Initialize the data
doInHibernate( this::sessionFactory, session -> { scope.inTransaction( session -> {
final PaintColor color = new PaintColor( 1, "Red" ); final PaintColor color = new PaintColor( 1, "Red" );
final Engine engine1 = new Engine( 1, 275 ); final Engine engine1 = new Engine( 1, 275 );
final Engine engine2 = new Engine( 2, 295 ); final Engine engine2 = new Engine( 2, 295 );
@ -48,14 +64,14 @@ public class OneToOneLazyNonOptionalOrphanRemovalTest extends BaseCoreFunctional
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Test orphan removal for unidirectional relationship // Test orphan removal for unidirectional relationship
doInHibernate( this::sessionFactory, session -> { scope.inTransaction( session -> {
final Car car = session.find( Car.class, 1 ); final Car car = session.find( Car.class, 1 );
final Engine engine = session.find( Engine.class, 2 ); final Engine engine = session.find( Engine.class, 2 );
car.setEngine( engine ); car.setEngine( engine );
session.update( car ); session.update( car );
} ); } );
doInHibernate( this::sessionFactory, session -> { scope.inTransaction( session -> {
final Car car = session.find( Car.class, 1 ); final Car car = session.find( Car.class, 1 );
assertNotNull( car.getEngine() ); assertNotNull( car.getEngine() );
@ -65,7 +81,7 @@ public class OneToOneLazyNonOptionalOrphanRemovalTest extends BaseCoreFunctional
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Test orphan removal for bidirectional relationship // Test orphan removal for bidirectional relationship
doInHibernate( this::sessionFactory, session -> { scope.inTransaction( session -> {
final PaintColor color = new PaintColor( 2, "Blue" ); final PaintColor color = new PaintColor( 2, "Blue" );
final Car car = session.find( Car.class, 1 ); final Car car = session.find( Car.class, 1 );
car.setPaintColor( color ); car.setPaintColor( color );
@ -73,7 +89,7 @@ public class OneToOneLazyNonOptionalOrphanRemovalTest extends BaseCoreFunctional
session.update( car ); session.update( car );
} ); } );
doInHibernate( this::sessionFactory, session -> { scope.inTransaction( session -> {
final Car car = session.find( Car.class, 1 ); final Car car = session.find( Car.class, 1 );
assertNotNull( car.getPaintColor() ); assertNotNull( car.getPaintColor() );
@ -87,6 +103,8 @@ public class OneToOneLazyNonOptionalOrphanRemovalTest extends BaseCoreFunctional
@Id @Id
private Integer id; private Integer id;
private String model;
// represents a bidirectional one-to-one // represents a bidirectional one-to-one
@OneToOne(orphanRemoval = true, fetch = FetchType.LAZY, optional = false) @OneToOne(orphanRemoval = true, fetch = FetchType.LAZY, optional = false)
private PaintColor paintColor; private PaintColor paintColor;

View File

@ -4,36 +4,52 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * 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; package org.hibernate.orm.test.orphan.one2one;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType; import javax.persistence.FetchType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.OneToOne; import javax.persistence.OneToOne;
import org.junit.Test;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.Assert.assertNull;
/** /**
* @author Chris Cranford * @author Chris Cranford
*/ */
@TestForIssue(jiraKey = "HHH-9663") @TestForIssue(jiraKey = "HHH-9663")
public class OneToOneLazyOrphanRemovalTest extends BaseCoreFunctionalTestCase { @DomainModel(
@Override annotatedClasses = {
protected Class<?>[] getAnnotatedClasses() { OneToOneLazyOrphanRemovalTest.Car.class,
return new Class<?>[] { Car.class, PaintColor.class, Engine.class }; OneToOneLazyOrphanRemovalTest.PaintColor.class,
OneToOneLazyOrphanRemovalTest.Engine.class
}
)
@SessionFactory
public class OneToOneLazyOrphanRemovalTest {
@AfterEach
public void tearDown(SessionFactoryScope scope){
scope.inTransaction(
session -> {
session.createQuery( "delete from PaintColor" ).executeUpdate();
session.createQuery( "delete from Engine" ).executeUpdate();
session.createQuery( "delete from Car" ).executeUpdate();
}
);
} }
@Test @Test
public void testOneToOneLazyOrphanRemoval() { public void testOneToOneLazyOrphanRemoval(SessionFactoryScope scope) {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Initialize the data // Initialize the data
doInHibernate( this::sessionFactory, session -> { scope.inTransaction( session -> {
final PaintColor color = new PaintColor( 1, "Red" ); final PaintColor color = new PaintColor( 1, "Red" );
final Engine engine = new Engine( 1, 275 ); final Engine engine = new Engine( 1, 275 );
final Car car = new Car( 1, engine, color ); final Car car = new Car( 1, engine, color );
@ -45,13 +61,13 @@ public class OneToOneLazyOrphanRemovalTest extends BaseCoreFunctionalTestCase {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Test orphan removal for unidirectional relationship // Test orphan removal for unidirectional relationship
doInHibernate( this::sessionFactory, session -> { scope.inTransaction( session -> {
final Car car = session.find( Car.class, 1 ); final Car car = session.find( Car.class, 1 );
car.setEngine( null ); car.setEngine( null );
session.update( car ); session.update( car );
} ); } );
doInHibernate( this::sessionFactory, session -> { scope.inTransaction( session -> {
final Car car = session.find( Car.class, 1 ); final Car car = session.find( Car.class, 1 );
assertNull( car.getEngine() ); assertNull( car.getEngine() );
@ -61,13 +77,13 @@ public class OneToOneLazyOrphanRemovalTest extends BaseCoreFunctionalTestCase {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Test orphan removal for bidirectional relationship // Test orphan removal for bidirectional relationship
doInHibernate( this::sessionFactory, session -> { scope.inTransaction( session -> {
final Car car = session.find( Car.class, 1 ); final Car car = session.find( Car.class, 1 );
car.setPaintColor( null ); car.setPaintColor( null );
session.update( car ); session.update( car );
} ); } );
doInHibernate( this::sessionFactory, session -> { scope.inTransaction( session -> {
final Car car = session.find( Car.class, 1 ); final Car car = session.find( Car.class, 1 );
assertNull( car.getPaintColor() ); assertNull( car.getPaintColor() );

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * 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; package org.hibernate.orm.test.orphan.one2one;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Entity; import javax.persistence.Entity;
@ -15,36 +15,46 @@ import javax.persistence.Id;
import javax.persistence.OneToOne; import javax.persistence.OneToOne;
import org.hibernate.Hibernate; import org.hibernate.Hibernate;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.junit.Test;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.Assert.assertNotNull;
/** /**
* A test that shows orphan-removal is triggered when an entity has a lazy one-to-one * A test that shows orphan-removal is triggered when an entity has a lazy one-to-one
* mapping with property-based annotations and the getter method unwraps the proxy * mapping with property-based annotations and the getter method unwraps the proxy
* inline during invocation leading to constraint violation due to attempted removal * inline during invocation leading to constraint violation due to attempted removal
* of the associated entity. * of the associated entity.
* * <p>
* This test case documents old behavior so that it can be preserved but allowing * This test case documents old behavior so that it can be preserved but allowing
* us to also maintain the fix for {@code HHH-9663}. * us to also maintain the fix for {@code HHH-9663}.
* *
* @author Chris Cranford * @author Chris Cranford
*/ */
@TestForIssue(jiraKey = "HHH-11965") @TestForIssue(jiraKey = "HHH-11965")
public class OneToOneProxyOrphanRemovalTest extends BaseEntityManagerFunctionalTestCase { @Jpa(
annotatedClasses = { OneToOneProxyOrphanRemovalTest.Child.class, OneToOneProxyOrphanRemovalTest.Parent.class }
)
public class OneToOneProxyOrphanRemovalTest {
@Override @AfterEach
protected Class<?>[] getAnnotatedClasses() { public void tearDown(EntityManagerFactoryScope scope){
return new Class<?>[] { Child.class, Parent.class }; scope.inTransaction(
entityManager -> {
entityManager.createQuery( "delete from Parent" ).executeUpdate();
entityManager.createQuery( "delete from Child" ).executeUpdate();
}
);
} }
@Test @Test
public void testUnproxyOneToOneWithCascade() { public void testUnproxyOneToOneWithCascade(EntityManagerFactoryScope scope) {
Integer pId = doInJPA( this::entityManagerFactory, entityManager -> { Integer pId = scope.fromTransaction(
entityManager -> {
Parent p = new Parent(); Parent p = new Parent();
p.setChild( new Child() ); p.setChild( new Child() );
entityManager.persist( p ); entityManager.persist( p );
@ -61,15 +71,18 @@ public class OneToOneProxyOrphanRemovalTest extends BaseEntityManagerFunctionalT
// //
// In short, no cascade of orphan-removal should be invoked for this scenario, thus avoiding the raised // In short, no cascade of orphan-removal should be invoked for this scenario, thus avoiding the raised
// constraint violation exception. // constraint violation exception.
doInJPA( this::entityManagerFactory, entityManager -> { scope.inTransaction(
assertNotNull( entityManager.find( Parent.class, pId ) ); entityManager ->
} ); assertNotNull( entityManager.find( Parent.class, pId ) )
);
} }
@Entity(name = "Child") @Entity(name = "Child")
public static class Child { public static class Child {
private Integer id; private Integer id;
private String name;
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() { public Integer getId() {
@ -84,6 +97,9 @@ public class OneToOneProxyOrphanRemovalTest extends BaseEntityManagerFunctionalT
@Entity(name = "Parent") @Entity(name = "Parent")
public static class Parent { public static class Parent {
private Integer id; private Integer id;
private String name;
private Child child; private Child child;
@Id @Id
@ -106,5 +122,4 @@ public class OneToOneProxyOrphanRemovalTest extends BaseEntityManagerFunctionalT
} }
} }
} }

View File

@ -0,0 +1,112 @@
/*
* 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.orm.test.orphan.one2one.fk.bidirectional;
import java.util.List;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
/**
* @author Steve Ebersole
*/
@DomainModel(
xmlMappings = "org/hibernate/orm/test/orphan/one2one/fk/bidirectional/Mapping.hbm.xml"
)
@SessionFactory
public class DeleteOneToOneOrphansTest {
@BeforeEach
public void createData(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Employee emp = new Employee();
emp.setInfo( new EmployeeInfo( emp ) );
session.save( emp );
}
);
}
@AfterEach
public void cleanupData(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete EmployeeInfo" ).executeUpdate();
session.createQuery( "delete Employee" ).executeUpdate();
}
);
}
@Test
public void testOrphanedWhileManaged(SessionFactoryScope scope) {
Employee e = scope.fromTransaction(
session -> {
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
Employee emp = (Employee) results.get( 0 );
assertNotNull( emp.getInfo() );
emp.setInfo( null );
return emp;
}
);
scope.inTransaction(
session -> {
Employee emp = session.get( Employee.class, e.getId() );
assertNull( emp.getInfo() );
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-6484")
public void testReplacedWhileManaged(SessionFactoryScope scope) {
Employee e = scope.fromTransaction(
session -> {
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
Employee emp = (Employee) results.get( 0 );
assertNotNull( emp.getInfo() );
// Replace with a new EmployeeInfo instance
emp.setInfo( new EmployeeInfo( emp ) );
return emp;
}
);
scope.inTransaction(
session -> {
Employee emp = session.get( Employee.class, e.getId() );
assertNotNull( emp.getInfo() );
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
}
);
}
}

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * 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.pk.unidirectional; package org.hibernate.orm.test.orphan.one2one.fk.bidirectional;
/** /**

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * 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.fk.bidirectional; package org.hibernate.orm.test.orphan.one2one.fk.bidirectional;
/** /**

View File

@ -10,7 +10,7 @@
'-//Hibernate/Hibernate Mapping DTD 3.0//EN' '-//Hibernate/Hibernate Mapping DTD 3.0//EN'
'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'> 'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'>
<hibernate-mapping package="org.hibernate.test.orphan.one2one.fk.bidirectional" > <hibernate-mapping package="org.hibernate.orm.test.orphan.one2one.fk.bidirectional" >
<class name="Employee"> <class name="Employee">
<id name="id" type="long" column="id"> <id name="id" type="long" column="id">

View File

@ -0,0 +1,258 @@
/*
* 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.orm.test.orphan.one2one.fk.bidirectional.multilevelcascade;
import java.util.List;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
/**
* @author Steve Ebersole
* @author Gail Badner
*/
@DomainModel(
annotatedClasses = {
Preisregelung.class,
Tranche.class,
Tranchenmodell.class,
X.class,
Y.class
}
)
@SessionFactory
public class DeleteMultiLevelOrphansTest {
@BeforeEach
public void createData(SessionFactoryScope scope) {
Preisregelung preisregelung = new Preisregelung();
Tranchenmodell tranchenmodell = new Tranchenmodell();
X x = new X();
Tranche tranche1 = new Tranche();
Y y = new Y();
Tranche tranche2 = new Tranche();
preisregelung.setTranchenmodell( tranchenmodell );
tranchenmodell.setPreisregelung( preisregelung );
tranchenmodell.setX( x );
x.setTranchenmodell( tranchenmodell );
tranchenmodell.getTranchen().add( tranche1 );
tranche1.setTranchenmodell( tranchenmodell );
tranchenmodell.getTranchen().add( tranche2 );
tranche2.setTranchenmodell( tranchenmodell );
tranche1.setY( y );
y.setTranche( tranche1 );
scope.inTransaction(
session ->
session.save( preisregelung )
);
}
@AfterEach
public void cleanupData(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete Tranche" ).executeUpdate();
session.createQuery( "delete Tranchenmodell" ).executeUpdate();
session.createQuery( "delete Preisregelung" ).executeUpdate();
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-9091")
public void testDirectAssociationOrphanedWhileManaged(SessionFactoryScope scope) {
Preisregelung p = scope.fromTransaction(
session -> {
List results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
Preisregelung preisregelung = (Preisregelung) results.get( 0 );
Tranchenmodell tranchenmodell = preisregelung.getTranchenmodell();
assertNotNull( tranchenmodell );
assertNotNull( tranchenmodell.getX() );
assertEquals( 2, tranchenmodell.getTranchen().size() );
assertNotNull( tranchenmodell.getTranchen().get( 0 ).getY() );
preisregelung.setTranchenmodell( null );
return preisregelung;
}
);
scope.inTransaction(
session -> {
Preisregelung preisregelung = session.get( Preisregelung.class, p.getId() );
assertNull( preisregelung.getTranchenmodell() );
List results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Tranche" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from X" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Y" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-9091")
public void testReplacedDirectAssociationWhileManaged(SessionFactoryScope scope) {
// Create a new Tranchenmodell with new direct and nested associations
Tranchenmodell tranchenmodellNew = new Tranchenmodell();
X xNew = new X();
tranchenmodellNew.setX( xNew );
xNew.setTranchenmodell( tranchenmodellNew );
Tranche trancheNew = new Tranche();
tranchenmodellNew.getTranchen().add( trancheNew );
trancheNew.setTranchenmodell( tranchenmodellNew );
Y yNew = new Y();
trancheNew.setY( yNew );
yNew.setTranche( trancheNew );
scope.inTransaction(
session -> {
List results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
Preisregelung preisregelung = (Preisregelung) results.get( 0 );
Tranchenmodell tranchenmodell = preisregelung.getTranchenmodell();
assertNotNull( tranchenmodell );
assertNotNull( tranchenmodell.getX() );
assertEquals( 2, tranchenmodell.getTranchen().size() );
assertNotNull( tranchenmodell.getTranchen().get( 0 ).getY() );
// Replace with a new Tranchenmodell instance containing new direct and nested associations
preisregelung.setTranchenmodell( tranchenmodellNew );
tranchenmodellNew.setPreisregelung( preisregelung );
}
);
Tranchenmodell t = scope.fromTransaction(
session -> {
List results = session.createQuery( "from Tranche" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from X" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Y" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
Preisregelung preisregelung = (Preisregelung) results.get( 0 );
Tranchenmodell tranchenmodell = preisregelung.getTranchenmodell();
assertNotNull( tranchenmodell );
assertEquals( tranchenmodellNew.getId(), tranchenmodell.getId() );
assertNotNull( tranchenmodell.getX() );
assertEquals( xNew.getId(), tranchenmodell.getX().getId() );
assertEquals( 1, tranchenmodell.getTranchen().size() );
assertEquals( trancheNew.getId(), tranchenmodell.getTranchen().get( 0 ).getId() );
assertEquals( yNew.getId(), tranchenmodell.getTranchen().get( 0 ).getY().getId() );
// Replace with a new Tranchenmodell instance with no associations
Tranchenmodell tr = new Tranchenmodell();
preisregelung.setTranchenmodell( tr );
tr.setPreisregelung( preisregelung );
return tr;
}
);
scope.inTransaction(
session -> {
List results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 1, results.size() );
Tranchenmodell tranchenmodell = (Tranchenmodell) results.get( 0 );
assertEquals( t.getId(), tranchenmodell.getId() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
Preisregelung preisregelung = (Preisregelung) results.get( 0 );
assertEquals( tranchenmodell, preisregelung.getTranchenmodell() );
results = session.createQuery( "from Tranche" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from X" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Y" ).list();
assertEquals( 0, results.size() );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-9091")
public void testDirectAndNestedAssociationsOrphanedWhileManaged(SessionFactoryScope scope) {
Preisregelung p = scope.fromTransaction(
session -> {
List results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
Preisregelung preisregelung = (Preisregelung) results.get( 0 );
Tranchenmodell tranchenmodell = preisregelung.getTranchenmodell();
assertNotNull( tranchenmodell );
assertNotNull( tranchenmodell.getX() );
assertEquals( 2, tranchenmodell.getTranchen().size() );
assertNotNull( tranchenmodell.getTranchen().get( 0 ).getY() );
preisregelung.setTranchenmodell( null );
tranchenmodell.setX( null );
tranchenmodell.getTranchen().get( 0 ).setY( null );
return preisregelung;
}
);
scope.inTransaction(
session -> {
Preisregelung preisregelung = (Preisregelung) session.get(
Preisregelung.class,
p.getId()
);
assertNull( preisregelung.getTranchenmodell() );
List results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Tranche" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from X" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Y" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
}
);
}
}

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * 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.fk.bidirectional.multilevelcascade; package org.hibernate.orm.test.orphan.one2one.fk.bidirectional.multilevelcascade;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Entity; import javax.persistence.Entity;

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * 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.fk.bidirectional.multilevelcascade; package org.hibernate.orm.test.orphan.one2one.fk.bidirectional.multilevelcascade;
import javax.persistence.*; import javax.persistence.*;

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * 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.fk.bidirectional.multilevelcascade; package org.hibernate.orm.test.orphan.one2one.fk.bidirectional.multilevelcascade;
import javax.persistence.*; import javax.persistence.*;
import java.util.ArrayList; import java.util.ArrayList;

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * 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.fk.bidirectional.multilevelcascade; package org.hibernate.orm.test.orphan.one2one.fk.bidirectional.multilevelcascade;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType; import javax.persistence.FetchType;

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * 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.fk.bidirectional.multilevelcascade; package org.hibernate.orm.test.orphan.one2one.fk.bidirectional.multilevelcascade;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType; import javax.persistence.FetchType;

View File

@ -0,0 +1,111 @@
/*
* 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.orm.test.orphan.one2one.fk.composite;
import java.util.List;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
/**
* @author Steve Ebersole
*/
@DomainModel(
xmlMappings = "org/hibernate/orm/test/orphan/one2one/fk/composite/Mapping.hbm.xml"
)
@SessionFactory
public class DeleteOneToOneOrphansTest {
@BeforeEach
public void createData(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Employee emp = new Employee();
emp.setInfo( new EmployeeInfo( 1L, 1L ) );
session.save( emp );
}
);
}
@AfterEach
public void cleanupData(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete Employee" ).executeUpdate();
session.createQuery( "delete EmployeeInfo" ).executeUpdate();
}
);
}
@Test
public void testOrphanedWhileManaged(SessionFactoryScope scope) {
Employee e = scope.fromTransaction(
session -> {
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
Employee emp = (Employee) results.get( 0 );
assertNotNull( emp.getInfo() );
emp.setInfo( null );
return emp;
}
);
scope.inTransaction(
session -> {
Employee emp = session.get( Employee.class, e.getId() );
assertNull( emp.getInfo() );
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-6484")
public void testReplacedWhileManaged(SessionFactoryScope scope) {
Employee e = scope.fromTransaction(
session -> {
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
Employee emp = (Employee) results.get( 0 );
assertNotNull( emp.getInfo() );
// Replace with a new EmployeeInfo instance
emp.setInfo( new EmployeeInfo( 2L, 2L ) );
return emp;
}
);
scope.inTransaction(
session -> {
Employee emp = session.get( Employee.class, e.getId() );
assertNotNull( emp.getInfo() );
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
}
);
}
}

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * 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.fk.bidirectional; package org.hibernate.orm.test.orphan.one2one.fk.composite;
/** /**

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * 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.fk.composite; package org.hibernate.orm.test.orphan.one2one.fk.composite;
import java.io.Serializable; import java.io.Serializable;
/** /**

View File

@ -10,7 +10,7 @@
'-//Hibernate/Hibernate Mapping DTD 3.0//EN' '-//Hibernate/Hibernate Mapping DTD 3.0//EN'
'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'> 'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'>
<hibernate-mapping package="org.hibernate.test.orphan.one2one.fk.composite" > <hibernate-mapping package="org.hibernate.orm.test.orphan.one2one.fk.composite" >
<class name="Employee"> <class name="Employee">
<id name="id" type="long" column="id"> <id name="id" type="long" column="id">

View File

@ -0,0 +1,113 @@
/*
* 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.orm.test.orphan.one2one.fk.reversed.bidirectional;
import java.util.List;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
/**
* @author Steve Ebersole
*/
@DomainModel(
xmlMappings = "org/hibernate/orm/test/orphan/one2one/fk/reversed/bidirectional/Mapping.hbm.xml"
)
@SessionFactory
public class DeleteOneToOneOrphansTest {
@BeforeEach
public void createData(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Employee emp = new Employee();
emp.setInfo( new EmployeeInfo( emp ) );
session.save( emp );
}
);
}
@AfterEach
public void cleanupData(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete Employee" ).executeUpdate();
session.createQuery( "delete EmployeeInfo" ).executeUpdate();
}
);
}
@Test
public void testOrphanedWhileManaged(SessionFactoryScope scope) {
Employee e = scope.fromTransaction(
session -> {
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
Employee emp = (Employee) results.get( 0 );
assertNotNull( emp.getInfo() );
emp.setInfo( null );
return emp;
}
);
scope.inTransaction(
session -> {
Employee emp = session.get( Employee.class, e.getId() );
assertNull( emp.getInfo() );
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-6484")
public void testReplacedWhileManaged(SessionFactoryScope scope) {
Employee e = scope.fromTransaction(
session -> {
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
Employee emp = (Employee) results.get( 0 );
assertNotNull( emp.getInfo() );
// Replace with a new EmployeeInfo instance
emp.setInfo( new EmployeeInfo( emp ) );
return emp;
}
);
scope.inTransaction(
session -> {
Employee emp = session.get( Employee.class, e.getId() );
assertNotNull( emp.getInfo() );
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
}
);
}
}

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * 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.fk.reversed.bidirectional; package org.hibernate.orm.test.orphan.one2one.fk.reversed.bidirectional;
/** /**

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * 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.fk.reversed.bidirectional; package org.hibernate.orm.test.orphan.one2one.fk.reversed.bidirectional;
/** /**

View File

@ -10,7 +10,7 @@
'-//Hibernate/Hibernate Mapping DTD 3.0//EN' '-//Hibernate/Hibernate Mapping DTD 3.0//EN'
'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'> 'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'>
<hibernate-mapping package="org.hibernate.test.orphan.one2one.fk.reversed.bidirectional" > <hibernate-mapping package="org.hibernate.orm.test.orphan.one2one.fk.reversed.bidirectional" >
<class name="Employee"> <class name="Employee">
<id name="id" type="long" column="id"> <id name="id" type="long" column="id">

View File

@ -0,0 +1,143 @@
/*
* 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.orm.test.orphan.one2one.fk.reversed.bidirectional.multilevelcascade;
import java.util.List;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
/**
* @author Steve Ebersole
* @author Gail Badner
*/
@DomainModel(
annotatedClasses = {
Preisregelung.class,
Tranche.class,
Tranchenmodell.class
}
)
@SessionFactory
public class DeleteMultiLevelOrphansTest {
@BeforeEach
public void createData(SessionFactoryScope scope) {
Preisregelung preisregelung = new Preisregelung();
preisregelung.setId( 17960L );
Tranchenmodell tranchenmodell = new Tranchenmodell();
tranchenmodell.setId( 1951L );
Tranche tranche1 = new Tranche();
tranche1.setId( 1951L );
Tranche tranche2 = new Tranche();
tranche2.setId( 1952L );
preisregelung.setTranchenmodell( tranchenmodell );
tranchenmodell.setPreisregelung( preisregelung );
tranchenmodell.getTranchen().add( tranche1 );
tranche1.setTranchenmodell( tranchenmodell );
tranchenmodell.getTranchen().add( tranche2 );
tranche2.setTranchenmodell( tranchenmodell );
scope.inTransaction(
session ->
session.save( preisregelung )
);
}
@AfterEach
public void cleanupData(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete Tranche" ).executeUpdate();
session.createQuery( "delete Preisregelung" ).executeUpdate();
session.createQuery( "delete Tranchenmodell" ).executeUpdate();
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-9091")
public void testOrphanedWhileManaged(SessionFactoryScope scope) {
Preisregelung p = scope.fromTransaction(
session -> {
List results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
Preisregelung preisregelung = (Preisregelung) results.get( 0 );
assertNotNull( preisregelung.getTranchenmodell() );
preisregelung.setTranchenmodell( null );
return preisregelung;
}
);
scope.inTransaction(
session -> {
Preisregelung preisregelung = session.get( Preisregelung.class, p.getId() );
assertNull( preisregelung.getTranchenmodell() );
List results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-9091")
public void testReplacedWhileManaged(SessionFactoryScope scope) {
Tranchenmodell t = scope.fromTransaction(
session -> {
List results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
Preisregelung preisregelung = (Preisregelung) results.get( 0 );
assertNotNull( preisregelung.getTranchenmodell() );
// Replace with a new Tranchenmodell instance
Tranchenmodell tranchenmodellNew = new Tranchenmodell();
tranchenmodellNew.setId( 1952L );
preisregelung.setTranchenmodell( tranchenmodellNew );
tranchenmodellNew.setPreisregelung( preisregelung );
return tranchenmodellNew;
}
);
scope.inTransaction(
session -> {
List results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 1, results.size() );
Tranchenmodell tranchenmodellQueried = (Tranchenmodell) results.get( 0 );
assertEquals( t.getId(), tranchenmodellQueried.getId() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
Preisregelung preisregelung1Queried = (Preisregelung) results.get( 0 );
assertEquals( preisregelung1Queried.getTranchenmodell(), tranchenmodellQueried );
results = session.createQuery( "from Tranche" ).list();
assertEquals( 0, results.size() );
}
);
}
}

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * 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.fk.reversed.bidirectional.multilevelcascade; package org.hibernate.orm.test.orphan.one2one.fk.reversed.bidirectional.multilevelcascade;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Entity; import javax.persistence.Entity;

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * 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.fk.reversed.bidirectional.multilevelcascade; package org.hibernate.orm.test.orphan.one2one.fk.reversed.bidirectional.multilevelcascade;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType; import javax.persistence.FetchType;

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * 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.fk.reversed.bidirectional.multilevelcascade; package org.hibernate.orm.test.orphan.one2one.fk.reversed.bidirectional.multilevelcascade;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;

View File

@ -0,0 +1,156 @@
/*
* 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.orm.test.orphan.one2one.fk.reversed.unidirectional;
import java.util.List;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/**
* @author Steve Ebersole
*/
@DomainModel(
xmlMappings = "org/hibernate/orm/test/orphan/one2one/fk/reversed/unidirectional/Mapping.hbm.xml"
)
@SessionFactory
public class DeleteOneToOneOrphansTest {
@BeforeEach
public void createData(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Employee emp = new Employee();
emp.setInfo( new EmployeeInfo() );
session.save( emp );
}
);
}
@AfterEach
public void cleanupData(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete Employee" ).executeUpdate();
session.createQuery( "delete EmployeeInfo" ).executeUpdate();
}
);
}
@Test
public void testOrphanedWhileManaged(SessionFactoryScope scope) {
Employee e = scope.fromTransaction(
session -> {
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
Employee emp = (Employee) results.get( 0 );
assertNotNull( emp.getInfo() );
emp.setInfo( null );
return emp;
}
);
scope.inTransaction(
session -> {
Employee emp = session.get( Employee.class, e.getId() );
assertNull( emp.getInfo() );
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-5267")
public void testOrphanedWhileDetached(SessionFactoryScope scope) {
Employee e = scope.fromTransaction(
session -> {
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
Employee emp = (Employee) results.get( 0 );
assertNotNull( emp.getInfo() );
//only fails if the object is detached
return emp;
}
);
scope.inTransaction(
session -> {
e.setInfo( null );
//save using the new session (this used to work prior to 3.5.x)
session.saveOrUpdate( e );
}
);
scope.inTransaction(
session -> {
Employee emp = session.get( Employee.class, e.getId() );
assertNull( emp.getInfo() );
// TODO: If merge was used instead of saveOrUpdate, this would work.
// However, re-attachment does not currently support handling orphans.
// See HHH-3795
// results = session.createQuery( "from EmployeeInfo" ).list();
// assertEquals( 0, results.size() );
List results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-6484")
public void testReplacedWhileManaged(SessionFactoryScope scope) {
Employee e = scope.fromTransaction(
session -> {
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
Employee emp = (Employee) results.get( 0 );
assertNotNull( emp.getInfo() );
// Replace with a new EmployeeInfo instance
emp.setInfo( new EmployeeInfo() );
return emp;
}
);
scope.inTransaction(
session -> {
Employee emp = session.get( Employee.class, e.getId() );
assertNotNull( emp.getInfo() );
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
}
);
}
}

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * 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.fk.reversed.unidirectional; package org.hibernate.orm.test.orphan.one2one.fk.reversed.unidirectional;
/** /**

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * 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.fk.reversed.unidirectional; package org.hibernate.orm.test.orphan.one2one.fk.reversed.unidirectional;
/** /**

View File

@ -10,7 +10,7 @@
'-//Hibernate/Hibernate Mapping DTD 3.0//EN' '-//Hibernate/Hibernate Mapping DTD 3.0//EN'
'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'> 'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'>
<hibernate-mapping package="org.hibernate.test.orphan.one2one.fk.reversed.unidirectional" > <hibernate-mapping package="org.hibernate.orm.test.orphan.one2one.fk.reversed.unidirectional" >
<class name="Employee"> <class name="Employee">
<id name="id" type="long" column="id"> <id name="id" type="long" column="id">

View File

@ -0,0 +1,79 @@
/*
* 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.orm.test.orphan.one2one.pk.bidirectional;
import java.util.List;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
/**
* @author Steve Ebersole
*/
@DomainModel(
xmlMappings = "org/hibernate/orm/test/orphan/one2one/pk/bidirectional/Mapping.hbm.xml"
)
@SessionFactory
public class DeleteOneToOneOrphansTest {
@BeforeEach
public void createData(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Employee emp = new Employee();
emp.setInfo( new EmployeeInfo( emp ) );
session.save( emp );
}
);
}
@AfterEach
public void cleanupData(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete EmployeeInfo" ).executeUpdate();
session.createQuery( "delete Employee" ).executeUpdate();
}
);
}
@Test
public void testOrphanedWhileManaged(SessionFactoryScope scope) {
Employee e = scope.fromTransaction(
session -> {
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
Employee emp = (Employee) results.get( 0 );
assertNotNull( emp.getInfo() );
emp.setInfo( null );
return emp;
}
);
scope.inTransaction(
session -> {
Employee emp = (Employee) session.get( Employee.class, e.getId() );
assertNull( emp.getInfo() );
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
}
);
}
}

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * 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.pk.bidirectional; package org.hibernate.orm.test.orphan.one2one.pk.bidirectional;
/** /**

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * 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.pk.bidirectional; package org.hibernate.orm.test.orphan.one2one.pk.bidirectional;
/** /**

View File

@ -10,7 +10,7 @@
'-//Hibernate/Hibernate Mapping DTD 3.0//EN' '-//Hibernate/Hibernate Mapping DTD 3.0//EN'
'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'> 'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'>
<hibernate-mapping package="org.hibernate.test.orphan.one2one.pk.bidirectional" > <hibernate-mapping package="org.hibernate.orm.test.orphan.one2one.pk.bidirectional" >
<class name="Employee"> <class name="Employee">
<id name="id" type="long" column="id"> <id name="id" type="long" column="id">

View File

@ -0,0 +1,86 @@
/*
* 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.orm.test.orphan.one2one.pk.unidirectional;
import java.util.List;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
/**
* @author Steve Ebersole
*/
@DomainModel(
xmlMappings = "org/hibernate/orm/test/orphan/one2one/pk/unidirectional/Mapping.hbm.xml"
)
@SessionFactory
public class DeleteOneToOneOrphansTest {
@BeforeEach
public void createData(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Employee emp = new Employee();
session.save( emp );
emp.setInfo( new EmployeeInfo( emp.getId() ) );
}
);
}
@AfterEach
public void cleanupData(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete EmployeeInfo" ).executeUpdate();
session.createQuery( "delete Employee" ).executeUpdate();
}
);
}
@Test
public void testOrphanedWhileManaged(SessionFactoryScope scope) {
Employee e = scope.fromTransaction(
session -> {
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
Employee emp = (Employee) results.get( 0 );
assertNotNull( emp.getInfo() );
results = session.createQuery( "from Employee e, EmployeeInfo i where e.info = i" ).list();
assertEquals( 1, results.size() );
Object[] result = (Object[]) results.get( 0 );
emp = (Employee) result[0];
assertNotNull( result[1] );
assertSame( emp.getInfo(), result[1] );
emp.setInfo( null );
return emp;
}
);
scope.inTransaction(
session -> {
Employee emp = session.get( Employee.class, e.getId() );
assertNull( emp.getInfo() );
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
}
);
}
}

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * 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.fk.composite; package org.hibernate.orm.test.orphan.one2one.pk.unidirectional;
/** /**

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * 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.pk.unidirectional; package org.hibernate.orm.test.orphan.one2one.pk.unidirectional;
/** /**

View File

@ -10,7 +10,7 @@
'-//Hibernate/Hibernate Mapping DTD 3.0//EN' '-//Hibernate/Hibernate Mapping DTD 3.0//EN'
'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'> 'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'>
<hibernate-mapping package="org.hibernate.test.orphan.one2one.pk.unidirectional" > <hibernate-mapping package="org.hibernate.orm.test.orphan.one2one.pk.unidirectional" >
<class name="Employee"> <class name="Employee">
<id name="id" type="long" column="id"> <id name="id" type="long" column="id">

View File

@ -0,0 +1,158 @@
/*
* 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.orm.test.orphan.onetomany;
import java.util.List;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @author Gail Badnmer
*/
@DomainModel(
annotatedClasses = {
Product.class,
Feature.class
}
)
@SessionFactory
public class DeleteOneToManyOrphansTest {
@BeforeEach
public void createData(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Feature newFeature = new Feature();
newFeature.setName( "Feature 1" );
session.persist( newFeature );
Product product = new Product();
newFeature.setProduct( product );
product.getFeatures().add( newFeature );
session.persist( product );
}
);
}
@AfterEach
public void cleanupData(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete Feature" ).executeUpdate();
session.createQuery( "delete Product" ).executeUpdate();
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-9330")
public void testOrphanedWhileManaged(SessionFactoryScope scope) {
Product p = scope.fromTransaction(
session -> {
List results = session.createQuery( "from Feature" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Product" ).list();
assertEquals( 1, results.size() );
Product product = (Product) results.get( 0 );
assertEquals( 1, product.getFeatures().size() );
product.getFeatures().clear();
return product;
}
);
scope.inTransaction(
session -> {
Product product = ( session.get( Product.class, p.getId() ) );
assertEquals( 0, product.getFeatures().size() );
List results = session.createQuery( "from Feature" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Product" ).list();
assertEquals( 1, results.size() );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-9330")
public void testOrphanedWhileManagedMergeOwner(SessionFactoryScope scope) {
Product p = scope.fromTransaction(
session -> {
List results = session.createQuery( "from Feature" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Product" ).list();
assertEquals( 1, results.size() );
Product product = (Product) results.get( 0 );
assertEquals( 1, product.getFeatures().size() );
product.getFeatures().clear();
session.merge( product );
return product;
}
);
scope.inTransaction(
session -> {
Product product = session.get( Product.class, p.getId() );
assertEquals( 0, product.getFeatures().size() );
List results = session.createQuery( "from Feature" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Product" ).list();
assertEquals( 1, results.size() );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-9330")
public void testReplacedWhileManaged(SessionFactoryScope scope) {
Feature f = scope.fromTransaction(
session -> {
List results = session.createQuery( "from Feature" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Product" ).list();
assertEquals( 1, results.size() );
Product product = (Product) results.get( 0 );
assertEquals( 1, product.getFeatures().size() );
// Replace with a new Feature instance
product.getFeatures().remove( 0 );
Feature featureNew = new Feature();
featureNew.setName( "Feature 2" );
featureNew.setProduct( product );
product.getFeatures().add( featureNew );
session.persist( featureNew );
return featureNew;
}
);
scope.inTransaction(
session -> {
List results = session.createQuery( "from Feature" ).list();
assertEquals( 1, results.size() );
Feature featureQueried = (Feature) results.get( 0 );
assertEquals( f.getId(), featureQueried.getId() );
results = session.createQuery( "from Product" ).list();
assertEquals( 1, results.size() );
Product productQueried = (Product) results.get( 0 );
assertEquals( 1, productQueried.getFeatures().size() );
assertEquals( featureQueried, productQueried.getFeatures().get( 0 ) );
}
);
}
}

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.orphan.onetomany; package org.hibernate.orm.test.orphan.onetomany;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.orphan.onetomany; package org.hibernate.orm.test.orphan.onetomany;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;

View File

@ -10,7 +10,7 @@
'-//Hibernate/Hibernate Mapping DTD 3.0//EN' '-//Hibernate/Hibernate Mapping DTD 3.0//EN'
'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'> 'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'>
<hibernate-mapping package="org.hibernate.test.orphan" > <hibernate-mapping package="org.hibernate.orm.test.orphan" >
<class name="Mail" table="t_mail"> <class name="Mail" table="t_mail">

View File

@ -1,386 +0,0 @@
/*
* 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;
import org.junit.Test;
import org.hibernate.Hibernate;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.internal.util.SerializationHelper;
import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* @author Gavin King
*/
public class OrphanTest extends BaseCoreFunctionalTestCase {
public String[] getMappings() {
return new String[] { "orphan/Product.hbm.xml" };
}
@Test
@SuppressWarnings( {"unchecked"})
public void testOrphanDeleteOnDelete() {
Session session = openSession();
Transaction t = session.beginTransaction();
Product prod = new Product();
prod.setName("Widget");
Part part = new Part();
part.setName("Widge");
part.setDescription("part if a Widget");
prod.getParts().add(part);
Part part2 = new Part();
part2.setName("Get");
part2.setDescription("another part if a Widget");
prod.getParts().add(part2);
session.persist(prod);
session.flush();
prod.getParts().remove(part);
session.delete(prod);
t.commit();
session.close();
session = openSession();
t = session.beginTransaction();
assertNull( session.get(Part.class, "Widge") );
assertNull( session.get(Part.class, "Get") );
assertNull( session.get(Product.class, "Widget") );
t.commit();
session.close();
}
@Test
@SuppressWarnings( {"unchecked"})
public void testOrphanDeleteAfterPersist() {
Session session = openSession();
Transaction t = session.beginTransaction();
Product prod = new Product();
prod.setName("Widget");
Part part = new Part();
part.setName("Widge");
part.setDescription("part if a Widget");
prod.getParts().add(part);
Part part2 = new Part();
part2.setName("Get");
part2.setDescription("another part if a Widget");
prod.getParts().add(part2);
session.persist(prod);
prod.getParts().remove(part);
t.commit();
session.close();
session = openSession();
t = session.beginTransaction();
assertNull( session.get(Part.class, "Widge") );
assertNotNull( session.get(Part.class, "Get") );
session.delete( session.get(Product.class, "Widget") );
t.commit();
session.close();
}
@Test
@SuppressWarnings( {"unchecked"})
public void testOrphanDeleteAfterPersistAndFlush() {
Session session = openSession();
Transaction t = session.beginTransaction();
Product prod = new Product();
prod.setName("Widget");
Part part = new Part();
part.setName("Widge");
part.setDescription("part if a Widget");
prod.getParts().add(part);
Part part2 = new Part();
part2.setName("Get");
part2.setDescription("another part if a Widget");
prod.getParts().add(part2);
session.persist(prod);
session.flush();
prod.getParts().remove(part);
t.commit();
session.close();
session = openSession();
t = session.beginTransaction();
assertNull( session.get(Part.class, "Widge") );
assertNotNull( session.get(Part.class, "Get") );
session.delete( session.get(Product.class, "Widget") );
t.commit();
session.close();
}
@Test
@SuppressWarnings( {"unchecked"})
public void testOrphanDeleteAfterLock() {
Session session = openSession();
Transaction t = session.beginTransaction();
Product prod = new Product();
prod.setName("Widget");
Part part = new Part();
part.setName("Widge");
part.setDescription("part if a Widget");
prod.getParts().add(part);
Part part2 = new Part();
part2.setName("Get");
part2.setDescription("another part if a Widget");
prod.getParts().add(part2);
session.persist(prod);
t.commit();
session.close();
session = openSession();
t = session.beginTransaction();
session.lock( prod, LockMode.READ );
prod.getParts().remove(part);
t.commit();
session.close();
session = openSession();
t = session.beginTransaction();
assertNull( session.get(Part.class, "Widge") );
assertNotNull( session.get(Part.class, "Get") );
session.delete( session.get(Product.class, "Widget") );
t.commit();
session.close();
}
@Test
@SuppressWarnings( {"unchecked"})
public void testOrphanDeleteOnSaveOrUpdate() {
Session session = openSession();
Transaction t = session.beginTransaction();
Product prod = new Product();
prod.setName("Widget");
Part part = new Part();
part.setName("Widge");
part.setDescription("part if a Widget");
prod.getParts().add(part);
Part part2 = new Part();
part2.setName("Get");
part2.setDescription("another part if a Widget");
prod.getParts().add(part2);
session.persist(prod);
t.commit();
session.close();
prod.getParts().remove(part);
session = openSession();
t = session.beginTransaction();
session.saveOrUpdate(prod);
t.commit();
session.close();
session = openSession();
t = session.beginTransaction();
assertNull( session.get(Part.class, "Widge") );
assertNotNull( session.get(Part.class, "Get") );
session.delete( session.get(Product.class, "Widget") );
t.commit();
session.close();
}
@Test
@SuppressWarnings( {"unchecked"})
public void testOrphanDeleteOnSaveOrUpdateAfterSerialization() {
Session session = openSession();
Transaction t = session.beginTransaction();
Product prod = new Product();
prod.setName("Widget");
Part part = new Part();
part.setName("Widge");
part.setDescription("part if a Widget");
prod.getParts().add(part);
Part part2 = new Part();
part2.setName("Get");
part2.setDescription("another part if a Widget");
prod.getParts().add(part2);
session.persist(prod);
t.commit();
session.close();
prod.getParts().remove(part);
prod = (Product) SerializationHelper.clone( prod );
session = openSession();
t = session.beginTransaction();
session.saveOrUpdate(prod);
t.commit();
session.close();
session = openSession();
t = session.beginTransaction();
assertNull( session.get(Part.class, "Widge") );
assertNotNull( session.get(Part.class, "Get") );
session.delete( session.get(Product.class, "Widget") );
t.commit();
session.close();
}
@Test
@SuppressWarnings( {"unchecked"})
public void testOrphanDelete() {
Session session = openSession();
Transaction t = session.beginTransaction();
Product prod = new Product();
prod.setName("Widget");
Part part = new Part();
part.setName("Widge");
part.setDescription("part if a Widget");
prod.getParts().add(part);
Part part2 = new Part();
part2.setName("Get");
part2.setDescription("another part if a Widget");
prod.getParts().add(part2);
session.persist(prod);
t.commit();
session.close();
sessionFactory().getCache().evictEntityRegion( Product.class );
sessionFactory().getCache().evictEntityRegion( Part.class );
session = openSession();
t = session.beginTransaction();
prod = (Product) session.get(Product.class, "Widget");
assertTrue( Hibernate.isInitialized( prod.getParts() ) );
part = (Part) session.get(Part.class, "Widge");
prod.getParts().remove(part);
t.commit();
session.close();
session = openSession();
t = session.beginTransaction();
assertNull( session.get(Part.class, "Widge") );
assertNotNull( session.get(Part.class, "Get") );
session.delete( session.get(Product.class, "Widget") );
t.commit();
session.close();
}
@Test
@SuppressWarnings( {"unchecked"})
public void testOrphanDeleteOnMerge() {
Session session = openSession();
Transaction t = session.beginTransaction();
Product prod = new Product();
prod.setName("Widget");
Part part = new Part();
part.setName("Widge");
part.setDescription("part if a Widget");
prod.getParts().add(part);
Part part2 = new Part();
part2.setName("Get");
part2.setDescription("another part if a Widget");
prod.getParts().add(part2);
session.persist(prod);
t.commit();
session.close();
prod.getParts().remove(part);
session = openSession();
t = session.beginTransaction();
session.merge(prod);
t.commit();
session.close();
session = openSession();
t = session.beginTransaction();
assertNull( session.get(Part.class, "Widge") );
assertNotNull( session.get(Part.class, "Get") );
session.delete( session.get(Product.class, "Widget") );
t.commit();
session.close();
}
@Test
@SuppressWarnings( {"unchecked"})
public void testOrphanDeleteOnMergeRemoveElementMerge() {
Session session = openSession();
Transaction t = session.beginTransaction();
Product prod = new Product();
prod.setName( "Widget" );
Part part = new Part();
part.setName("Widge");
part.setDescription("part if a Widget");
prod.getParts().add(part);
session.persist(prod);
t.commit();
session.close();
session = openSession();
t = session.beginTransaction();
session.merge(prod);
prod.getParts().remove( part );
session.merge( prod );
t.commit();
session.close();
session = openSession();
t = session.beginTransaction();
assertNull( session.get( Part.class, "Widge" ) );
session.delete( session.get(Product.class, "Widget") );
t.commit();
session.close();
}
@Test
@SuppressWarnings( {"unchecked"})
@TestForIssue(jiraKey = "HHH-9171")
public void testOrphanDeleteOnAddElementMergeRemoveElementMerge() {
Session session = openSession();
Transaction t = session.beginTransaction();
Product prod = new Product();
prod.setName( "Widget" );
session.persist(prod);
t.commit();
session.close();
Part part = new Part();
part.setName("Widge");
part.setDescription("part if a Widget");
prod.getParts().add(part);
session = openSession();
t = session.beginTransaction();
session.merge(prod);
// In Section 2.9, Entity Relationships, the JPA 2.1 spec says:
// "If the entity being orphaned is a detached, new, or removed entity,
// the semantics of orphanRemoval do not apply."
// In other words, since part is a new entity, it will not be deleted when removed
// from prod.parts, even though cascade for the association includes "delete-orphan".
prod.getParts().remove(part);
session.merge( prod );
t.commit();
session.close();
session = openSession();
t = session.beginTransaction();
assertNotNull( session.get( Part.class, "Widge" ) );
session.delete( session.get(Product.class, "Widget") );
t.commit();
session.close();
}
}

View File

@ -7,24 +7,40 @@
package org.hibernate.test.orphan; package org.hibernate.test.orphan;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.orm.junit.DomainModel;
import org.junit.Test; import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
*/ */
@TestForIssue(jiraKey = "HHH-565") @TestForIssue(jiraKey = "HHH-565")
public class PropertyRefTest extends BaseCoreFunctionalTestCase { @DomainModel(
@Override xmlMappings = {
public String[] getMappings() { "org/hibernate/test/orphan/User.hbm.xml",
return new String[] { "orphan/User.hbm.xml", "orphan/Mail.hbm.xml" }; "org/hibernate/test/orphan/Mail.hbm.xml"
}
)
@SessionFactory
public class PropertyRefTest {
@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete from Mail" ).executeUpdate();
session.createQuery( "delete from User" ).executeUpdate();
}
);
} }
@Test @Test
public void testDeleteParentWithBidirOrphanDeleteCollectionBasedOnPropertyRef() { public void testDeleteParentWithBidirOrphanDeleteCollectionBasedOnPropertyRef(SessionFactoryScope scope) {
User user = new User( "test" ); User user = new User( "test" );
inTransaction( scope.inTransaction(
session -> { session -> {
user.addMail( "test" ); user.addMail( "test" );
user.addMail( "test" ); user.addMail( "test" );
@ -32,22 +48,21 @@ public class PropertyRefTest extends BaseCoreFunctionalTestCase {
} }
); );
inTransaction( scope.inTransaction(
session -> { session -> {
User u = session.load( User.class, user.getId() ); User u = session.load( User.class, user.getId() );
session.delete( u ); session.delete( u );
} }
); );
inTransaction( scope.inTransaction(
s -> { session -> {
session.createQuery( "delete from Mail where alias = :alias" ) session.createQuery( "delete from Mail where alias = :alias" )
.setParameter( "alias", "test" ) .setParameter( "alias", "test" )
.executeUpdate(); .executeUpdate();
session.createQuery( "delete from User where userid = :userid" ) session.createQuery( "delete from User where userid = :userid" )
.setParameter( "userid", "test" ) .setParameter( "userid", "test" )
.executeUpdate(); .executeUpdate();
} }
); );
} }

View File

@ -10,7 +10,7 @@
'-//Hibernate/Hibernate Mapping DTD 3.0//EN' '-//Hibernate/Hibernate Mapping DTD 3.0//EN'
'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'> 'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'>
<hibernate-mapping package="org.hibernate.test.orphan" > <hibernate-mapping package="org.hibernate.orm.test.orphan" >
<class name="User" table="t_user"> <class name="User" table="t_user">

View File

@ -1,53 +0,0 @@
package org.hibernate.test.orphan.elementcollection;
import java.util.Collections;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.testing.transaction.TransactionUtil;
import org.junit.Before;
import org.junit.Test;
@TestForIssue(jiraKey = "HHH-14597")
public class ElementCollectionOrphanTest extends BaseCoreFunctionalTestCase {
@Override
protected String[] getMappings() {
return new String[] { "orphan/elementcollection/student.hbm.xml" };
}
@Test
public void setCompositeElementTest() {
TransactionUtil.doInHibernate(
this::sessionFactory,
session -> {
EnrollableClass aClass = new EnrollableClass();
aClass.setId("123");
aClass.setName("Math");
session.save(aClass);
Student aStudent = new Student();
aStudent.setId("s1");
aStudent.setFirstName("John");
aStudent.setLastName("Smith");
EnrolledClassSeat seat = new EnrolledClassSeat();
seat.setId("seat1");
seat.setRow(10);
seat.setColumn(5);
StudentEnrolledClass enrClass = new StudentEnrolledClass();
enrClass.setEnrolledClass(aClass);
enrClass.setClassStartTime(130);
enrClass.setSeat(seat);
aStudent.setEnrolledClasses(Collections.singleton(enrClass));
session.save(aStudent);
}
);
}
}

View File

@ -1,112 +0,0 @@
/*
* 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.fk.bidirectional;
import java.util.List;
import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/**
* @author Steve Ebersole
*/
public class DeleteOneToOneOrphansTest extends BaseCoreFunctionalTestCase {
@Override
public String[] getMappings() {
return new String[] { "orphan/one2one/fk/bidirectional/Mapping.hbm.xml" };
}
private void createData() {
Session session = openSession();
session.beginTransaction();
Employee emp = new Employee();
emp.setInfo( new EmployeeInfo( emp ) );
session.save( emp );
session.getTransaction().commit();
session.close();
}
private void cleanupData() {
Session session = openSession();
session.beginTransaction();
session.createQuery( "delete EmployeeInfo" ).executeUpdate();
session.createQuery( "delete Employee" ).executeUpdate();
session.getTransaction().commit();
session.close();
}
@Test
public void testOrphanedWhileManaged() {
createData();
Session session = openSession();
session.beginTransaction();
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
Employee emp = ( Employee ) results.get( 0 );
assertNotNull( emp.getInfo() );
emp.setInfo( null );
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
emp = ( Employee ) session.get( Employee.class, emp.getId() );
assertNull( emp.getInfo() );
results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
session.getTransaction().commit();
session.close();
cleanupData();
}
@Test
@TestForIssue(jiraKey = "HHH-6484")
public void testReplacedWhileManaged() {
createData();
Session session = openSession();
session.beginTransaction();
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
Employee emp = (Employee) results.get( 0 );
assertNotNull( emp.getInfo() );
// Replace with a new EmployeeInfo instance
emp.setInfo( new EmployeeInfo( emp ) );
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
emp = (Employee) session.get( Employee.class, emp.getId() );
assertNotNull( emp.getInfo() );
results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
session.getTransaction().commit();
session.close();
cleanupData();
}
}

View File

@ -1,261 +0,0 @@
/*
* 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.fk.bidirectional.multilevelcascade;
import java.util.List;
import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/**
* @author Steve Ebersole
* @author Gail Badner
*/
public class DeleteMultiLevelOrphansTest extends BaseCoreFunctionalTestCase {
private void createData() {
Preisregelung preisregelung = new Preisregelung();
Tranchenmodell tranchenmodell = new Tranchenmodell();
X x = new X();
Tranche tranche1 = new Tranche();
Y y = new Y();
Tranche tranche2 = new Tranche();
preisregelung.setTranchenmodell( tranchenmodell );
tranchenmodell.setPreisregelung( preisregelung );
tranchenmodell.setX( x );
x.setTranchenmodell( tranchenmodell );
tranchenmodell.getTranchen().add( tranche1 );
tranche1.setTranchenmodell( tranchenmodell );
tranchenmodell.getTranchen().add( tranche2 );
tranche2.setTranchenmodell( tranchenmodell );
tranche1.setY( y );
y.setTranche( tranche1 );
Session session = openSession();
session.beginTransaction();
session.save( preisregelung );
session.getTransaction().commit();
session.close();
}
private void cleanupData() {
Session session = openSession();
session.beginTransaction();
session.createQuery( "delete Tranche" ).executeUpdate();
session.createQuery( "delete Tranchenmodell" ).executeUpdate();
session.createQuery( "delete Preisregelung" ).executeUpdate();
session.getTransaction().commit();
session.close();
}
@Test
@TestForIssue( jiraKey = "HHH-9091")
public void testDirectAssociationOrphanedWhileManaged() {
createData();
Session session = openSession();
session.beginTransaction();
List results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
Preisregelung preisregelung = ( Preisregelung ) results.get( 0 );
Tranchenmodell tranchenmodell = preisregelung.getTranchenmodell();
assertNotNull( tranchenmodell );
assertNotNull( tranchenmodell.getX() );
assertEquals( 2, tranchenmodell.getTranchen().size() );
assertNotNull( tranchenmodell.getTranchen().get( 0 ).getY() );
preisregelung.setTranchenmodell( null );
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
preisregelung = ( Preisregelung ) session.get( Preisregelung.class, preisregelung.getId() );
assertNull( preisregelung.getTranchenmodell() );
results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Tranche" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from X" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Y" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
session.getTransaction().commit();
session.close();
cleanupData();
}
@Test
@TestForIssue( jiraKey = "HHH-9091")
public void testReplacedDirectAssociationWhileManaged() {
createData();
Session session = openSession();
session.beginTransaction();
List results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
Preisregelung preisregelung = ( Preisregelung ) results.get( 0 );
Tranchenmodell tranchenmodell = preisregelung.getTranchenmodell();
assertNotNull( tranchenmodell );
assertNotNull( tranchenmodell.getX() );
assertEquals( 2, tranchenmodell.getTranchen().size() );
assertNotNull( tranchenmodell.getTranchen().get( 0 ).getY() );
// Create a new Tranchenmodell with new direct and nested associations
Tranchenmodell tranchenmodellNew = new Tranchenmodell();
X xNew = new X();
tranchenmodellNew.setX( xNew );
xNew.setTranchenmodell( tranchenmodellNew );
Tranche trancheNew = new Tranche();
tranchenmodellNew.getTranchen().add( trancheNew );
trancheNew.setTranchenmodell( tranchenmodellNew );
Y yNew = new Y();
trancheNew.setY( yNew );
yNew.setTranche( trancheNew );
// Replace with a new Tranchenmodell instance containing new direct and nested associations
preisregelung.setTranchenmodell(tranchenmodellNew );
tranchenmodellNew.setPreisregelung( preisregelung );
session.getTransaction().commit();
session.close();
session = openSession();
session.getTransaction().begin();
results = session.createQuery( "from Tranche" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from X" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Y" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
preisregelung = ( Preisregelung ) results.get( 0 );
tranchenmodell = preisregelung.getTranchenmodell();
assertNotNull( tranchenmodell );
assertEquals( tranchenmodellNew.getId(), tranchenmodell.getId() );
assertNotNull( tranchenmodell.getX() );
assertEquals( xNew.getId(), tranchenmodell.getX().getId() );
assertEquals( 1, tranchenmodell.getTranchen().size() );
assertEquals( trancheNew.getId(), tranchenmodell.getTranchen().get( 0 ).getId() );
assertEquals( yNew.getId(), tranchenmodell.getTranchen().get( 0 ).getY().getId() );
// Replace with a new Tranchenmodell instance with no associations
tranchenmodellNew = new Tranchenmodell();
preisregelung.setTranchenmodell(tranchenmodellNew );
tranchenmodellNew.setPreisregelung( preisregelung );
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 1, results.size() );
tranchenmodell = (Tranchenmodell) results.get( 0 );
assertEquals( tranchenmodellNew.getId(), tranchenmodell.getId() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
preisregelung = (Preisregelung) results.get( 0 );
assertEquals( tranchenmodell, preisregelung.getTranchenmodell() );
results = session.createQuery( "from Tranche" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from X" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Y" ).list();
assertEquals( 0, results.size() );
session.getTransaction().commit();
session.close();
cleanupData();
}
@Test
@TestForIssue( jiraKey = "HHH-9091")
public void testDirectAndNestedAssociationsOrphanedWhileManaged() {
createData();
Session session = openSession();
session.beginTransaction();
List results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
Preisregelung preisregelung = ( Preisregelung ) results.get( 0 );
Tranchenmodell tranchenmodell = preisregelung.getTranchenmodell();
assertNotNull( tranchenmodell );
assertNotNull( tranchenmodell.getX() );
assertEquals( 2, tranchenmodell.getTranchen().size() );
assertNotNull( tranchenmodell.getTranchen().get( 0 ).getY() );
preisregelung.setTranchenmodell( null );
tranchenmodell.setX( null );
tranchenmodell.getTranchen().get( 0 ).setY( null );
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
preisregelung = ( Preisregelung ) session.get( Preisregelung.class, preisregelung.getId() );
assertNull( preisregelung.getTranchenmodell() );
results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Tranche" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from X" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Y" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
session.getTransaction().commit();
session.close();
cleanupData();
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[]{
Preisregelung.class,
Tranche.class,
Tranchenmodell.class,
X.class,
Y.class
};
}
}

View File

@ -1,112 +0,0 @@
/*
* 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.fk.composite;
import java.util.List;
import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/**
* @author Steve Ebersole
*/
public class DeleteOneToOneOrphansTest extends BaseCoreFunctionalTestCase {
@Override
public String[] getMappings() {
return new String[] { "orphan/one2one/fk/composite/Mapping.hbm.xml" };
}
private void createData() {
Session session = openSession();
session.beginTransaction();
Employee emp = new Employee();
emp.setInfo( new EmployeeInfo( 1L, 1L) );
session.save( emp );
session.getTransaction().commit();
session.close();
}
private void cleanupData() {
Session session = openSession();
session.beginTransaction();
session.createQuery( "delete Employee" ).executeUpdate();
session.createQuery( "delete EmployeeInfo" ).executeUpdate();
session.getTransaction().commit();
session.close();
}
@Test
public void testOrphanedWhileManaged() {
createData();
Session session = openSession();
session.beginTransaction();
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
Employee emp = ( Employee ) results.get( 0 );
assertNotNull( emp.getInfo() );
emp.setInfo( null );
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
emp = ( Employee ) session.get( Employee.class, emp.getId() );
assertNull( emp.getInfo() );
results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
session.getTransaction().commit();
session.close();
cleanupData();
}
@Test
@TestForIssue(jiraKey = "HHH-6484")
public void testReplacedWhileManaged() {
createData();
Session session = openSession();
session.beginTransaction();
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
Employee emp = (Employee) results.get( 0 );
assertNotNull( emp.getInfo() );
// Replace with a new EmployeeInfo instance
emp.setInfo( new EmployeeInfo( 2L, 2L ) );
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
emp = (Employee) session.get( Employee.class, emp.getId() );
assertNotNull( emp.getInfo() );
results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
session.getTransaction().commit();
session.close();
cleanupData();
}
}

View File

@ -1,111 +0,0 @@
/*
* 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.fk.reversed.bidirectional;
import java.util.List;
import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/**
* @author Steve Ebersole
*/
public class DeleteOneToOneOrphansTest extends BaseCoreFunctionalTestCase {
public String[] getMappings() {
return new String[] { "orphan/one2one/fk/reversed/bidirectional/Mapping.hbm.xml" };
}
private void createData() {
Session session = openSession();
session.beginTransaction();
Employee emp = new Employee();
emp.setInfo( new EmployeeInfo( emp ) );
session.save( emp );
session.getTransaction().commit();
session.close();
}
private void cleanupData() {
Session session = openSession();
session.beginTransaction();
session.createQuery( "delete Employee" ).executeUpdate();
session.createQuery( "delete EmployeeInfo" ).executeUpdate();
session.getTransaction().commit();
session.close();
}
@Test
public void testOrphanedWhileManaged() {
createData();
Session session = openSession();
session.beginTransaction();
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
Employee emp = ( Employee ) results.get( 0 );
assertNotNull( emp.getInfo() );
emp.setInfo( null );
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
emp = ( Employee ) session.get( Employee.class, emp.getId() );
assertNull( emp.getInfo() );
results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
session.getTransaction().commit();
session.close();
cleanupData();
}
@Test
@TestForIssue(jiraKey = "HHH-6484")
public void testReplacedWhileManaged() {
createData();
Session session = openSession();
session.beginTransaction();
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
Employee emp = (Employee) results.get( 0 );
assertNotNull( emp.getInfo() );
// Replace with a new EmployeeInfo instance
emp.setInfo( new EmployeeInfo( emp ) );
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
emp = (Employee) session.get( Employee.class, emp.getId() );
assertNotNull( emp.getInfo() );
results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
session.getTransaction().commit();
session.close();
cleanupData();
}
}

View File

@ -1,148 +0,0 @@
/*
* 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.fk.reversed.bidirectional.multilevelcascade;
import java.util.List;
import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/**
* @author Steve Ebersole
* @author Gail Badner
*/
public class DeleteMultiLevelOrphansTest extends BaseCoreFunctionalTestCase {
private void createData() {
Preisregelung preisregelung = new Preisregelung();
preisregelung.setId( 17960L );
Tranchenmodell tranchenmodell = new Tranchenmodell();
tranchenmodell.setId( 1951L );
Tranche tranche1 = new Tranche();
tranche1.setId( 1951L);
Tranche tranche2 = new Tranche();
tranche2.setId( 1952L);
preisregelung.setTranchenmodell( tranchenmodell );
tranchenmodell.setPreisregelung( preisregelung );
tranchenmodell.getTranchen().add( tranche1 );
tranche1.setTranchenmodell( tranchenmodell );
tranchenmodell.getTranchen().add( tranche2 );
tranche2.setTranchenmodell( tranchenmodell );
Session session = openSession();
session.beginTransaction();
session.save( preisregelung );
session.getTransaction().commit();
session.close();
}
private void cleanupData() {
Session session = openSession();
session.beginTransaction();
session.createQuery( "delete Tranche" ).executeUpdate();
session.createQuery( "delete Preisregelung" ).executeUpdate();
session.createQuery( "delete Tranchenmodell" ).executeUpdate();
session.getTransaction().commit();
session.close();
}
@Test
@TestForIssue( jiraKey = "HHH-9091")
public void testOrphanedWhileManaged() {
createData();
Session session = openSession();
session.beginTransaction();
List results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
Preisregelung preisregelung = (Preisregelung) results.get( 0 );
assertNotNull( preisregelung.getTranchenmodell() );
preisregelung.setTranchenmodell( null );
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
preisregelung = (Preisregelung) session.get( Preisregelung.class, preisregelung.getId() );
assertNull( preisregelung.getTranchenmodell() );
results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
session.getTransaction().commit();
session.close();
cleanupData();
}
@Test
@TestForIssue( jiraKey = "HHH-9091")
public void testReplacedWhileManaged() {
createData();
Session session = openSession();
session.beginTransaction();
List results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
Preisregelung preisregelung = (Preisregelung) results.get( 0 );
assertNotNull( preisregelung.getTranchenmodell() );
// Replace with a new Tranchenmodell instance
Tranchenmodell tranchenmodellNew = new Tranchenmodell();
tranchenmodellNew.setId( 1952L );
preisregelung.setTranchenmodell( tranchenmodellNew );
tranchenmodellNew.setPreisregelung( preisregelung );
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 1, results.size() );
Tranchenmodell tranchenmodellQueried = (Tranchenmodell) results.get( 0 );
assertEquals( tranchenmodellNew.getId(), tranchenmodellQueried.getId() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
Preisregelung preisregelung1Queried = (Preisregelung) results.get( 0 );
assertEquals( preisregelung1Queried.getTranchenmodell(), tranchenmodellQueried );
results = session.createQuery( "from Tranche" ).list();
assertEquals( 0, results.size() );
session.getTransaction().commit();
session.close();
cleanupData();
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[]{
Preisregelung.class,
Tranche.class,
Tranchenmodell.class
};
}
}

View File

@ -1,155 +0,0 @@
/*
* 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.fk.reversed.unidirectional;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
/**
* @author Steve Ebersole
*/
public class DeleteOneToOneOrphansTest extends BaseCoreFunctionalTestCase {
public String[] getMappings() {
return new String[] { "orphan/one2one/fk/reversed/unidirectional/Mapping.hbm.xml" };
}
private void createData() {
Session session = openSession();
session.beginTransaction();
Employee emp = new Employee();
emp.setInfo( new EmployeeInfo() );
session.save( emp );
session.getTransaction().commit();
session.close();
}
private void cleanupData() {
Session session = openSession();
session.beginTransaction();
session.createQuery( "delete Employee" ).executeUpdate();
session.createQuery( "delete EmployeeInfo" ).executeUpdate();
session.getTransaction().commit();
session.close();
}
@Test
public void testOrphanedWhileManaged() {
createData();
Session session = openSession();
session.beginTransaction();
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
Employee emp = ( Employee ) results.get( 0 );
assertNotNull( emp.getInfo() );
emp.setInfo( null );
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
emp = ( Employee ) session.get( Employee.class, emp.getId() );
assertNull( emp.getInfo() );
results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
session.getTransaction().commit();
session.close();
cleanupData();
}
@Test
@TestForIssue( jiraKey = "HHH-5267" )
public void testOrphanedWhileDetached() {
createData();
Session session = openSession();
session.beginTransaction();
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
Employee emp = ( Employee ) results.get( 0 );
assertNotNull( emp.getInfo() );
//only fails if the object is detached
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
emp.setInfo( null );
//save using the new session (this used to work prior to 3.5.x)
session.saveOrUpdate(emp);
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
emp = ( Employee ) session.get( Employee.class, emp.getId() );
assertNull( emp.getInfo() );
// TODO: If merge was used instead of saveOrUpdate, this would work.
// However, re-attachment does not currently support handling orphans.
// See HHH-3795
// results = session.createQuery( "from EmployeeInfo" ).list();
// assertEquals( 0, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
session.getTransaction().commit();
session.close();
cleanupData();
}
@Test
@TestForIssue(jiraKey = "HHH-6484")
public void testReplacedWhileManaged() {
createData();
Session session = openSession();
session.beginTransaction();
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
Employee emp = (Employee) results.get( 0 );
assertNotNull( emp.getInfo() );
// Replace with a new EmployeeInfo instance
emp.setInfo( new EmployeeInfo() );
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
emp = (Employee) session.get( Employee.class, emp.getId() );
assertNotNull( emp.getInfo() );
results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
session.getTransaction().commit();
session.close();
cleanupData();
}
}

View File

@ -1,77 +0,0 @@
/*
* 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.pk.bidirectional;
import java.util.List;
import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/**
* @author Steve Ebersole
*/
public class DeleteOneToOneOrphansTest extends BaseCoreFunctionalTestCase {
@Override
public String[] getMappings() {
return new String[] { "orphan/one2one/pk/bidirectional/Mapping.hbm.xml" };
}
private void createData() {
Session session = openSession();
session.beginTransaction();
Employee emp = new Employee();
emp.setInfo( new EmployeeInfo( emp ) );
session.save( emp );
session.getTransaction().commit();
session.close();
}
private void cleanupData() {
Session session = openSession();
session.beginTransaction();
session.createQuery( "delete EmployeeInfo" ).executeUpdate();
session.createQuery( "delete Employee" ).executeUpdate();
session.getTransaction().commit();
session.close();
}
@Test
public void testOrphanedWhileManaged() {
createData();
Session session = openSession();
session.beginTransaction();
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
Employee emp = ( Employee ) results.get( 0 );
assertNotNull( emp.getInfo() );
emp.setInfo( null );
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
emp = ( Employee ) session.get( Employee.class, emp.getId() );
assertNull( emp.getInfo() );
results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
session.getTransaction().commit();
session.close();
cleanupData();
}
}

View File

@ -1,84 +0,0 @@
/*
* 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.pk.unidirectional;
import java.util.List;
import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
/**
* @author Steve Ebersole
*/
public class DeleteOneToOneOrphansTest extends BaseCoreFunctionalTestCase {
@Override
public String[] getMappings() {
return new String[] { "orphan/one2one/pk/unidirectional/Mapping.hbm.xml" };
}
private void createData() {
Session session = openSession();
session.beginTransaction();
Employee emp = new Employee();
session.save( emp );
emp.setInfo( new EmployeeInfo( emp.getId() ) );
session.getTransaction().commit();
session.close();
}
private void cleanupData() {
Session session = openSession();
session.beginTransaction();
session.createQuery( "delete EmployeeInfo" ).executeUpdate();
session.createQuery( "delete Employee" ).executeUpdate();
session.getTransaction().commit();
session.close();
}
@Test
public void testOrphanedWhileManaged() {
createData();
Session session = openSession();
session.beginTransaction();
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
Employee emp = ( Employee ) results.get( 0 );
assertNotNull( emp.getInfo() );
results = session.createQuery( "from Employee e, EmployeeInfo i where e.info = i" ).list();
assertEquals( 1, results.size() );
Object [] result = ( Object [] ) results.get( 0 );
emp = ( Employee ) result[ 0 ];
assertNotNull( result[ 1 ] );
assertSame( emp.getInfo(), result[ 1 ] );
emp.setInfo( null );
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
emp = ( Employee ) session.get( Employee.class, emp.getId() );
assertNull( emp.getInfo() );
results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
session.getTransaction().commit();
session.close();
cleanupData();
}
}

View File

@ -1,167 +0,0 @@
/*
* 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.onetomany;
import java.util.List;
import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
/**
* @author Gail Badnmer
*/
public class DeleteOneToManyOrphansTest extends BaseCoreFunctionalTestCase {
private void createData() {
Session s = openSession();
s.getTransaction().begin();
Feature newFeature = new Feature();
newFeature.setName("Feature 1");
s.persist( newFeature );
Product product = new Product();
newFeature.setProduct( product );
product.getFeatures().add( newFeature );
s.persist( product );
s.getTransaction().commit();
s.clear();
}
private void cleanupData() {
Session session = openSession();
session.beginTransaction();
session.createQuery( "delete Feature" ).executeUpdate();
session.createQuery( "delete Product" ).executeUpdate();
session.getTransaction().commit();
session.close();
}
@Test
@TestForIssue( jiraKey = "HHH-9330")
public void testOrphanedWhileManaged() {
createData();
Session session = openSession();
session.beginTransaction();
List results = session.createQuery( "from Feature" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Product" ).list();
assertEquals( 1, results.size() );
Product product = ( Product ) results.get( 0 );
assertEquals( 1, product.getFeatures().size() );
product.getFeatures().clear();
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
product = ( Product ) session.get( Product.class, product.getId() );
assertEquals( 0, product.getFeatures().size() );
results = session.createQuery( "from Feature" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Product" ).list();
assertEquals( 1, results.size() );
session.getTransaction().commit();
session.close();
cleanupData();
}
@Test
@TestForIssue( jiraKey = "HHH-9330")
public void testOrphanedWhileManagedMergeOwner() {
createData();
Session session = openSession();
session.beginTransaction();
List results = session.createQuery( "from Feature" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Product" ).list();
assertEquals( 1, results.size() );
Product product = ( Product ) results.get( 0 );
assertEquals( 1, product.getFeatures().size() );
product.getFeatures().clear();
session.merge( product );
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
product = ( Product ) session.get( Product.class, product.getId() );
assertEquals( 0, product.getFeatures().size() );
results = session.createQuery( "from Feature" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Product" ).list();
assertEquals( 1, results.size() );
session.getTransaction().commit();
session.close();
cleanupData();
}
@Test
@TestForIssue( jiraKey = "HHH-9330")
public void testReplacedWhileManaged() {
createData();
Session session = openSession();
session.beginTransaction();
List results = session.createQuery( "from Feature" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Product" ).list();
assertEquals( 1, results.size() );
Product product = ( Product ) results.get( 0 );
assertEquals( 1, product.getFeatures().size() );
// Replace with a new Feature instance
product.getFeatures().remove( 0 );
Feature featureNew = new Feature();
featureNew.setName( "Feature 2" );
featureNew.setProduct( product );
product.getFeatures().add( featureNew );
session.persist( featureNew );
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
results = session.createQuery( "from Feature" ).list();
assertEquals( 1, results.size() );
Feature featureQueried = (Feature) results.get( 0 );
assertEquals( featureNew.getId(), featureQueried.getId() );
results = session.createQuery( "from Product" ).list();
assertEquals( 1, results.size() );
Product productQueried = (Product) results.get( 0 );
assertEquals( 1, productQueried.getFeatures().size() );
assertEquals( featureQueried, productQueried.getFeatures().get( 0 ) );
session.getTransaction().commit();
session.close();
cleanupData();
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[]{
Product.class,
Feature.class
};
}
}

View File

@ -6,6 +6,8 @@
*/ */
package org.hibernate.testing.orm.junit; package org.hibernate.testing.orm.junit;
import java.util.Optional;
import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext; import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException; import org.junit.jupiter.api.extension.ParameterResolutionException;