From 96be0130893c7a7b6aa50b7ee8d90301eb54c7e7 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Sat, 10 Jul 2021 20:59:21 +0200 Subject: [PATCH] Re-enabled additional tests --- .../hibernate/orm/test/orphan/OrphanTest.java | 391 ++++++++++++++++++ .../hibernate/{ => orm}/test/orphan/Part.java | 2 +- .../{ => orm}/test/orphan/Product.hbm.xml | 2 +- .../{ => orm}/test/orphan/Product.java | 3 +- .../ElementCollectionOrphanTest.java | 58 +++ .../elementcollection/EnrollableClass.java | 2 +- .../elementcollection/EnrolledClassSeat.java | 2 +- .../orphan/elementcollection/Student.java | 2 +- .../StudentEnrolledClass.java | 2 +- .../orphan/elementcollection/student.hbm.xml | 16 +- .../test/orphan/manytomany/Group.java | 2 +- .../manytomany/ManyToManyOrphanTest.java | 40 +- .../test/orphan/manytomany/User.java | 2 +- .../test/orphan/manytomany/UserGroup.hbm.xml | 2 +- ...oOneEagerNonOptionalOrphanRemovalTest.java | 54 ++- .../OneToOneEagerOrphanRemovalTest.java | 52 ++- ...ToOneLazyNonOptionalOrphanRemovalTest.java | 54 ++- .../OneToOneLazyOrphanRemovalTest.java | 50 ++- .../OneToOneProxyOrphanRemovalTest.java | 59 ++- .../DeleteOneToOneOrphansTest.java | 112 +++++ .../one2one/fk/bidirectional}/Employee.java | 2 +- .../fk/bidirectional/EmployeeInfo.java | 2 +- .../one2one/fk/bidirectional/Mapping.hbm.xml | 2 +- .../DeleteMultiLevelOrphansTest.java | 258 ++++++++++++ .../multilevelcascade/Preisregelung.java | 2 +- .../multilevelcascade/Tranche.java | 2 +- .../multilevelcascade/Tranchenmodell.java | 2 +- .../fk/bidirectional/multilevelcascade/X.java | 2 +- .../fk/bidirectional/multilevelcascade/Y.java | 2 +- .../composite/DeleteOneToOneOrphansTest.java | 111 +++++ .../one2one/fk/composite}/Employee.java | 2 +- .../one2one/fk/composite/EmployeeInfo.java | 2 +- .../one2one/fk/composite/Mapping.hbm.xml | 2 +- .../DeleteOneToOneOrphansTest.java | 113 +++++ .../fk/reversed/bidirectional/Employee.java | 2 +- .../reversed/bidirectional/EmployeeInfo.java | 2 +- .../fk/reversed/bidirectional/Mapping.hbm.xml | 2 +- .../DeleteMultiLevelOrphansTest.java | 143 +++++++ .../multilevelcascade/Preisregelung.java | 2 +- .../multilevelcascade/Tranche.java | 2 +- .../multilevelcascade/Tranchenmodell.java | 2 +- .../DeleteOneToOneOrphansTest.java | 156 +++++++ .../fk/reversed/unidirectional/Employee.java | 2 +- .../reversed/unidirectional/EmployeeInfo.java | 2 +- .../reversed/unidirectional/Mapping.hbm.xml | 2 +- .../DeleteOneToOneOrphansTest.java | 79 ++++ .../one2one/pk/bidirectional/Employee.java | 2 +- .../pk/bidirectional/EmployeeInfo.java | 2 +- .../one2one/pk/bidirectional/Mapping.hbm.xml | 2 +- .../DeleteOneToOneOrphansTest.java | 86 ++++ .../one2one/pk/unidirectional}/Employee.java | 2 +- .../pk/unidirectional/EmployeeInfo.java | 2 +- .../one2one/pk/unidirectional/Mapping.hbm.xml | 2 +- .../onetomany/DeleteOneToManyOrphansTest.java | 158 +++++++ .../test/orphan/onetomany/Feature.java | 2 +- .../test/orphan/onetomany/Product.java | 2 +- .../org/hibernate/test/orphan/Mail.hbm.xml | 2 +- .../org/hibernate/test/orphan/OrphanTest.java | 386 ----------------- .../test/orphan/PropertyRefTest.java | 39 +- .../org/hibernate/test/orphan/User.hbm.xml | 2 +- .../ElementCollectionOrphanTest.java | 53 --- .../DeleteOneToOneOrphansTest.java | 112 ----- .../DeleteMultiLevelOrphansTest.java | 261 ------------ .../composite/DeleteOneToOneOrphansTest.java | 112 ----- .../DeleteOneToOneOrphansTest.java | 111 ----- .../DeleteMultiLevelOrphansTest.java | 148 ------- .../DeleteOneToOneOrphansTest.java | 155 ------- .../DeleteOneToOneOrphansTest.java | 77 ---- .../DeleteOneToOneOrphansTest.java | 84 ---- .../onetomany/DeleteOneToManyOrphansTest.java | 167 -------- .../SessionFactoryScopeParameterResolver.java | 2 + 71 files changed, 1946 insertions(+), 1832 deletions(-) create mode 100755 hibernate-core/src/test/java/org/hibernate/orm/test/orphan/OrphanTest.java rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/Part.java (94%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/Product.hbm.xml (92%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/Product.java (94%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/orphan/elementcollection/ElementCollectionOrphanTest.java rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/elementcollection/EnrollableClass.java (89%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/elementcollection/EnrolledClassSeat.java (87%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/elementcollection/Student.java (92%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/elementcollection/StudentEnrolledClass.java (91%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/elementcollection/student.hbm.xml (58%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/manytomany/Group.java (90%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/manytomany/ManyToManyOrphanTest.java (65%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/manytomany/User.java (90%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/manytomany/UserGroup.hbm.xml (90%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/one2one/OneToOneEagerNonOptionalOrphanRemovalTest.java (74%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/one2one/OneToOneEagerOrphanRemovalTest.java (75%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/one2one/OneToOneLazyNonOptionalOrphanRemovalTest.java (75%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/one2one/OneToOneLazyOrphanRemovalTest.java (75%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/one2one/OneToOneProxyOrphanRemovalTest.java (66%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/DeleteOneToOneOrphansTest.java rename hibernate-core/src/test/java/org/hibernate/{test/orphan/one2one/pk/unidirectional => orm/test/orphan/one2one/fk/bidirectional}/Employee.java (89%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/one2one/fk/bidirectional/EmployeeInfo.java (91%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/one2one/fk/bidirectional/Mapping.hbm.xml (92%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/multilevelcascade/DeleteMultiLevelOrphansTest.java rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/one2one/fk/bidirectional/multilevelcascade/Preisregelung.java (92%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/one2one/fk/bidirectional/multilevelcascade/Tranche.java (92%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/one2one/fk/bidirectional/multilevelcascade/Tranchenmodell.java (93%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/one2one/fk/bidirectional/multilevelcascade/X.java (91%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/one2one/fk/bidirectional/multilevelcascade/Y.java (90%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/composite/DeleteOneToOneOrphansTest.java rename hibernate-core/src/test/java/org/hibernate/{test/orphan/one2one/fk/bidirectional => orm/test/orphan/one2one/fk/composite}/Employee.java (90%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/one2one/fk/composite/EmployeeInfo.java (96%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/one2one/fk/composite/Mapping.hbm.xml (92%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/bidirectional/DeleteOneToOneOrphansTest.java rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/one2one/fk/reversed/bidirectional/Employee.java (88%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/one2one/fk/reversed/bidirectional/EmployeeInfo.java (90%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/one2one/fk/reversed/bidirectional/Mapping.hbm.xml (91%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/DeleteMultiLevelOrphansTest.java rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/Preisregelung.java (90%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/Tranche.java (90%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/Tranchenmodell.java (93%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/unidirectional/DeleteOneToOneOrphansTest.java rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/one2one/fk/reversed/unidirectional/Employee.java (88%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/one2one/fk/reversed/unidirectional/EmployeeInfo.java (86%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/one2one/fk/reversed/unidirectional/Mapping.hbm.xml (90%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/pk/bidirectional/DeleteOneToOneOrphansTest.java rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/one2one/pk/bidirectional/Employee.java (89%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/one2one/pk/bidirectional/EmployeeInfo.java (91%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/one2one/pk/bidirectional/Mapping.hbm.xml (92%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/pk/unidirectional/DeleteOneToOneOrphansTest.java rename hibernate-core/src/test/java/org/hibernate/{test/orphan/one2one/fk/composite => orm/test/orphan/one2one/pk/unidirectional}/Employee.java (89%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/one2one/pk/unidirectional/EmployeeInfo.java (88%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/one2one/pk/unidirectional/Mapping.hbm.xml (91%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/orphan/onetomany/DeleteOneToManyOrphansTest.java rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/onetomany/Feature.java (95%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/orphan/onetomany/Product.java (95%) delete mode 100755 hibernate-core/src/test/java/org/hibernate/test/orphan/OrphanTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/orphan/elementcollection/ElementCollectionOrphanTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/DeleteOneToOneOrphansTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/multilevelcascade/DeleteMultiLevelOrphansTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/DeleteOneToOneOrphansTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/bidirectional/DeleteOneToOneOrphansTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/DeleteMultiLevelOrphansTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/unidirectional/DeleteOneToOneOrphansTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/pk/bidirectional/DeleteOneToOneOrphansTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/pk/unidirectional/DeleteOneToOneOrphansTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/orphan/onetomany/DeleteOneToManyOrphansTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/OrphanTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/OrphanTest.java new file mode 100755 index 0000000000..2fab5f5962 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/OrphanTest.java @@ -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 . + */ +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" ) ); + } + ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/Part.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/Part.java similarity index 94% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/Part.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/Part.java index 17b99d6c95..ae2558e2a3 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/Part.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/Part.java @@ -6,7 +6,7 @@ */ //$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; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/Product.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/Product.hbm.xml similarity index 92% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/Product.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/Product.hbm.xml index 075e6170c7..f6ad16a69d 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/Product.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/Product.hbm.xml @@ -14,7 +14,7 @@ --> - + diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/Product.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/Product.java similarity index 94% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/Product.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/Product.java index defa6b8e10..0ac77069d9 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/Product.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/Product.java @@ -6,7 +6,8 @@ */ //$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.util.HashSet; import java.util.Set; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/elementcollection/ElementCollectionOrphanTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/elementcollection/ElementCollectionOrphanTest.java new file mode 100644 index 0000000000..36a0651483 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/elementcollection/ElementCollectionOrphanTest.java @@ -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 ); + } + ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/elementcollection/EnrollableClass.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/elementcollection/EnrollableClass.java similarity index 89% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/elementcollection/EnrollableClass.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/elementcollection/EnrollableClass.java index 5ae6e94d60..e108a94ac0 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/elementcollection/EnrollableClass.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/elementcollection/EnrollableClass.java @@ -1,4 +1,4 @@ -package org.hibernate.test.orphan.elementcollection; +package org.hibernate.orm.test.orphan.elementcollection; import javax.persistence.Column; import javax.persistence.Entity; diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/elementcollection/EnrolledClassSeat.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/elementcollection/EnrolledClassSeat.java similarity index 87% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/elementcollection/EnrolledClassSeat.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/elementcollection/EnrolledClassSeat.java index 3a1d2654d0..3951ba737e 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/elementcollection/EnrolledClassSeat.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/elementcollection/EnrolledClassSeat.java @@ -1,4 +1,4 @@ -package org.hibernate.test.orphan.elementcollection; +package org.hibernate.orm.test.orphan.elementcollection; public class EnrolledClassSeat { private String id; diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/elementcollection/Student.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/elementcollection/Student.java similarity index 92% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/elementcollection/Student.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/elementcollection/Student.java index 541184233f..51ee92f5e2 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/elementcollection/Student.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/elementcollection/Student.java @@ -1,4 +1,4 @@ -package org.hibernate.test.orphan.elementcollection; +package org.hibernate.orm.test.orphan.elementcollection; import java.util.Set; diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/elementcollection/StudentEnrolledClass.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/elementcollection/StudentEnrolledClass.java similarity index 91% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/elementcollection/StudentEnrolledClass.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/elementcollection/StudentEnrolledClass.java index 199c865000..264b6ffd63 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/elementcollection/StudentEnrolledClass.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/elementcollection/StudentEnrolledClass.java @@ -1,4 +1,4 @@ -package org.hibernate.test.orphan.elementcollection; +package org.hibernate.orm.test.orphan.elementcollection; public class StudentEnrolledClass { private EnrollableClass enrolledClass; diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/elementcollection/student.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/elementcollection/student.hbm.xml similarity index 58% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/elementcollection/student.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/elementcollection/student.hbm.xml index 60af248813..1e48f42226 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/elementcollection/student.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/elementcollection/student.hbm.xml @@ -5,7 +5,7 @@ "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> - + @@ -17,17 +17,17 @@ - - + + - + - + @@ -35,7 +35,7 @@ - + diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/Group.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/manytomany/Group.java similarity index 90% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/Group.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/manytomany/Group.java index c4ec143919..37ef98ef55 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/Group.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/manytomany/Group.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.manytomany; +package org.hibernate.orm.test.orphan.manytomany; import java.io.Serializable; diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/ManyToManyOrphanTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/manytomany/ManyToManyOrphanTest.java similarity index 65% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/ManyToManyOrphanTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/manytomany/ManyToManyOrphanTest.java index e1c7a79eb0..f04a573134 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/ManyToManyOrphanTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/manytomany/ManyToManyOrphanTest.java @@ -4,29 +4,41 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.manytomany; +package org.hibernate.orm.test.orphan.manytomany; import java.util.List; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; +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.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 - protected String[] getMappings() { - return new String[] { "orphan/manytomany/UserGroup.hbm.xml" }; + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + session.createQuery( "delete from User" ).executeUpdate(); + session.createQuery( "delete from Group" ).executeUpdate(); + } + ); } @Test @TestForIssue(jiraKey = "HHH-8749") - public void testManyToManyWithCascadeDeleteOrphan() { - inTransaction( + public void testManyToManyWithCascadeDeleteOrphan(SessionFactoryScope scope) { + scope.inTransaction( s -> { User bob = new User( "bob", "jboss" ); Group seam = new Group( "seam", "jboss" ); @@ -41,7 +53,7 @@ public class ManyToManyOrphanTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( s -> { User b = s.get( User.class, "bob" ); assertEquals( 2, b.getGroups().size() ); @@ -52,7 +64,7 @@ public class ManyToManyOrphanTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( s -> { User b = s.get( User.class, "bob" ); assertEquals( 2, b.getGroups().size() ); @@ -62,7 +74,7 @@ public class ManyToManyOrphanTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( s -> { User b = s.get( User.class, "bob" ); assertEquals( 1, b.getGroups().size() ); @@ -70,7 +82,7 @@ public class ManyToManyOrphanTest extends BaseCoreFunctionalTestCase { ); // Verify orphan group was deleted - inTransaction( + scope.inTransaction( s -> { CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); CriteriaQuery criteria = criteriaBuilder.createQuery( Group.class ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/User.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/manytomany/User.java similarity index 90% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/User.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/manytomany/User.java index 6429d3a4e2..06b061421c 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/User.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/manytomany/User.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.manytomany; +package org.hibernate.orm.test.orphan.manytomany; import java.io.Serializable; import java.util.HashMap; diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/UserGroup.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/manytomany/UserGroup.hbm.xml similarity index 90% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/UserGroup.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/manytomany/UserGroup.hbm.xml index f24d1138b1..6013734994 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/UserGroup.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/manytomany/UserGroup.hbm.xml @@ -9,7 +9,7 @@ "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> - + diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneEagerNonOptionalOrphanRemovalTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/OneToOneEagerNonOptionalOrphanRemovalTest.java similarity index 74% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneEagerNonOptionalOrphanRemovalTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/OneToOneEagerNonOptionalOrphanRemovalTest.java index 76aca1e2e5..2fccbf182a 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneEagerNonOptionalOrphanRemovalTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/OneToOneEagerNonOptionalOrphanRemovalTest.java @@ -4,36 +4,52 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.one2one; +package org.hibernate.orm.test.orphan.one2one; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.OneToOne; -import org.junit.Test; - import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import 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.Assert.assertNotNull; -import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; /** * @author Chris Cranford */ -@TestForIssue( jiraKey = "HHH-9663" ) -public class OneToOneEagerNonOptionalOrphanRemovalTest extends BaseCoreFunctionalTestCase { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Car.class, PaintColor.class, Engine.class }; +@TestForIssue(jiraKey = "HHH-9663") +@DomainModel( + annotatedClasses = { + OneToOneEagerNonOptionalOrphanRemovalTest.Car.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 - public void testOneToOneLazyNonOptionalOrphanRemoval() { + public void testOneToOneLazyNonOptionalOrphanRemoval(SessionFactoryScope scope) { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Initialize the data - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { final PaintColor color = new PaintColor( 1, "Red" ); final Engine engine1 = new Engine( 1, 275 ); final Engine engine2 = new Engine( 2, 295 ); @@ -47,14 +63,14 @@ public class OneToOneEagerNonOptionalOrphanRemovalTest extends BaseCoreFunctiona // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Test orphan removal for unidirectional relationship - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { final Car car = session.find( Car.class, 1 ); final Engine engine = session.find( Engine.class, 2 ); car.setEngine( engine ); session.update( car ); } ); - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { final Car car = session.find( Car.class, 1 ); assertNotNull( car.getEngine() ); @@ -64,7 +80,7 @@ public class OneToOneEagerNonOptionalOrphanRemovalTest extends BaseCoreFunctiona // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Test orphan removal for bidirectional relationship - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { final PaintColor color = new PaintColor( 2, "Blue" ); final Car car = session.find( Car.class, 1 ); car.setPaintColor( color ); @@ -72,7 +88,7 @@ public class OneToOneEagerNonOptionalOrphanRemovalTest extends BaseCoreFunctiona session.update( car ); } ); - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { final Car car = session.find( Car.class, 1 ); assertNotNull( car.getPaintColor() ); @@ -86,6 +102,8 @@ public class OneToOneEagerNonOptionalOrphanRemovalTest extends BaseCoreFunctiona @Id private Integer id; + private String model; + // represents a bidirectional one-to-one @OneToOne(orphanRemoval = true, optional = false) private PaintColor paintColor; diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneEagerOrphanRemovalTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/OneToOneEagerOrphanRemovalTest.java similarity index 75% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneEagerOrphanRemovalTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/OneToOneEagerOrphanRemovalTest.java index ee491de683..44c80d01d4 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneEagerOrphanRemovalTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/OneToOneEagerOrphanRemovalTest.java @@ -4,36 +4,52 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.one2one; +package org.hibernate.orm.test.orphan.one2one; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.OneToOne; -import org.junit.Test; - import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import 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.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertNull; /** * @author Chris Cranford */ -@TestForIssue( jiraKey = "HHH-9663" ) -public class OneToOneEagerOrphanRemovalTest extends BaseCoreFunctionalTestCase { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Car.class, PaintColor.class, Engine.class }; +@TestForIssue(jiraKey = "HHH-9663") +@DomainModel( + annotatedClasses = { + OneToOneEagerOrphanRemovalTest.Car.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 - public void testOneToOneEagerOrphanRemoval() { + public void testOneToOneEagerOrphanRemoval(SessionFactoryScope scope) { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Initialize the data - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { final PaintColor color = new PaintColor( 1, "Red" ); final Engine engine = new Engine( 1, 275 ); final Car car = new Car( 1, engine, color ); @@ -45,13 +61,13 @@ public class OneToOneEagerOrphanRemovalTest extends BaseCoreFunctionalTestCase { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Test orphan removal for unidirectional relationship - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { final Car car = session.find( Car.class, 1 ); car.setEngine( null ); session.update( car ); } ); - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { final Car car = session.find( Car.class, 1 ); assertNull( car.getEngine() ); @@ -61,13 +77,13 @@ public class OneToOneEagerOrphanRemovalTest extends BaseCoreFunctionalTestCase { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Test orphan removal for bidirectional relationship - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { final Car car = session.find( Car.class, 1 ); car.setPaintColor( null ); session.update( car ); } ); - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { final Car car = session.find( Car.class, 1 ); assertNull( car.getPaintColor() ); @@ -81,6 +97,8 @@ public class OneToOneEagerOrphanRemovalTest extends BaseCoreFunctionalTestCase { @Id private Integer id; + private String model; + // represents a bidirectional one-to-one @OneToOne(orphanRemoval = true, fetch = FetchType.EAGER) private PaintColor paintColor; diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneLazyNonOptionalOrphanRemovalTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/OneToOneLazyNonOptionalOrphanRemovalTest.java similarity index 75% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneLazyNonOptionalOrphanRemovalTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/OneToOneLazyNonOptionalOrphanRemovalTest.java index 3c8ef6e94b..9b98edd0cd 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneLazyNonOptionalOrphanRemovalTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/OneToOneLazyNonOptionalOrphanRemovalTest.java @@ -4,37 +4,53 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.one2one; +package org.hibernate.orm.test.orphan.one2one; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.OneToOne; -import org.junit.Test; - import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import 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.Assert.assertNotNull; -import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; /** * @author Chris Cranford */ -@TestForIssue( jiraKey = "HHH-9663" ) -public class OneToOneLazyNonOptionalOrphanRemovalTest extends BaseCoreFunctionalTestCase { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Car.class, PaintColor.class, Engine.class }; +@TestForIssue(jiraKey = "HHH-9663") +@DomainModel( + annotatedClasses = { + OneToOneLazyNonOptionalOrphanRemovalTest.Car.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 - public void testOneToOneLazyNonOptionalOrphanRemoval() { + public void testOneToOneLazyNonOptionalOrphanRemoval(SessionFactoryScope scope) { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Initialize the data - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { final PaintColor color = new PaintColor( 1, "Red" ); final Engine engine1 = new Engine( 1, 275 ); final Engine engine2 = new Engine( 2, 295 ); @@ -48,14 +64,14 @@ public class OneToOneLazyNonOptionalOrphanRemovalTest extends BaseCoreFunctional // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Test orphan removal for unidirectional relationship - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { final Car car = session.find( Car.class, 1 ); final Engine engine = session.find( Engine.class, 2 ); car.setEngine( engine ); session.update( car ); } ); - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { final Car car = session.find( Car.class, 1 ); assertNotNull( car.getEngine() ); @@ -65,7 +81,7 @@ public class OneToOneLazyNonOptionalOrphanRemovalTest extends BaseCoreFunctional // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Test orphan removal for bidirectional relationship - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { final PaintColor color = new PaintColor( 2, "Blue" ); final Car car = session.find( Car.class, 1 ); car.setPaintColor( color ); @@ -73,7 +89,7 @@ public class OneToOneLazyNonOptionalOrphanRemovalTest extends BaseCoreFunctional session.update( car ); } ); - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { final Car car = session.find( Car.class, 1 ); assertNotNull( car.getPaintColor() ); @@ -87,6 +103,8 @@ public class OneToOneLazyNonOptionalOrphanRemovalTest extends BaseCoreFunctional @Id private Integer id; + private String model; + // represents a bidirectional one-to-one @OneToOne(orphanRemoval = true, fetch = FetchType.LAZY, optional = false) private PaintColor paintColor; diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneLazyOrphanRemovalTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/OneToOneLazyOrphanRemovalTest.java similarity index 75% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneLazyOrphanRemovalTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/OneToOneLazyOrphanRemovalTest.java index cbfa1b9e14..6d91611383 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneLazyOrphanRemovalTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/OneToOneLazyOrphanRemovalTest.java @@ -4,36 +4,52 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.one2one; +package org.hibernate.orm.test.orphan.one2one; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.OneToOne; -import org.junit.Test; - import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import 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.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertNull; /** * @author Chris Cranford */ -@TestForIssue( jiraKey = "HHH-9663" ) -public class OneToOneLazyOrphanRemovalTest extends BaseCoreFunctionalTestCase { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Car.class, PaintColor.class, Engine.class }; +@TestForIssue(jiraKey = "HHH-9663") +@DomainModel( + annotatedClasses = { + OneToOneLazyOrphanRemovalTest.Car.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 - public void testOneToOneLazyOrphanRemoval() { + public void testOneToOneLazyOrphanRemoval(SessionFactoryScope scope) { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Initialize the data - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { final PaintColor color = new PaintColor( 1, "Red" ); final Engine engine = new Engine( 1, 275 ); final Car car = new Car( 1, engine, color ); @@ -45,13 +61,13 @@ public class OneToOneLazyOrphanRemovalTest extends BaseCoreFunctionalTestCase { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Test orphan removal for unidirectional relationship - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { final Car car = session.find( Car.class, 1 ); car.setEngine( null ); session.update( car ); } ); - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { final Car car = session.find( Car.class, 1 ); assertNull( car.getEngine() ); @@ -61,13 +77,13 @@ public class OneToOneLazyOrphanRemovalTest extends BaseCoreFunctionalTestCase { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Test orphan removal for bidirectional relationship - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { final Car car = session.find( Car.class, 1 ); car.setPaintColor( null ); session.update( car ); } ); - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { final Car car = session.find( Car.class, 1 ); assertNull( car.getPaintColor() ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneProxyOrphanRemovalTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/OneToOneProxyOrphanRemovalTest.java similarity index 66% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneProxyOrphanRemovalTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/OneToOneProxyOrphanRemovalTest.java index c2340ecf80..6f6ccba3ff 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/OneToOneProxyOrphanRemovalTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/OneToOneProxyOrphanRemovalTest.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.one2one; +package org.hibernate.orm.test.orphan.one2one; import javax.persistence.CascadeType; import javax.persistence.Entity; @@ -15,41 +15,51 @@ import javax.persistence.Id; import javax.persistence.OneToOne; import org.hibernate.Hibernate; -import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; -import org.junit.Test; 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.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotNull; /** * 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 * inline during invocation leading to constraint violation due to attempted removal * of the associated entity. - * + *

* This test case documents old behavior so that it can be preserved but allowing * us to also maintain the fix for {@code HHH-9663}. * * @author Chris Cranford */ -@TestForIssue( jiraKey = "HHH-11965" ) -public class OneToOneProxyOrphanRemovalTest extends BaseEntityManagerFunctionalTestCase { +@TestForIssue(jiraKey = "HHH-11965") +@Jpa( + annotatedClasses = { OneToOneProxyOrphanRemovalTest.Child.class, OneToOneProxyOrphanRemovalTest.Parent.class } +) +public class OneToOneProxyOrphanRemovalTest { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Child.class, Parent.class }; + @AfterEach + public void tearDown(EntityManagerFactoryScope scope){ + scope.inTransaction( + entityManager -> { + entityManager.createQuery( "delete from Parent" ).executeUpdate(); + entityManager.createQuery( "delete from Child" ).executeUpdate(); + } + ); } @Test - public void testUnproxyOneToOneWithCascade() { - Integer pId = doInJPA( this::entityManagerFactory, entityManager -> { - Parent p = new Parent(); - p.setChild( new Child() ); - entityManager.persist( p ); - return p.getId(); - } ); + public void testUnproxyOneToOneWithCascade(EntityManagerFactoryScope scope) { + Integer pId = scope.fromTransaction( + entityManager -> { + Parent p = new Parent(); + p.setChild( new Child() ); + entityManager.persist( p ); + return p.getId(); + } ); // This lambda fails because during flush the cascade of operations determine that the entity state // maintains the unwrapped proxy (from the getter) does not match the value maintained in the persistence @@ -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 // constraint violation exception. - doInJPA( this::entityManagerFactory, entityManager -> { - assertNotNull( entityManager.find( Parent.class, pId ) ); - } ); + scope.inTransaction( + entityManager -> + assertNotNull( entityManager.find( Parent.class, pId ) ) + ); } @Entity(name = "Child") public static class Child { private Integer id; + private String name; + @Id @GeneratedValue(strategy = GenerationType.AUTO) public Integer getId() { @@ -84,6 +97,9 @@ public class OneToOneProxyOrphanRemovalTest extends BaseEntityManagerFunctionalT @Entity(name = "Parent") public static class Parent { private Integer id; + + private String name; + private Child child; @Id @@ -106,5 +122,4 @@ public class OneToOneProxyOrphanRemovalTest extends BaseEntityManagerFunctionalT } } - } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/DeleteOneToOneOrphansTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/DeleteOneToOneOrphansTest.java new file mode 100644 index 0000000000..d4ddbad6e2 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/DeleteOneToOneOrphansTest.java @@ -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 . + */ +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() ); + } + ); + + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/pk/unidirectional/Employee.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/Employee.java similarity index 89% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/pk/unidirectional/Employee.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/Employee.java index 5485a557f3..54798e4517 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/pk/unidirectional/Employee.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/Employee.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.one2one.pk.unidirectional; +package org.hibernate.orm.test.orphan.one2one.fk.bidirectional; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/EmployeeInfo.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/EmployeeInfo.java similarity index 91% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/EmployeeInfo.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/EmployeeInfo.java index b3e011d8a0..b7cbfcfe6f 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/EmployeeInfo.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/EmployeeInfo.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.one2one.fk.bidirectional; +package org.hibernate.orm.test.orphan.one2one.fk.bidirectional; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/Mapping.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/Mapping.hbm.xml similarity index 92% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/Mapping.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/Mapping.hbm.xml index d9e694a560..b8c139635d 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/Mapping.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/Mapping.hbm.xml @@ -10,7 +10,7 @@ '-//Hibernate/Hibernate Mapping DTD 3.0//EN' 'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'> - + diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/multilevelcascade/DeleteMultiLevelOrphansTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/multilevelcascade/DeleteMultiLevelOrphansTest.java new file mode 100644 index 0000000000..2442921d91 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/multilevelcascade/DeleteMultiLevelOrphansTest.java @@ -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 . + */ +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() ); + } + ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/multilevelcascade/Preisregelung.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/multilevelcascade/Preisregelung.java similarity index 92% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/multilevelcascade/Preisregelung.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/multilevelcascade/Preisregelung.java index 6591c3308a..21c52ad919 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/multilevelcascade/Preisregelung.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/multilevelcascade/Preisregelung.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.one2one.fk.bidirectional.multilevelcascade; +package org.hibernate.orm.test.orphan.one2one.fk.bidirectional.multilevelcascade; import javax.persistence.CascadeType; import javax.persistence.Entity; diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/multilevelcascade/Tranche.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/multilevelcascade/Tranche.java similarity index 92% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/multilevelcascade/Tranche.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/multilevelcascade/Tranche.java index 56c334b644..b4c9c8d70e 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/multilevelcascade/Tranche.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/multilevelcascade/Tranche.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.one2one.fk.bidirectional.multilevelcascade; +package org.hibernate.orm.test.orphan.one2one.fk.bidirectional.multilevelcascade; import javax.persistence.*; diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/multilevelcascade/Tranchenmodell.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/multilevelcascade/Tranchenmodell.java similarity index 93% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/multilevelcascade/Tranchenmodell.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/multilevelcascade/Tranchenmodell.java index feecc95617..f2976718ce 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/multilevelcascade/Tranchenmodell.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/multilevelcascade/Tranchenmodell.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.one2one.fk.bidirectional.multilevelcascade; +package org.hibernate.orm.test.orphan.one2one.fk.bidirectional.multilevelcascade; import javax.persistence.*; import java.util.ArrayList; diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/multilevelcascade/X.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/multilevelcascade/X.java similarity index 91% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/multilevelcascade/X.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/multilevelcascade/X.java index 97aeac4761..6abb2b43e8 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/multilevelcascade/X.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/multilevelcascade/X.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.one2one.fk.bidirectional.multilevelcascade; +package org.hibernate.orm.test.orphan.one2one.fk.bidirectional.multilevelcascade; import javax.persistence.Entity; import javax.persistence.FetchType; diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/multilevelcascade/Y.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/multilevelcascade/Y.java similarity index 90% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/multilevelcascade/Y.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/multilevelcascade/Y.java index ff1e83a0eb..d851f5ebda 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/multilevelcascade/Y.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/bidirectional/multilevelcascade/Y.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.one2one.fk.bidirectional.multilevelcascade; +package org.hibernate.orm.test.orphan.one2one.fk.bidirectional.multilevelcascade; import javax.persistence.Entity; import javax.persistence.FetchType; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/composite/DeleteOneToOneOrphansTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/composite/DeleteOneToOneOrphansTest.java new file mode 100644 index 0000000000..0a907dfd93 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/composite/DeleteOneToOneOrphansTest.java @@ -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 . + */ +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() ); + } + ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/Employee.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/composite/Employee.java similarity index 90% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/Employee.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/composite/Employee.java index 8f38066758..3245869bb1 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/Employee.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/composite/Employee.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.one2one.fk.bidirectional; +package org.hibernate.orm.test.orphan.one2one.fk.composite; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/EmployeeInfo.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/composite/EmployeeInfo.java similarity index 96% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/EmployeeInfo.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/composite/EmployeeInfo.java index 8342526984..81a64ff72e 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/EmployeeInfo.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/composite/EmployeeInfo.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.one2one.fk.composite; +package org.hibernate.orm.test.orphan.one2one.fk.composite; import java.io.Serializable; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/Mapping.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/composite/Mapping.hbm.xml similarity index 92% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/Mapping.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/composite/Mapping.hbm.xml index 7d50a70e9b..d750f02dbf 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/Mapping.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/composite/Mapping.hbm.xml @@ -10,7 +10,7 @@ '-//Hibernate/Hibernate Mapping DTD 3.0//EN' 'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'> - + diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/bidirectional/DeleteOneToOneOrphansTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/bidirectional/DeleteOneToOneOrphansTest.java new file mode 100644 index 0000000000..00835e92b7 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/bidirectional/DeleteOneToOneOrphansTest.java @@ -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 . + */ +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() ); + } + ); + + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/bidirectional/Employee.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/bidirectional/Employee.java similarity index 88% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/bidirectional/Employee.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/bidirectional/Employee.java index 2a024c0fc3..f84bc57b30 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/bidirectional/Employee.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/bidirectional/Employee.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.one2one.fk.reversed.bidirectional; +package org.hibernate.orm.test.orphan.one2one.fk.reversed.bidirectional; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/bidirectional/EmployeeInfo.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/bidirectional/EmployeeInfo.java similarity index 90% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/bidirectional/EmployeeInfo.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/bidirectional/EmployeeInfo.java index 9de92c1793..409f3c7da3 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/bidirectional/EmployeeInfo.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/bidirectional/EmployeeInfo.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.one2one.fk.reversed.bidirectional; +package org.hibernate.orm.test.orphan.one2one.fk.reversed.bidirectional; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/bidirectional/Mapping.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/bidirectional/Mapping.hbm.xml similarity index 91% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/bidirectional/Mapping.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/bidirectional/Mapping.hbm.xml index 449a77eeaa..f53aa11221 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/bidirectional/Mapping.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/bidirectional/Mapping.hbm.xml @@ -10,7 +10,7 @@ '-//Hibernate/Hibernate Mapping DTD 3.0//EN' 'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'> - + diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/DeleteMultiLevelOrphansTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/DeleteMultiLevelOrphansTest.java new file mode 100644 index 0000000000..fadfaf42a3 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/DeleteMultiLevelOrphansTest.java @@ -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 . + */ +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() ); + } + ); + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/Preisregelung.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/Preisregelung.java similarity index 90% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/Preisregelung.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/Preisregelung.java index 558009eacc..852ee4bc60 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/Preisregelung.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/Preisregelung.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.one2one.fk.reversed.bidirectional.multilevelcascade; +package org.hibernate.orm.test.orphan.one2one.fk.reversed.bidirectional.multilevelcascade; import javax.persistence.CascadeType; import javax.persistence.Entity; diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/Tranche.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/Tranche.java similarity index 90% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/Tranche.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/Tranche.java index abada58a94..fa9520046d 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/Tranche.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/Tranche.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.one2one.fk.reversed.bidirectional.multilevelcascade; +package org.hibernate.orm.test.orphan.one2one.fk.reversed.bidirectional.multilevelcascade; import javax.persistence.Entity; import javax.persistence.FetchType; diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/Tranchenmodell.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/Tranchenmodell.java similarity index 93% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/Tranchenmodell.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/Tranchenmodell.java index adb504e874..9bcba83f41 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/Tranchenmodell.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/Tranchenmodell.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.one2one.fk.reversed.bidirectional.multilevelcascade; +package org.hibernate.orm.test.orphan.one2one.fk.reversed.bidirectional.multilevelcascade; import java.util.ArrayList; import java.util.List; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/unidirectional/DeleteOneToOneOrphansTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/unidirectional/DeleteOneToOneOrphansTest.java new file mode 100644 index 0000000000..1483cc3066 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/unidirectional/DeleteOneToOneOrphansTest.java @@ -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 . + */ +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() ); + } + ); + + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/unidirectional/Employee.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/unidirectional/Employee.java similarity index 88% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/unidirectional/Employee.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/unidirectional/Employee.java index 7db8d1a14c..f18a03ce05 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/unidirectional/Employee.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/unidirectional/Employee.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.one2one.fk.reversed.unidirectional; +package org.hibernate.orm.test.orphan.one2one.fk.reversed.unidirectional; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/unidirectional/EmployeeInfo.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/unidirectional/EmployeeInfo.java similarity index 86% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/unidirectional/EmployeeInfo.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/unidirectional/EmployeeInfo.java index bc221b9dc8..ac4f56f29b 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/unidirectional/EmployeeInfo.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/unidirectional/EmployeeInfo.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.one2one.fk.reversed.unidirectional; +package org.hibernate.orm.test.orphan.one2one.fk.reversed.unidirectional; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/unidirectional/Mapping.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/unidirectional/Mapping.hbm.xml similarity index 90% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/unidirectional/Mapping.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/unidirectional/Mapping.hbm.xml index 6ddbb9a38d..358ce1b76f 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/unidirectional/Mapping.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/fk/reversed/unidirectional/Mapping.hbm.xml @@ -10,7 +10,7 @@ '-//Hibernate/Hibernate Mapping DTD 3.0//EN' 'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'> - + diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/pk/bidirectional/DeleteOneToOneOrphansTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/pk/bidirectional/DeleteOneToOneOrphansTest.java new file mode 100644 index 0000000000..2f9a27a555 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/pk/bidirectional/DeleteOneToOneOrphansTest.java @@ -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 . + */ +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() ); + } + ); + + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/pk/bidirectional/Employee.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/pk/bidirectional/Employee.java similarity index 89% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/pk/bidirectional/Employee.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/pk/bidirectional/Employee.java index 3c8ded913c..8e37c9af53 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/pk/bidirectional/Employee.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/pk/bidirectional/Employee.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.one2one.pk.bidirectional; +package org.hibernate.orm.test.orphan.one2one.pk.bidirectional; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/pk/bidirectional/EmployeeInfo.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/pk/bidirectional/EmployeeInfo.java similarity index 91% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/pk/bidirectional/EmployeeInfo.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/pk/bidirectional/EmployeeInfo.java index 45715116be..7178cb5c25 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/pk/bidirectional/EmployeeInfo.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/pk/bidirectional/EmployeeInfo.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.one2one.pk.bidirectional; +package org.hibernate.orm.test.orphan.one2one.pk.bidirectional; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/pk/bidirectional/Mapping.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/pk/bidirectional/Mapping.hbm.xml similarity index 92% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/pk/bidirectional/Mapping.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/pk/bidirectional/Mapping.hbm.xml index cfc7d68f92..a1e3846d84 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/pk/bidirectional/Mapping.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/pk/bidirectional/Mapping.hbm.xml @@ -10,7 +10,7 @@ '-//Hibernate/Hibernate Mapping DTD 3.0//EN' 'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'> - + diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/pk/unidirectional/DeleteOneToOneOrphansTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/pk/unidirectional/DeleteOneToOneOrphansTest.java new file mode 100644 index 0000000000..3a9ce30627 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/pk/unidirectional/DeleteOneToOneOrphansTest.java @@ -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 . + */ +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() ); + } + ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/Employee.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/pk/unidirectional/Employee.java similarity index 89% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/Employee.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/pk/unidirectional/Employee.java index a779fcd8b0..b694f284a4 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/Employee.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/pk/unidirectional/Employee.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.one2one.fk.composite; +package org.hibernate.orm.test.orphan.one2one.pk.unidirectional; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/pk/unidirectional/EmployeeInfo.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/pk/unidirectional/EmployeeInfo.java similarity index 88% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/pk/unidirectional/EmployeeInfo.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/pk/unidirectional/EmployeeInfo.java index 783be52db0..7ecc02e27f 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/pk/unidirectional/EmployeeInfo.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/pk/unidirectional/EmployeeInfo.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.one2one.pk.unidirectional; +package org.hibernate.orm.test.orphan.one2one.pk.unidirectional; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/pk/unidirectional/Mapping.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/pk/unidirectional/Mapping.hbm.xml similarity index 91% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/pk/unidirectional/Mapping.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/pk/unidirectional/Mapping.hbm.xml index 41ff6e1234..2ba5ddc5a4 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/pk/unidirectional/Mapping.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/one2one/pk/unidirectional/Mapping.hbm.xml @@ -10,7 +10,7 @@ '-//Hibernate/Hibernate Mapping DTD 3.0//EN' 'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'> - + diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/onetomany/DeleteOneToManyOrphansTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/onetomany/DeleteOneToManyOrphansTest.java new file mode 100644 index 0000000000..ab35b2d08d --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/onetomany/DeleteOneToManyOrphansTest.java @@ -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 . + */ +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 ) ); + } + ); + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/onetomany/Feature.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/onetomany/Feature.java similarity index 95% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/onetomany/Feature.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/onetomany/Feature.java index 32081b2f81..19adab08fb 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/onetomany/Feature.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/onetomany/Feature.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.onetomany; +package org.hibernate.orm.test.orphan.onetomany; import javax.persistence.Entity; import javax.persistence.GeneratedValue; diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/onetomany/Product.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/onetomany/Product.java similarity index 95% rename from hibernate-core/src/test/java/org/hibernate/test/orphan/onetomany/Product.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/orphan/onetomany/Product.java index 6984140ded..9eddfdd933 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/onetomany/Product.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orphan/onetomany/Product.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.orphan.onetomany; +package org.hibernate.orm.test.orphan.onetomany; import java.util.ArrayList; import java.util.List; diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/Mail.hbm.xml b/hibernate-core/src/test/java/org/hibernate/test/orphan/Mail.hbm.xml index 1acb1d772a..ec09b3acb6 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/Mail.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/test/orphan/Mail.hbm.xml @@ -10,7 +10,7 @@ '-//Hibernate/Hibernate Mapping DTD 3.0//EN' 'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'> - + diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/OrphanTest.java b/hibernate-core/src/test/java/org/hibernate/test/orphan/OrphanTest.java deleted file mode 100755 index 9353cffcfa..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/OrphanTest.java +++ /dev/null @@ -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 . - */ -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(); - } - -} - diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/PropertyRefTest.java b/hibernate-core/src/test/java/org/hibernate/test/orphan/PropertyRefTest.java index 909978200f..34da5331dc 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/PropertyRefTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/orphan/PropertyRefTest.java @@ -7,24 +7,40 @@ package org.hibernate.test.orphan; import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; +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; /** * @author Steve Ebersole */ @TestForIssue(jiraKey = "HHH-565") -public class PropertyRefTest extends BaseCoreFunctionalTestCase { - @Override - public String[] getMappings() { - return new String[] { "orphan/User.hbm.xml", "orphan/Mail.hbm.xml" }; +@DomainModel( + xmlMappings = { + "org/hibernate/test/orphan/User.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 - public void testDeleteParentWithBidirOrphanDeleteCollectionBasedOnPropertyRef() { + public void testDeleteParentWithBidirOrphanDeleteCollectionBasedOnPropertyRef(SessionFactoryScope scope) { User user = new User( "test" ); - inTransaction( + scope.inTransaction( session -> { user.addMail( "test" ); user.addMail( "test" ); @@ -32,22 +48,21 @@ public class PropertyRefTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { User u = session.load( User.class, user.getId() ); session.delete( u ); } ); - inTransaction( - s -> { + scope.inTransaction( + session -> { session.createQuery( "delete from Mail where alias = :alias" ) .setParameter( "alias", "test" ) .executeUpdate(); session.createQuery( "delete from User where userid = :userid" ) .setParameter( "userid", "test" ) .executeUpdate(); - } ); } diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/User.hbm.xml b/hibernate-core/src/test/java/org/hibernate/test/orphan/User.hbm.xml index 97ef4ee97e..7b043516c5 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/User.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/test/orphan/User.hbm.xml @@ -10,7 +10,7 @@ '-//Hibernate/Hibernate Mapping DTD 3.0//EN' 'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'> - + diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/elementcollection/ElementCollectionOrphanTest.java b/hibernate-core/src/test/java/org/hibernate/test/orphan/elementcollection/ElementCollectionOrphanTest.java deleted file mode 100644 index 83de4af020..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/elementcollection/ElementCollectionOrphanTest.java +++ /dev/null @@ -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); - } - ); - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/DeleteOneToOneOrphansTest.java b/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/DeleteOneToOneOrphansTest.java deleted file mode 100644 index 94a21f4441..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/DeleteOneToOneOrphansTest.java +++ /dev/null @@ -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 . - */ -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(); - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/multilevelcascade/DeleteMultiLevelOrphansTest.java b/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/multilevelcascade/DeleteMultiLevelOrphansTest.java deleted file mode 100644 index 3fd0d3a20e..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/multilevelcascade/DeleteMultiLevelOrphansTest.java +++ /dev/null @@ -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 . - */ -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 - }; - } - -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/DeleteOneToOneOrphansTest.java b/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/DeleteOneToOneOrphansTest.java deleted file mode 100644 index 88443627e4..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/DeleteOneToOneOrphansTest.java +++ /dev/null @@ -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 . - */ -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(); - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/bidirectional/DeleteOneToOneOrphansTest.java b/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/bidirectional/DeleteOneToOneOrphansTest.java deleted file mode 100644 index 92e94f80b0..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/bidirectional/DeleteOneToOneOrphansTest.java +++ /dev/null @@ -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 . - */ -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(); - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/DeleteMultiLevelOrphansTest.java b/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/DeleteMultiLevelOrphansTest.java deleted file mode 100644 index 629cb9c350..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/bidirectional/multilevelcascade/DeleteMultiLevelOrphansTest.java +++ /dev/null @@ -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 . - */ -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 - }; - } - -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/unidirectional/DeleteOneToOneOrphansTest.java b/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/unidirectional/DeleteOneToOneOrphansTest.java deleted file mode 100644 index 17f7478631..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/unidirectional/DeleteOneToOneOrphansTest.java +++ /dev/null @@ -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 . - */ -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(); - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/pk/bidirectional/DeleteOneToOneOrphansTest.java b/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/pk/bidirectional/DeleteOneToOneOrphansTest.java deleted file mode 100644 index 9a810d15f8..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/pk/bidirectional/DeleteOneToOneOrphansTest.java +++ /dev/null @@ -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 . - */ -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(); - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/pk/unidirectional/DeleteOneToOneOrphansTest.java b/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/pk/unidirectional/DeleteOneToOneOrphansTest.java deleted file mode 100644 index 614d004196..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/pk/unidirectional/DeleteOneToOneOrphansTest.java +++ /dev/null @@ -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 . - */ -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(); - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/onetomany/DeleteOneToManyOrphansTest.java b/hibernate-core/src/test/java/org/hibernate/test/orphan/onetomany/DeleteOneToManyOrphansTest.java deleted file mode 100644 index 6eb5fc5bba..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/onetomany/DeleteOneToManyOrphansTest.java +++ /dev/null @@ -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 . - */ -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 - }; - } -} diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/SessionFactoryScopeParameterResolver.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/SessionFactoryScopeParameterResolver.java index c7f4bd2486..a770ed34d5 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/SessionFactoryScopeParameterResolver.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/SessionFactoryScopeParameterResolver.java @@ -6,6 +6,8 @@ */ package org.hibernate.testing.orm.junit; +import java.util.Optional; + import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.ParameterContext; import org.junit.jupiter.api.extension.ParameterResolutionException;