diff --git a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/group/LGMB_From.java b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/group/LGMB_From.java new file mode 100644 index 0000000000..5a8e33de53 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/group/LGMB_From.java @@ -0,0 +1,98 @@ +package org.hibernate.test.bytecode.enhancement.lazy.group; + +import javax.persistence.Access; +import javax.persistence.AccessType; +import javax.persistence.Basic; +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +import org.hibernate.annotations.LazyGroup; +import org.hibernate.annotations.LazyToOne; +import org.hibernate.annotations.LazyToOneOption; + +/** + * Source of a LazyToOne - relationship with FK on the other side + * + * @author Jan-Oliver Lustig, Sebastian Viefhaus + */ +@Entity +@Access(AccessType.FIELD) +public class LGMB_From { + + @Column(length = 50, nullable = false) + private String name; + + // Lazy-Attribut without LazyGroup-Annotation (therefore Default-LazyGroup) + @Column(length = 65000, columnDefinition = "text") + @Basic(fetch = FetchType.LAZY) + private String bigText; + + // Lazy-Assoziation with mappdedBy in own LazyGroup + @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "fromRelation", optional = true) + @LazyToOne(LazyToOneOption.NO_PROXY) + @LazyGroup(value = "toRelationLazyGroup") + private LGMB_To toRelation; + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(nullable = false) + private Long id; + + /** + * Default Constructor + */ + public LGMB_From() { + super(); + } + + /** + * Constructor + */ + public LGMB_From(String name) { + super(); + this.name = name; + } + + /** + * @return the toRelation + */ + public LGMB_To getToRelation() { + return toRelation; + } + + /** + * @param toRelation the toRelation to set + */ + public void setToRelation(LGMB_To toRelation) { + this.toRelation = toRelation; + } + + public String getBigText() { + return bigText; + } + + /** + * @return id + */ + public Long getId() { + return id; + } + + /** + * @param id + */ + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/group/LGMB_To.java b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/group/LGMB_To.java new file mode 100644 index 0000000000..ab341f53f7 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/group/LGMB_To.java @@ -0,0 +1,78 @@ +package org.hibernate.test.bytecode.enhancement.lazy.group; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +import org.hibernate.annotations.AccessType; + +/** + * Target of a LazyToOne - relationship (Foreignkey on this side) + * + * @author Jan-Oliver Lustig, Sebastian Viefhaus + */ +@Entity +public class LGMB_To { + @Id + private static final long serialVersionUID = 1L; + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(nullable = false) + @AccessType("property") + private Long id; + + @OneToOne + LGMB_From fromRelation; + + @Column(length = 50, nullable = false) + private String name; + + /** + * Default Constructor + */ + public LGMB_To() { + super(); + } + + /** + * Constructor + */ + public LGMB_To(String name) { + super(); + this.name = name; + } + + /** + * @return the fromRelation + */ + public LGMB_From getFromRelation() { + return fromRelation; + } + + /** + * @param fromRelation the fromRelation to set + */ + public void setFromRelation(LGMB_From fromRelation) { + this.fromRelation = fromRelation; + } + + /** + * @return id + */ + public Long getId() { + return id; + } + + /** + * @param id + */ + public void setId(Long id) { + this.id = id; + } + + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/group/LazyGroupMappedByTest.java b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/group/LazyGroupMappedByTest.java new file mode 100644 index 0000000000..84d1c8f0e4 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/group/LazyGroupMappedByTest.java @@ -0,0 +1,87 @@ +package org.hibernate.test.bytecode.enhancement.lazy.group; + +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; +import static org.junit.Assert.assertEquals; + +import org.hibernate.stat.SessionStatistics; +import org.hibernate.stat.Statistics; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * Testing OneToOne LazyToOne association + * + * @author Jan-Oliver Lustig, Sebastian Viefhaus + */ +@TestForIssue(jiraKey = "HHH-11986") +@RunWith(BytecodeEnhancerRunner.class) +public class LazyGroupMappedByTest extends BaseCoreFunctionalTestCase { + + public Class[] getAnnotatedClasses() { + return new Class[] { LGMB_From.class, LGMB_To.class }; + } + + @Test + @TestForIssue(jiraKey = "HHH-11986") + public void test() { + Long fromId = createEntities(); + + Statistics stats = sessionFactory().getStatistics(); + stats.setStatisticsEnabled( true ); + stats.clear(); + + doInHibernate( + this::sessionFactory, session -> { + + SessionStatistics sessionStats = session.getStatistics(); + + // Should be loaded lazy. + LGMB_From from = session.get( LGMB_From.class, fromId ); + assertEquals( 1, sessionStats.getEntityCount() ); + assertEquals( 1, stats.getPrepareStatementCount() ); + + // Lazy text is accessed, toRelation should not be read yet. + String bigText = from.getBigText(); + assertEquals( 1, sessionStats.getEntityCount() ); + assertEquals( 2, stats.getPrepareStatementCount() ); + + // Second table is accessed and the lazy one should be reloaded. + LGMB_To to = from.getToRelation(); + assertEquals( 2, sessionStats.getEntityCount() ); + assertEquals( 3, stats.getPrepareStatementCount() ); + + to.getFromRelation().getName(); + assertEquals( 3, stats.getPrepareStatementCount() ); + } + ); + } + + /** + * Hilfsmethode: Eine Entität anlegen + * + * @return ID der Quell-Entität + */ + public Long createEntities() { + return doInHibernate( + this::sessionFactory, session -> { + session.createNativeQuery( "DELETE FROM LGMB_TO" ).executeUpdate(); + session.createNativeQuery( "DELETE FROM LGMB_FROM" ).executeUpdate(); + + LGMB_From from = new LGMB_From( "A" ); + LGMB_To to = new LGMB_To( "B" ); + from.setToRelation( to ); + to.setFromRelation( from ); + + session.save( from ); + session.flush(); + + return from.getId(); + } + ); + } + +}