HHH-14659 Test query "join fetch" on lazy to-one associations with bytecode enhancement

This commit is contained in:
Yoann Rodière 2021-06-07 11:10:59 +02:00
parent 0eb187fae4
commit 50b8ad1f2b
12 changed files with 327 additions and 45 deletions

View File

@ -20,12 +20,17 @@
import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor;
import org.hibernate.bytecode.spi.BytecodeEnhancementMetadata;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.testing.BeforeClassOnce;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.bytecode.enhancement.EnhancementOptions;
import org.hibernate.testing.jdbc.SQLStatementInterceptor;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -36,6 +41,7 @@
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hibernate.annotations.FetchMode.JOIN;
import static org.junit.Assert.assertTrue;
/**
* Test for lazy uni-directional to-one (with JOIN fetching) when enhanced proxies are allowed
@ -61,17 +67,6 @@ protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBu
@Test
public void testOwnerIsProxy() {
inTransaction(
(session) -> {
final Customer customer = new Customer( 1, "Acme Brick" );
session.persist( customer );
final Order order = new Order( 1, customer, BigDecimal.ONE );
session.persist( order );
}
);
sqlStatementInterceptor.clear();
final EntityPersister orderDescriptor = sessionFactory().getMetamodel().entityPersister( Order.class );
final BytecodeEnhancementMetadata orderEnhancementMetadata = orderDescriptor.getBytecodeEnhancementMetadata();
assertThat( orderEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) );
@ -116,6 +111,50 @@ public void testOwnerIsProxy() {
);
}
@Test
@TestForIssue(jiraKey = "HHH-14659")
public void testQueryJoinFetch() {
Order order = fromTransaction( (session) -> {
final Order result = session.createQuery(
"select o from Order o join fetch o.customer",
Order.class )
.uniqueResult();
assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) );
return result;
} );
// The "join fetch" should have already initialized the property,
// so that the getter can safely be called outside of a session.
assertTrue( Hibernate.isPropertyInitialized( order, "customer" ) );
// The "join fetch" should have already initialized the associated entity.
Customer customer = order.getCustomer();
assertTrue( Hibernate.isInitialized( customer ) );
assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) );
}
@Before
public void createTestData() {
inTransaction(
(session) -> {
final Customer customer = new Customer( 1, "Acme Brick" );
session.persist( customer );
final Order order = new Order( 1, customer, BigDecimal.ONE );
session.persist( order );
}
);
sqlStatementInterceptor.clear();
}
@After
public void dropTestData() {
inTransaction(
(session) -> {
session.createQuery( "delete Order" ).executeUpdate();
session.createQuery( "delete Customer" ).executeUpdate();
}
);
}
@Entity( name = "Customer" )
@Table( name = "customer" )
public static class Customer {

View File

@ -12,6 +12,7 @@
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.Hibernate;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor;
@ -19,8 +20,10 @@
import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor;
import org.hibernate.bytecode.spi.BytecodeEnhancementMetadata;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.bytecode.enhancement.EnhancementOptions;
import org.hibernate.testing.jdbc.SQLStatementInterceptor;
@ -36,6 +39,7 @@
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
/**
* Test for lazy uni-directional to-one (with SELECT fetching) when enhanced proxies are allowed
@ -61,8 +65,6 @@ protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBu
@Test
public void testOwnerIsProxy() {
sqlStatementInterceptor.clear();
final EntityPersister orderDescriptor = sessionFactory().getMetamodel().entityPersister( Order.class );
final BytecodeEnhancementMetadata orderEnhancementMetadata = orderDescriptor.getBytecodeEnhancementMetadata();
assertThat( orderEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) );
@ -115,6 +117,27 @@ public void testOwnerIsProxy() {
);
}
@Test
@TestForIssue(jiraKey = "HHH-14659")
public void testQueryJoinFetch() {
Order order = fromTransaction( (session) -> {
final Order result = session.createQuery(
"select o from Order o join fetch o.customer",
Order.class )
.uniqueResult();
assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) );
return result;
} );
// The "join fetch" should have already initialized the property,
// so that the getter can safely be called outside of a session.
assertTrue( Hibernate.isPropertyInitialized( order, "customer" ) );
// The "join fetch" should have already initialized the associated entity.
Customer customer = order.getCustomer();
assertTrue( Hibernate.isInitialized( customer ) );
assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) );
}
@Before
public void createTestData() {
inTransaction(
@ -125,6 +148,7 @@ public void createTestData() {
session.persist( order );
}
);
sqlStatementInterceptor.clear();
}
@After

View File

@ -12,6 +12,7 @@
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.Hibernate;
import org.hibernate.annotations.LazyToOne;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
@ -22,11 +23,13 @@
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.bytecode.enhancement.EnhancementOptions;
import org.hibernate.testing.jdbc.SQLStatementInterceptor;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -37,6 +40,7 @@
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hibernate.annotations.LazyToOneOption.NO_PROXY;
import static org.junit.Assert.assertTrue;
/**
* Baseline test for uni-directional to-one, using an explicit @LazyToOne(NO_PROXY)
@ -64,17 +68,6 @@ protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBu
@Test
public void testOwnerIsProxy() {
inTransaction(
(session) -> {
final Customer customer = new Customer( 1, "Acme Brick" );
session.persist( customer );
final Order order = new Order( 1, customer, BigDecimal.ONE );
session.persist( order );
}
);
sqlStatementInterceptor.clear();
final EntityPersister orderDescriptor = sessionFactory().getMetamodel().entityPersister( Order.class );
final BytecodeEnhancementMetadata orderEnhancementMetadata = orderDescriptor.getBytecodeEnhancementMetadata();
assertThat( orderEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) );
@ -127,6 +120,40 @@ public void testOwnerIsProxy() {
);
}
@Test
@TestForIssue(jiraKey = "HHH-14659")
public void testQueryJoinFetch() {
Order order = fromTransaction( (session) -> {
final Order result = session.createQuery(
"select o from Order o join fetch o.customer",
Order.class )
.uniqueResult();
assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) );
return result;
} );
// The "join fetch" should have already initialized the property,
// so that the getter can safely be called outside of a session.
assertTrue( Hibernate.isPropertyInitialized( order, "customer" ) );
// The "join fetch" should have already initialized the associated entity.
Customer customer = order.getCustomer();
assertTrue( Hibernate.isInitialized( customer ) );
assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) );
}
@Before
public void createTestData() {
inTransaction(
(session) -> {
final Customer customer = new Customer( 1, "Acme Brick" );
session.persist( customer );
final Order order = new Order( 1, customer, BigDecimal.ONE );
session.persist( order );
}
);
sqlStatementInterceptor.clear();
}
@After
public void dropTestData() {
inTransaction(

View File

@ -11,6 +11,7 @@
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.hibernate.Hibernate;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor;
@ -20,6 +21,7 @@
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.bytecode.enhancement.EnhancementOptions;
import org.hibernate.testing.jdbc.SQLStatementInterceptor;
@ -35,6 +37,7 @@
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
/**
* @author Steve Ebersole
@ -60,8 +63,6 @@ protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBu
@Test
public void testOwnerIsProxy() {
sqlStatementInterceptor.clear();
final EntityPersister supplementalInfoDescriptor = sessionFactory().getMetamodel().entityPersister( SupplementalInfo.class );
final BytecodeEnhancementMetadata supplementalInfoEnhancementMetadata = supplementalInfoDescriptor.getBytecodeEnhancementMetadata();
assertThat( supplementalInfoEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) );
@ -113,6 +114,27 @@ public void testOwnerIsProxy() {
);
}
@Test
@TestForIssue(jiraKey = "HHH-14659")
public void testQueryJoinFetch() {
SupplementalInfo info = fromTransaction( (session) -> {
final SupplementalInfo result = session.createQuery(
"select s from SupplementalInfo s join fetch s.customer",
SupplementalInfo.class )
.uniqueResult();
assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) );
return result;
} );
// The "join fetch" should have already initialized the property,
// so that the getter can safely be called outside of a session.
assertTrue( Hibernate.isPropertyInitialized( info, "customer" ) );
// The "join fetch" should have already initialized the associated entity.
Customer customer = info.getCustomer();
assertTrue( Hibernate.isInitialized( customer ) );
assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) );
}
@Before
public void createTestData() {
inTransaction(
@ -123,6 +145,7 @@ public void createTestData() {
session.persist( supplementalInfo );
}
);
sqlStatementInterceptor.clear();
}
@After

View File

@ -11,6 +11,7 @@
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.hibernate.Hibernate;
import org.hibernate.annotations.LazyToOne;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
@ -20,11 +21,13 @@
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.bytecode.enhancement.EnhancementOptions;
import org.hibernate.testing.jdbc.SQLStatementInterceptor;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -33,6 +36,7 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hibernate.annotations.LazyToOneOption.NO_PROXY;
import static org.junit.Assert.assertTrue;
/**
* Baseline test for inverse (mappedBy) to-one, using an explicit @LazyToOne(NO_PROXY)
@ -58,17 +62,6 @@ protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBu
@Test
public void testOwnerIsProxy() {
inTransaction(
(session) -> {
final Customer customer = new Customer( 1, "Acme Brick" );
session.persist( customer );
final SupplementalInfo supplementalInfo = new SupplementalInfo( 1, customer, "extra details" );
session.persist( supplementalInfo );
}
);
sqlStatementInterceptor.clear();
final EntityPersister supplementalInfoDescriptor = sessionFactory().getMetamodel().entityPersister( SupplementalInfo.class );
final BytecodeEnhancementMetadata supplementalInfoEnhancementMetadata = supplementalInfoDescriptor.getBytecodeEnhancementMetadata();
assertThat( supplementalInfoEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) );
@ -140,6 +133,40 @@ public void testOwnerIsProxy() {
);
}
@Test
@TestForIssue(jiraKey = "HHH-14659")
public void testQueryJoinFetch() {
SupplementalInfo info = fromTransaction( (session) -> {
final SupplementalInfo result = session.createQuery(
"select s from SupplementalInfo s join fetch s.customer",
SupplementalInfo.class )
.uniqueResult();
assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) );
return result;
} );
// The "join fetch" should have already initialized the property,
// so that the getter can safely be called outside of a session.
assertTrue( Hibernate.isPropertyInitialized( info, "customer" ) );
// The "join fetch" should have already initialized the associated entity.
Customer customer = info.getCustomer();
assertTrue( Hibernate.isInitialized( customer ) );
assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) );
}
@Before
public void createTestData() {
inTransaction(
(session) -> {
final Customer customer = new Customer( 1, "Acme Brick" );
session.persist( customer );
final SupplementalInfo supplementalInfo = new SupplementalInfo( 1, customer, "extra details" );
session.persist( supplementalInfo );
}
);
sqlStatementInterceptor.clear();
}
@After
public void dropTestData() {
inTransaction(

View File

@ -11,6 +11,7 @@
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.hibernate.Hibernate;
import org.hibernate.annotations.Fetch;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
@ -21,6 +22,7 @@
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.bytecode.enhancement.EnhancementOptions;
import org.hibernate.testing.jdbc.SQLStatementInterceptor;
@ -37,6 +39,7 @@
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hibernate.annotations.FetchMode.JOIN;
import static org.junit.Assert.assertTrue;
/**
* @author Steve Ebersole
@ -62,8 +65,6 @@ protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBu
@Test
public void testOwnerIsProxy() {
sqlStatementInterceptor.clear();
final EntityPersister supplementalInfoDescriptor = sessionFactory().getMetamodel().entityPersister( SupplementalInfo.class );
final BytecodeEnhancementMetadata supplementalInfoEnhancementMetadata = supplementalInfoDescriptor.getBytecodeEnhancementMetadata();
assertThat( supplementalInfoEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) );
@ -110,6 +111,27 @@ public void testOwnerIsProxy() {
);
}
@Test
@TestForIssue(jiraKey = "HHH-14659")
public void testQueryJoinFetch() {
SupplementalInfo info = fromTransaction( (session) -> {
final SupplementalInfo result = session.createQuery(
"select s from SupplementalInfo s join fetch s.customer",
SupplementalInfo.class )
.uniqueResult();
assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) );
return result;
} );
// The "join fetch" should have already initialized the property,
// so that the getter can safely be called outside of a session.
assertTrue( Hibernate.isPropertyInitialized( info, "customer" ) );
// The "join fetch" should have already initialized the associated entity.
Customer customer = info.getCustomer();
assertTrue( Hibernate.isInitialized( customer ) );
assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) );
}
@Before
public void createTestData() {
inTransaction(
@ -120,6 +142,7 @@ public void createTestData() {
session.persist( supplementalInfo );
}
);
sqlStatementInterceptor.clear();
}
@After

View File

@ -11,6 +11,7 @@
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.hibernate.Hibernate;
import org.hibernate.annotations.Fetch;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
@ -18,6 +19,7 @@
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.bytecode.enhancement.EnhancementOptions;
import org.hibernate.testing.jdbc.SQLStatementInterceptor;
@ -31,6 +33,7 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hibernate.annotations.FetchMode.JOIN;
import static org.junit.Assert.assertTrue;
/**
* @author Steve Ebersole
@ -56,8 +59,6 @@ protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBu
@Test
public void testOwnerIsProxy() {
sqlStatementInterceptor.clear();
final EntityPersister supplementalInfoDescriptor = sessionFactory().getMetamodel().entityPersister( SupplementalInfo.class );
final BytecodeEnhancementMetadata supplementalInfoEnhancementMetadata = supplementalInfoDescriptor.getBytecodeEnhancementMetadata();
assertThat( supplementalInfoEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) );
@ -94,6 +95,27 @@ public void testOwnerIsProxy() {
);
}
@Test
@TestForIssue(jiraKey = "HHH-14659")
public void testQueryJoinFetch() {
SupplementalInfo info = fromTransaction( (session) -> {
final SupplementalInfo result = session.createQuery(
"select s from SupplementalInfo s join fetch s.customer",
SupplementalInfo.class )
.uniqueResult();
assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) );
return result;
} );
// The "join fetch" should have already initialized the property,
// so that the getter can safely be called outside of a session.
assertTrue( Hibernate.isPropertyInitialized( info, "customer" ) );
// The "join fetch" should have already initialized the associated entity.
Customer customer = info.getCustomer();
assertTrue( Hibernate.isInitialized( customer ) );
assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) );
}
@Before
public void createTestData() {
inTransaction(
@ -104,6 +126,7 @@ public void createTestData() {
session.persist( supplementalInfo );
}
);
sqlStatementInterceptor.clear();
}
@After

View File

@ -11,6 +11,7 @@
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.hibernate.Hibernate;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor;
@ -20,6 +21,7 @@
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.bytecode.enhancement.EnhancementOptions;
import org.hibernate.testing.jdbc.SQLStatementInterceptor;
@ -35,6 +37,7 @@
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
/**
* @author Steve Ebersole
@ -60,8 +63,6 @@ protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBu
@Test
public void testOwnerIsProxy() {
sqlStatementInterceptor.clear();
final EntityPersister supplementalInfoDescriptor = sessionFactory().getMetamodel().entityPersister( SupplementalInfo.class );
final BytecodeEnhancementMetadata supplementalInfoEnhancementMetadata = supplementalInfoDescriptor.getBytecodeEnhancementMetadata();
assertThat( supplementalInfoEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) );
@ -114,6 +115,27 @@ public void testOwnerIsProxy() {
);
}
@Test
@TestForIssue(jiraKey = "HHH-14659")
public void testQueryJoinFetch() {
SupplementalInfo info = fromTransaction( (session) -> {
final SupplementalInfo result = session.createQuery(
"select s from SupplementalInfo s join fetch s.customer",
SupplementalInfo.class )
.uniqueResult();
assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) );
return result;
} );
// The "join fetch" should have already initialized the property,
// so that the getter can safely be called outside of a session.
assertTrue( Hibernate.isPropertyInitialized( info, "customer" ) );
// The "join fetch" should have already initialized the associated entity.
Customer customer = info.getCustomer();
assertTrue( Hibernate.isInitialized( customer ) );
assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) );
}
@Before
public void createTestData() {
inTransaction(
@ -124,6 +146,7 @@ public void createTestData() {
session.persist( supplementalInfo );
}
);
sqlStatementInterceptor.clear();
}
@After

View File

@ -11,6 +11,7 @@
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.hibernate.Hibernate;
import org.hibernate.annotations.LazyToOne;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
@ -21,6 +22,7 @@
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.bytecode.enhancement.EnhancementOptions;
import org.hibernate.testing.jdbc.SQLStatementInterceptor;
@ -37,6 +39,7 @@
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hibernate.annotations.LazyToOneOption.NO_PROXY;
import static org.junit.Assert.assertTrue;
/**
* Baseline test for uni-directional one-to-one, using an explicit @LazyToOne(NO_PROXY) and allowing enhanced proxies
@ -62,8 +65,6 @@ protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBu
@Test
public void testOwnerIsProxy() {
sqlStatementInterceptor.clear();
final EntityPersister supplementalInfoDescriptor = sessionFactory().getMetamodel().entityPersister( SupplementalInfo.class );
final BytecodeEnhancementMetadata supplementalInfoEnhancementMetadata = supplementalInfoDescriptor.getBytecodeEnhancementMetadata();
assertThat( supplementalInfoEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) );
@ -116,6 +117,27 @@ public void testOwnerIsProxy() {
);
}
@Test
@TestForIssue(jiraKey = "HHH-14659")
public void testQueryJoinFetch() {
SupplementalInfo info = fromTransaction( (session) -> {
final SupplementalInfo result = session.createQuery(
"select s from SupplementalInfo s join fetch s.customer",
SupplementalInfo.class )
.uniqueResult();
assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) );
return result;
} );
// The "join fetch" should have already initialized the property,
// so that the getter can safely be called outside of a session.
assertTrue( Hibernate.isPropertyInitialized( info, "customer" ) );
// The "join fetch" should have already initialized the associated entity.
Customer customer = info.getCustomer();
assertTrue( Hibernate.isInitialized( customer ) );
assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) );
}
@Before
public void createTestData() {
inTransaction(
@ -126,6 +148,7 @@ public void createTestData() {
session.persist( supplementalInfo );
}
);
sqlStatementInterceptor.clear();
}
@After

View File

@ -20,6 +20,7 @@
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.bytecode.enhancement.EnhancementOptions;
import org.hibernate.testing.jdbc.SQLStatementInterceptor;
@ -72,6 +73,21 @@ public void testInheritedToOneLaziness() {
);
}
@Test
@TestForIssue(jiraKey = "HHH-14659")
public void testQueryJoinFetch() {
inTransaction(
(session) -> {
final Order order = session.createQuery( "select o from Order o join fetch o.customer", Order.class )
.uniqueResult();
assertTrue( Hibernate.isPropertyInitialized( order, "customer" ) );
Customer customer = order.getCustomer();
assertTrue( Hibernate.isInitialized( customer ) );
}
);
}
@Before
public void createTestData() {
inTransaction(

View File

@ -22,6 +22,7 @@
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.bytecode.enhancement.EnhancementOptions;
import org.hibernate.testing.jdbc.SQLStatementInterceptor;
@ -36,6 +37,7 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* @author Steve Ebersole
@ -100,6 +102,21 @@ public void testInheritedToOneLaziness() {
);
}
@Test
@TestForIssue(jiraKey = "HHH-14659")
public void testQueryJoinFetch() {
inTransaction(
(session) -> {
final Order order = session.createQuery( "select o from Order o join fetch o.customer", Order.class )
.uniqueResult();
assertTrue( Hibernate.isPropertyInitialized( order, "customer" ) );
Customer customer = order.getCustomer();
assertTrue( Hibernate.isInitialized( customer ) );
}
);
}
@Before
public void createTestData() {
inTransaction(

View File

@ -20,6 +20,7 @@
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.bytecode.enhancement.EnhancementOptions;
import org.hibernate.testing.jdbc.SQLStatementInterceptor;
@ -34,6 +35,7 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* @author Steve Ebersole
@ -72,6 +74,21 @@ public void testInheritedToOneLaziness() {
);
}
@Test
@TestForIssue(jiraKey = "HHH-14659")
public void testQueryJoinFetch() {
inTransaction(
(session) -> {
final Order order = session.createQuery( "select o from Order o join fetch o.customer", Order.class )
.uniqueResult();
assertTrue( Hibernate.isPropertyInitialized( order, "customer" ) );
Customer customer = order.getCustomer();
assertTrue( Hibernate.isInitialized( customer ) );
}
);
}
@Before
public void createTestData() {
inTransaction(