diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/version/VersionedBidirectionalOneToOneMergeTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/version/VersionedBidirectionalOneToOneMergeTest.java new file mode 100644 index 0000000000..fd3b9fe6ee --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/version/VersionedBidirectionalOneToOneMergeTest.java @@ -0,0 +1,73 @@ +package org.hibernate.orm.test.version; + +import java.util.UUID; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.OneToOne; +import jakarta.persistence.Version; + +@DomainModel( + annotatedClasses = { + VersionedBidirectionalOneToOneMergeTest.TestEntity.class, + VersionedBidirectionalOneToOneMergeTest.AnotherTestEntity.class + } +) +@SessionFactory +@JiraKey("HHH-16833") +public class VersionedBidirectionalOneToOneMergeTest { + + @Test + public void testMerge(SessionFactoryScope scope) { + AnotherTestEntity anotherTestEntity = new AnotherTestEntity(); + scope.inTransaction( + session -> { + session.persist( anotherTestEntity ); + } + ); + + scope.inTransaction( + session -> { + TestEntity testEntity = new TestEntity( anotherTestEntity ); + session.persist( testEntity ); + } + ); + } + + @Entity(name = "TestEntity") + public static class TestEntity { + @Id + UUID uuid = UUID.randomUUID(); + + @Version + Long version; + + @OneToOne + @JoinColumn(name = "OTHER_ENTITY_ID") + AnotherTestEntity anotherTestEntity; + + public TestEntity() { + } + + public TestEntity(AnotherTestEntity anotherTestEntity) { + this.anotherTestEntity = anotherTestEntity; + } + } + + @Entity(name = "AnotherTestEntity") + public static class AnotherTestEntity { + @Id + UUID uuid = UUID.randomUUID(); + + @OneToOne(mappedBy = "anotherTestEntity") + TestEntity testEntity; + + } +}