HHH-13725 - Implement ManyToOne associations support

This commit is contained in:
Andrea Boriero 2019-11-14 10:14:49 +00:00 committed by Steve Ebersole
parent bfb640196d
commit 86dee1f66c
1 changed files with 64 additions and 24 deletions

View File

@ -46,7 +46,7 @@ import static org.junit.Assert.assertTrue;
public class ManyToOneTest {
@Test
public void testHqlSelect(SessionFactoryScope scope) {
public void testHqlSelectWithoutJoin(SessionFactoryScope scope) {
StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
@ -95,13 +95,41 @@ public class ManyToOneTest {
.uniqueResult();
// the ManyToOne is eager but the value is not null so a second query is executed
assertThat( statistics.getPrepareStatementCount(), is( 2L ) );
assertThat( otherEntity.getName(), is( "Bar" ) );
SimpleEntity simpleEntity = otherEntity.getSimpleEntity();
assertFalse( Hibernate.isInitialized( simpleEntity ) );
AnotherSimpleEntity anotherSimpleEntity = otherEntity.getAnotherSimpleEntity();
assertTrue( Hibernate.isInitialized( anotherSimpleEntity ) );
assertThat( statistics.getPrepareStatementCount(), is( 2L ) );
}
);
}
@Test
public void testHQLSelectWithJoin(SessionFactoryScope scope) {
StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> {
OtherEntity otherEntity = session.
createQuery( "from OtherEntity o join o.simpleEntity", OtherEntity.class )
.uniqueResult();
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
assertThat( otherEntity.getName(), is( "Bar" ) );
SimpleEntity simpleEntity = otherEntity.getSimpleEntity();
assertFalse( Hibernate.isInitialized( simpleEntity ) );
assertThat( simpleEntity, notNullValue() );
assertThat( simpleEntity.getName(), is( "Fab" ) );
assertThat( statistics.getPrepareStatementCount(), is( 2L ) );
AnotherSimpleEntity anotherSimpleEntity = otherEntity.getAnotherSimpleEntity();
assertTrue( Hibernate.isInitialized( anotherSimpleEntity ) );
assertThat( statistics.getPrepareStatementCount(), is( 2L ) );
}
);
@ -117,15 +145,18 @@ public class ManyToOneTest {
createQuery( "from OtherEntity o join fetch o.simpleEntity", OtherEntity.class )
.uniqueResult();
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
assertThat( otherEntity.getName(), is( "Bar" ) );
assertTrue( Hibernate.isInitialized( otherEntity.getSimpleEntity() ) );
assertThat( otherEntity.getSimpleEntity(), notNullValue() );
assertThat( otherEntity.getSimpleEntity().getName(), is( "Fab" ) );
SimpleEntity simpleEntity = otherEntity.getSimpleEntity();
assertTrue( Hibernate.isInitialized( simpleEntity ) );
assertThat( simpleEntity, notNullValue() );
assertThat( simpleEntity.getName(), is( "Fab" ) );
AnotherSimpleEntity anotherSimpleEntity = otherEntity.getAnotherSimpleEntity();
assertTrue( Hibernate.isInitialized( anotherSimpleEntity ) );
assertThat( statistics.getPrepareStatementCount(), is(1L) );
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
}
);
}
@ -143,13 +174,17 @@ public class ManyToOneTest {
)
.uniqueResult();
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
assertThat( otherEntity.getName(), is( "Bar" ) );
assertTrue( Hibernate.isInitialized( otherEntity.getSimpleEntity() ) );
assertThat( otherEntity.getSimpleEntity(), notNullValue() );
assertThat( otherEntity.getSimpleEntity().getName(), is( "Fab" ) );
SimpleEntity simpleEntity = otherEntity.getSimpleEntity();
assertTrue( Hibernate.isInitialized( simpleEntity ) );
assertThat( simpleEntity, notNullValue() );
assertThat( simpleEntity.getName(), is( "Fab" ) );
assertTrue( Hibernate.isInitialized( otherEntity.getAnotherSimpleEntity() ) );
assertThat( otherEntity.getAnotherSimpleEntity(), nullValue() );
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
}
);
@ -172,17 +207,21 @@ public class ManyToOneTest {
scope.inTransaction(
session -> {
OtherEntity otherEntity = session.
createQuery( "from OtherEntity o join fetch o.simpleEntity left join fetch o.anotherSimpleEntity", OtherEntity.class )
createQuery(
"from OtherEntity o join fetch o.simpleEntity left join fetch o.anotherSimpleEntity",
OtherEntity.class
)
.uniqueResult();
// the ManyToOne is eager but the value is not null so a second query is executed
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
assertThat( otherEntity.getName(), is( "Bar" ) );
SimpleEntity simpleEntity = otherEntity.getSimpleEntity();
assertTrue( Hibernate.isInitialized( simpleEntity ) );
AnotherSimpleEntity anotherSimpleEntity = otherEntity.getAnotherSimpleEntity();
assertTrue( Hibernate.isInitialized( anotherSimpleEntity ) );
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
}
);
@ -195,10 +234,11 @@ public class ManyToOneTest {
scope.inTransaction(
session -> {
OtherEntity otherEntity = session.get( OtherEntity.class, 2 );
assertThat( otherEntity.getName(), is( "Bar" ) );
assertFalse( Hibernate.isInitialized( otherEntity.getSimpleEntity() ) );
assertTrue( Hibernate.isInitialized( otherEntity.getAnotherSimpleEntity() ) );
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
}
);