HHH-15967 Add test for issue

This commit is contained in:
Andrea Boriero 2023-01-03 14:27:39 +01:00 committed by Yoann Rodière
parent 475e4944df
commit ecde949c7f
2 changed files with 362 additions and 0 deletions

View File

@ -0,0 +1,181 @@
package org.hibernate.orm.test.bytecode.enhancement.lazy.proxy;
import java.io.Serializable;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.bytecode.enhancement.EnhancementOptions;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToOne;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
@RunWith(BytecodeEnhancerRunner.class)
@EnhancementOptions(lazyLoading = true)
@TestForIssue(jiraKey = "HHH-15967")
public class EagerOneToOneMappedByInDoubleEmbeddedTest extends BaseNonConfigCoreFunctionalTestCase {
@Override
public Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { EntityA.class, EntityB.class };
}
@Before
public void prepare() {
inTransaction( s -> {
EntityA entityA = new EntityA( 1 );
EntityB entityB = new EntityB( 2 );
EmbeddedValueInA embeddedValueInA = new EmbeddedValueInA();
EmbeddedValueInB embeddedValueInB = new EmbeddedValueInB();
embeddedValueInA.setEntityB( entityB );
embeddedValueInB.setEntityA( entityA );
entityB.setEmbedded( embeddedValueInB );
entityA.setEmbedded( embeddedValueInA );
s.persist( entityA );
s.persist( entityB );
} );
}
@After
public void tearDown() {
inTransaction(
session -> {
session.createQuery( "delete from EntityB" ).executeUpdate();
session.createQuery( "delete from EntityA" ).executeUpdate();
}
);
}
@Test
public void testGetEntityA() {
inTransaction(
session -> {
EntityA entityA = session.get( EntityA.class, 1 );
assertThat( entityA ).isNotNull();
EntityB entityB = entityA.getEmbedded().getEntityB();
assertThat( entityB ).isNotNull();
assertThat( entityB.getEmbedded().getEntityA() ).isNotNull();
assertThat( entityB.getEmbedded().getEntityA() ).isEqualTo( entityA );
}
);
}
@Test
public void testGetReferenceEntityA() {
inTransaction(
session -> {
EntityA entityA = session.getReference( EntityA.class, 1 );
assertThat( entityA ).isNotNull();
EntityB entityB = entityA.getEmbedded().getEntityB();
assertThat( entityB ).isNotNull();
assertThat( entityB.getEmbedded().getEntityA() ).isNotNull();
assertThat( entityB.getEmbedded().getEntityA() ).isEqualTo( entityA );
}
);
}
@Entity(name = "EntityA")
public static class EntityA {
@Id
private Integer id;
@Embedded
private EmbeddedValueInA embedded = new EmbeddedValueInA();
public EntityA() {
}
private EntityA(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public EmbeddedValueInA getEmbedded() {
return embedded;
}
public void setEmbedded(EmbeddedValueInA embedded) {
this.embedded = embedded;
}
}
@Embeddable
public static class EmbeddedValueInA implements Serializable {
@OneToOne(mappedBy = "embedded.entityA")
private EntityB entityB;
public EmbeddedValueInA() {
}
public EntityB getEntityB() {
return entityB;
}
public void setEntityB(
EntityB entityB) {
this.entityB = entityB;
}
}
@Entity(name = "EntityB")
public static class EntityB {
@Id
private Integer id;
@Embedded
private EmbeddedValueInB embedded = new EmbeddedValueInB();
public EntityB() {
}
private EntityB(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public EmbeddedValueInB getEmbedded() {
return embedded;
}
public void setEmbedded(EmbeddedValueInB embedded) {
this.embedded = embedded;
}
}
@Embeddable
public static class EmbeddedValueInB implements Serializable {
@OneToOne
private EntityA entityA;
public EmbeddedValueInB() {
}
public EntityA getEntityA() {
return entityA;
}
public void setEntityA(
EntityA entityA) {
this.entityA = entityA;
}
}
}

View File

@ -0,0 +1,181 @@
package org.hibernate.orm.test.bytecode.enhancement.lazy.proxy;
import java.io.Serializable;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.bytecode.enhancement.EnhancementOptions;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToOne;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
@RunWith(BytecodeEnhancerRunner.class)
@EnhancementOptions(lazyLoading = true)
@TestForIssue(jiraKey = "HHH-15967")
public class LazyOneToOneMappedByInDoubleEmbeddedTest extends BaseNonConfigCoreFunctionalTestCase {
@Override
public Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { EntityA.class, EntityB.class };
}
@Before
public void prepare() {
inTransaction( s -> {
EntityA entityA = new EntityA( 1 );
EntityB entityB = new EntityB( 2 );
EmbeddedValueInA embeddedValueInA = new EmbeddedValueInA();
EmbeddedValueInB embeddedValueInB = new EmbeddedValueInB();
embeddedValueInA.setEntityB( entityB );
embeddedValueInB.setEntityA( entityA );
entityB.setEmbedded( embeddedValueInB );
entityA.setEmbedded( embeddedValueInA );
s.persist( entityA );
s.persist( entityB );
} );
}
@After
public void tearDown() {
inTransaction(
session -> {
session.createQuery( "delete from EntityB" ).executeUpdate();
session.createQuery( "delete from EntityA" ).executeUpdate();
}
);
}
@Test
public void testGetEntityA() {
inTransaction(
session -> {
EntityA entityA = session.get( EntityA.class, 1 );
assertThat( entityA ).isNotNull();
EntityB entityB = entityA.getEmbedded().getEntityB();
assertThat( entityB ).isNotNull();
assertThat( entityB.getEmbedded().getEntityA() ).isNotNull();
assertThat( entityB.getEmbedded().getEntityA() ).isEqualTo( entityA );
}
);
}
@Test
public void testGetReferenceEntityA() {
inTransaction(
session -> {
EntityA entityA = session.getReference( EntityA.class, 1 );
assertThat( entityA ).isNotNull();
EntityB entityB = entityA.getEmbedded().getEntityB();
assertThat( entityB ).isNotNull();
assertThat( entityB.getEmbedded().getEntityA() ).isNotNull();
assertThat( entityB.getEmbedded().getEntityA() ).isEqualTo( entityA );
}
);
}
@Entity(name = "EntityA")
public static class EntityA {
@Id
private Integer id;
@Embedded
private EmbeddedValueInA embedded = new EmbeddedValueInA();
public EntityA() {
}
private EntityA(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public EmbeddedValueInA getEmbedded() {
return embedded;
}
public void setEmbedded(EmbeddedValueInA embedded) {
this.embedded = embedded;
}
}
@Embeddable
public static class EmbeddedValueInA implements Serializable {
@OneToOne(mappedBy = "embedded.entityA", fetch = FetchType.LAZY)
private EntityB entityB;
public EmbeddedValueInA() {
}
public EntityB getEntityB() {
return entityB;
}
public void setEntityB(
EntityB entityB) {
this.entityB = entityB;
}
}
@Entity(name = "EntityB")
public static class EntityB {
@Id
private Integer id;
@Embedded
private EmbeddedValueInB embedded = new EmbeddedValueInB();
public EntityB() {
}
private EntityB(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public EmbeddedValueInB getEmbedded() {
return embedded;
}
public void setEmbedded(EmbeddedValueInB embedded) {
this.embedded = embedded;
}
}
@Embeddable
public static class EmbeddedValueInB implements Serializable {
@OneToOne
private EntityA entityA;
public EmbeddedValueInB() {
}
public EntityA getEntityA() {
return entityA;
}
public void setEntityA(
EntityA entityA) {
this.entityA = entityA;
}
}
}