HHH-13725 Add more tests

This commit is contained in:
Andrea Boriero 2019-11-18 09:37:39 +00:00 committed by Steve Ebersole
parent 4ca9617b6f
commit 461e559184
4 changed files with 444 additions and 121 deletions

View File

@ -9,10 +9,14 @@ package org.hibernate.orm.test.sql.exec.onetoone;
import java.util.Calendar; import java.util.Calendar;
import org.hibernate.Hibernate; import org.hibernate.Hibernate;
import org.hibernate.stat.spi.StatisticsImplementor;
import org.hibernate.testing.junit5.SessionFactoryBasedFunctionalTest;
import org.hibernate.testing.orm.domain.gambit.EntityWithLazyOneToOne; import org.hibernate.testing.orm.domain.gambit.EntityWithLazyOneToOne;
import org.hibernate.testing.orm.domain.gambit.SimpleEntity; import org.hibernate.testing.orm.domain.gambit.SimpleEntity;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -20,6 +24,7 @@ import org.junit.jupiter.api.Test;
import org.hamcrest.CoreMatchers; import org.hamcrest.CoreMatchers;
import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
@ -29,75 +34,103 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* @author Andrea Boriero * @author Andrea Boriero
*/ */
public class EntityWithLazyOneToOneTest extends SessionFactoryBasedFunctionalTest { @DomainModel(
annotatedClasses = {
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] {
EntityWithLazyOneToOne.class, EntityWithLazyOneToOne.class,
SimpleEntity.class SimpleEntity.class
}; }
} )
@ServiceRegistry
@SessionFactory(generateStatistics = true)
public class EntityWithLazyOneToOneTest {
@BeforeEach @BeforeEach
public void setUp() { public void setUp(SessionFactoryScope scope) {
EntityWithLazyOneToOne entity = new EntityWithLazyOneToOne( 1, "first", Integer.MAX_VALUE );
SimpleEntity other = new SimpleEntity( scope.inTransaction( session -> {
2, EntityWithLazyOneToOne entity = new EntityWithLazyOneToOne( 1, "first", Integer.MAX_VALUE );
Calendar.getInstance().getTime(),
null,
Integer.MAX_VALUE,
Long.MAX_VALUE,
null
);
entity.setOther( other ); SimpleEntity other = new SimpleEntity(
2,
Calendar.getInstance().getTime(),
null,
Integer.MAX_VALUE,
Long.MAX_VALUE,
null
);
inTransaction( session -> { entity.setOther( other );
session.save( other );
session.save( entity );
} );
EntityWithLazyOneToOne entity = new EntityWithLazyOneToOne( 3, "second", Integer.MAX_VALUE );
scope.inTransaction( session -> {
SimpleEntity other = new SimpleEntity(
4,
Calendar.getInstance().getTime(),
null,
1,
Long.MAX_VALUE,
null
);
entity.setOther( other );
session.save( other ); session.save( other );
session.save( entity ); session.save( entity );
} ); } );
} }
@AfterEach @AfterEach
public void tearDown() { public void tearDown(SessionFactoryScope scope) {
inTransaction( session -> { scope.inTransaction(
deleteAll(); session -> {
} ); session.createQuery( "delete from EntityWithLazyOneToOne" ).executeUpdate();
session.createQuery( "delete from SimpleEntity" ).executeUpdate();
}
);
} }
@Test @Test
public void testGet() { public void testGet(SessionFactoryScope scope) {
inTransaction( StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> { session -> {
final EntityWithLazyOneToOne loaded = session.get( EntityWithLazyOneToOne.class, 1 ); final EntityWithLazyOneToOne loaded = session.get( EntityWithLazyOneToOne.class, 1 );
assert loaded != null; assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
assertThat( loaded, notNullValue() );
assertThat( loaded.getName(), equalTo( "first" ) ); assertThat( loaded.getName(), equalTo( "first" ) );
SimpleEntity other = loaded.getOther();
assertFalse( assertFalse(
Hibernate.isInitialized( loaded.getOther() ), Hibernate.isInitialized( other ),
"The lazy association should not be initialized" "The lazy association should not be initialized"
); );
SimpleEntity loadedOther = loaded.getOther(); assertThat( other, notNullValue() );
assert loadedOther != null; assertThat( other.getId(), equalTo( 2 ) );
assertThat( loaded.getOther().getId(), equalTo( 2 ) );
assertFalse( assertFalse(
Hibernate.isInitialized( loaded.getOther() ), Hibernate.isInitialized( other ),
"getId() should not trigger the lazy association initialization" "getId() should not trigger the lazy association initialization"
); );
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
loadedOther.getSomeDate();
other.getSomeDate();
assertTrue( assertTrue(
Hibernate.isInitialized( loaded.getOther() ), Hibernate.isInitialized( other ),
"The lazy association should be initialized" "The lazy association should be initialized"
); );
assertThat( statistics.getPrepareStatementCount(), is( 2L ) );
} }
); );
inTransaction( scope.inTransaction(
session -> { session -> {
final SimpleEntity loaded = session.get( SimpleEntity.class, 2 ); final SimpleEntity loaded = session.get( SimpleEntity.class, 2 );
assert loaded != null; assert loaded != null;
@ -107,22 +140,95 @@ public class EntityWithLazyOneToOneTest extends SessionFactoryBasedFunctionalTes
} }
@Test @Test
public void testHqlSelect() { public void testHqlSelectParentAttribute(SessionFactoryScope scope) {
StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
inTransaction( statistics.clear();
scope.inTransaction(
session -> { session -> {
final String value = session.createQuery( final String value = session.createQuery(
"select e.name from EntityWithLazyOneToOne e where e.other.id = 2", "select e.name from EntityWithLazyOneToOne e where e.other.id = 2",
String.class String.class
).uniqueResult(); ).uniqueResult();
assertThat( value, equalTo( "first" ) ); assertThat( value, equalTo( "first" ) );
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
} }
); );
} }
private void deleteAll() { @Test
public void testHqlSelectParent(SessionFactoryScope scope) {
StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> {
final EntityWithLazyOneToOne loaded = session.createQuery(
"select e from EntityWithLazyOneToOne e where e.other.id = 2",
EntityWithLazyOneToOne.class
).uniqueResult();
inTransaction( assertThat( loaded.getName(), equalTo( "first" ) );
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
SimpleEntity other = loaded.getOther();
assertFalse(
Hibernate.isInitialized( other ),
"The lazy association should not be initialized"
);
assertThat( other, notNullValue() );
assertThat( other.getId(), equalTo( 2 ) );
assertFalse(
Hibernate.isInitialized( other ),
"getId() should not trigger the lazy association initialization"
);
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
other.getSomeDate();
assertTrue(
Hibernate.isInitialized( other ),
"The lazy association should be initialized"
);
assertThat( statistics.getPrepareStatementCount(), is( 2L ) );
}
);
}
@Test
public void testHqlSelectParentJoinFetch(SessionFactoryScope scope) {
StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> {
final EntityWithLazyOneToOne loaded = session.createQuery(
"select e from EntityWithLazyOneToOne e join fetch e.other where e.other.id = 2",
EntityWithLazyOneToOne.class
).uniqueResult();
assertThat( loaded.getName(), equalTo( "first" ) );
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
SimpleEntity other = loaded.getOther();
assertTrue(
Hibernate.isInitialized( other ),
"The lazy association should not initialized"
);
assertThat( other, notNullValue() );
assertThat( other.getId(), equalTo( 2 ) );
other.getSomeDate();
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
}
);
}
@Test
public void testRemove(SessionFactoryScope scope){
scope.inTransaction(
session -> { session -> {
final EntityWithLazyOneToOne loaded = session.get( EntityWithLazyOneToOne.class, 1 ); final EntityWithLazyOneToOne loaded = session.get( EntityWithLazyOneToOne.class, 1 );
assert loaded != null; assert loaded != null;
@ -131,20 +237,13 @@ public class EntityWithLazyOneToOneTest extends SessionFactoryBasedFunctionalTes
} }
); );
inTransaction( scope.inTransaction(
session -> { session -> {
final EntityWithLazyOneToOne notfound = session.find( EntityWithLazyOneToOne.class, 1 ); final EntityWithLazyOneToOne notfound = session.find( EntityWithLazyOneToOne.class, 1 );
assertThat( notfound, CoreMatchers.nullValue() ); assertThat( notfound, CoreMatchers.nullValue() );
} }
); );
inTransaction(
session -> {
final SimpleEntity simpleEntity = session.find( SimpleEntity.class, 2 );
assertThat( simpleEntity, notNullValue() );
session.remove( simpleEntity );
}
);
} }
} }

View File

@ -8,42 +8,40 @@ package org.hibernate.orm.test.sql.exec.onetoone;
import java.util.Calendar; import java.util.Calendar;
import org.hibernate.testing.junit5.SessionFactoryBasedFunctionalTest; import org.hibernate.Hibernate;
import org.hibernate.stat.spi.StatisticsImplementor;
import org.hibernate.testing.orm.domain.gambit.EntityWithOneToOneJoinTable; import org.hibernate.testing.orm.domain.gambit.EntityWithOneToOneJoinTable;
import org.hibernate.testing.orm.domain.gambit.SimpleEntity; import org.hibernate.testing.orm.domain.gambit.SimpleEntity;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
/** /**
* @author Andrea Boriero * @author Andrea Boriero
*/ */
public class EntityWithOneToOneJoinTableTest extends SessionFactoryBasedFunctionalTest { @DomainModel(
annotatedClasses = {
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] {
EntityWithOneToOneJoinTable.class, EntityWithOneToOneJoinTable.class,
SimpleEntity.class SimpleEntity.class
}; }
} )
@ServiceRegistry
@SessionFactory(generateStatistics = true)
public class EntityWithOneToOneJoinTableTest {
@AfterEach @BeforeEach
public void tearDown() { public void setUp(SessionFactoryScope scope) {
inTransaction(
session -> {
final EntityWithOneToOneJoinTable loaded = session.get( EntityWithOneToOneJoinTable.class, 1 );
session.delete( loaded );
session.delete( loaded.getOther() );
}
);
}
@Test
public void testOperations() {
EntityWithOneToOneJoinTable entity = new EntityWithOneToOneJoinTable( 1, "first", Integer.MAX_VALUE ); EntityWithOneToOneJoinTable entity = new EntityWithOneToOneJoinTable( 1, "first", Integer.MAX_VALUE );
SimpleEntity other = new SimpleEntity( SimpleEntity other = new SimpleEntity(
@ -57,54 +55,197 @@ public class EntityWithOneToOneJoinTableTest extends SessionFactoryBasedFunction
entity.setOther( other ); entity.setOther( other );
inTransaction( session -> {session.save( other );} ); scope.inTransaction( session -> {
inTransaction( session -> {session.save( entity );} ); session.save( other );
} );
scope.inTransaction( session -> {
session.save( entity );
} );
inTransaction( scope.inTransaction(
session -> {
EntityWithOneToOneJoinTable entity2 = new EntityWithOneToOneJoinTable(
2,
"second",
Integer.MAX_VALUE
);
SimpleEntity other2 = new SimpleEntity(
3,
Calendar.getInstance().getTime(),
null,
1,
Long.MAX_VALUE,
null
);
entity2.setOther( other2 );
session.save( other2 );
session.save( entity2 );
}
);
}
@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete from EntityWithOneToOneJoinTable" ).executeUpdate();
session.createQuery( "delete from SimpleEntity" ).executeUpdate();
}
);
}
@Test
public void testGet(SessionFactoryScope scope) {
StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> { session -> {
final EntityWithOneToOneJoinTable loaded = session.get( EntityWithOneToOneJoinTable.class, 1 ); final EntityWithOneToOneJoinTable loaded = session.get( EntityWithOneToOneJoinTable.class, 1 );
assert loaded != null; assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
assertThat( loaded, notNullValue() );
assertThat( loaded.getName(), equalTo( "first" ) ); assertThat( loaded.getName(), equalTo( "first" ) );
assert loaded.getOther() != null;
assertThat( loaded.getOther().getId(), equalTo( 2 ) ); SimpleEntity other = loaded.getOther();
assertTrue( Hibernate.isInitialized( other ) );
assertThat( other, notNullValue() );
assertThat( other.getId(), equalTo( 2 ) );
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
} }
); );
inTransaction( scope.inTransaction(
session -> { session -> {
final SimpleEntity loaded = session.get( SimpleEntity.class, 2 ); final SimpleEntity loaded = session.get( SimpleEntity.class, 2 );
assert loaded != null; assert loaded != null;
assertThat( loaded.getSomeInteger(), equalTo( Integer.MAX_VALUE ) ); assertThat( loaded.getSomeInteger(), equalTo( Integer.MAX_VALUE ) );
} }
); );
}
inTransaction( @Test
public void testHqlSelectParentAttribute(SessionFactoryScope scope) {
StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> { session -> {
final String value = session.createQuery( final String value = session.createQuery(
"select e.name from EntityWithOneToOneJoinTable e where e.other.id = 2", "select e.name from EntityWithOneToOneJoinTable e where e.other.id = 2",
String.class String.class
).uniqueResult(); ).uniqueResult();
assertThat( value, equalTo( "first" ) ); assertThat( value, equalTo( "first" ) );
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
} }
); );
} }
@Test @Test
public void testUpdate() { public void testHqlSelectParentWithImplicitJoin(SessionFactoryScope scope) {
EntityWithOneToOneJoinTable entity = new EntityWithOneToOneJoinTable( 1, "first", Integer.MAX_VALUE ); StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> {
final EntityWithOneToOneJoinTable loaded = session.createQuery(
"select e from EntityWithOneToOneJoinTable e where e.other.id = 2",
EntityWithOneToOneJoinTable.class
).uniqueResult();
assertThat( loaded.getName(), equalTo( "first" ) );
assertThat( statistics.getPrepareStatementCount(), is( 2L ) );
assertThat( loaded, notNullValue() );
assertThat( loaded.getName(), equalTo( "first" ) );
SimpleEntity other = loaded.getOther();
assertTrue( Hibernate.isInitialized( other ) );
assertThat( other, notNullValue() );
assertThat( other.getId(), equalTo( 2 ) );
assertThat( statistics.getPrepareStatementCount(), is( 2L ) );
}
);
}
@Test
public void testHqlSelectParentWithJoin(SessionFactoryScope scope) {
StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> {
final EntityWithOneToOneJoinTable loaded = session.createQuery(
"select e from EntityWithOneToOneJoinTable e join e.other o where e.id = 1",
EntityWithOneToOneJoinTable.class
).uniqueResult();
assertThat( loaded.getName(), equalTo( "first" ) );
assertThat( statistics.getPrepareStatementCount(), is( 2L ) );
assertThat( loaded, notNullValue() );
assertThat( loaded.getName(), equalTo( "first" ) );
SimpleEntity other = loaded.getOther();
assertTrue( Hibernate.isInitialized( other ) );
assertThat( other, notNullValue() );
assertThat( other.getId(), equalTo( 2 ) );
assertThat( statistics.getPrepareStatementCount(), is( 2L ) );
}
);
}
@Test
public void testHqlSelectParentWithJoinFetch(SessionFactoryScope scope) {
StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> {
final EntityWithOneToOneJoinTable loaded = session.createQuery(
"select e from EntityWithOneToOneJoinTable e join fetch e.other o where e.id = 1",
EntityWithOneToOneJoinTable.class
).uniqueResult();
assertThat( loaded.getName(), equalTo( "first" ) );
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
assertThat( loaded, notNullValue() );
assertThat( loaded.getName(), equalTo( "first" ) );
SimpleEntity other = loaded.getOther();
assertTrue( Hibernate.isInitialized( other ) );
assertThat( other, notNullValue() );
assertThat( other.getId(), equalTo( 2 ) );
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
}
);
}
@Test
public void testUpdate(SessionFactoryScope scope) {
EntityWithOneToOneJoinTable entity = new EntityWithOneToOneJoinTable( 3, "first", Integer.MAX_VALUE );
SimpleEntity other = new SimpleEntity( SimpleEntity other = new SimpleEntity(
2, 4,
Calendar.getInstance().getTime(), Calendar.getInstance().getTime(),
null, null,
Integer.MAX_VALUE, 5,
Long.MAX_VALUE, Long.MAX_VALUE,
null null
); );
entity.setOther( other ); entity.setOther( other );
inTransaction( scope.inTransaction(
session -> { session -> {
session.save( other ); session.save( other );
session.save( entity ); session.save( entity );
@ -112,7 +253,7 @@ public class EntityWithOneToOneJoinTableTest extends SessionFactoryBasedFunction
); );
SimpleEntity anOther = new SimpleEntity( SimpleEntity anOther = new SimpleEntity(
3, 5,
Calendar.getInstance().getTime(), Calendar.getInstance().getTime(),
null, null,
Integer.MIN_VALUE, Integer.MIN_VALUE,
@ -120,20 +261,20 @@ public class EntityWithOneToOneJoinTableTest extends SessionFactoryBasedFunction
null null
); );
inTransaction( scope.inTransaction(
session -> { session -> {
EntityWithOneToOneJoinTable loaded = session.get( EntityWithOneToOneJoinTable.class, 1 ); EntityWithOneToOneJoinTable loaded = session.get( EntityWithOneToOneJoinTable.class, 3 );
session.save( anOther ); session.save( anOther );
loaded.setOther( anOther ); loaded.setOther( anOther );
} }
); );
inTransaction( scope.inTransaction(
session -> { session -> {
EntityWithOneToOneJoinTable loaded = session.get( EntityWithOneToOneJoinTable.class, 1 ); EntityWithOneToOneJoinTable loaded = session.get( EntityWithOneToOneJoinTable.class, 3 );
SimpleEntity loadedOther = loaded.getOther(); SimpleEntity loadedOther = loaded.getOther();
assertThat( loadedOther, notNullValue() ); assertThat( loadedOther, notNullValue() );
assertThat( loadedOther.getId(), equalTo( 3 ) ); assertThat( loadedOther.getId(), equalTo( 5 ) );
} }
); );
} }

View File

@ -96,6 +96,8 @@ public class EntityWithOneToOneSharingPrimaryKeyTest {
assertTrue( Hibernate.isInitialized( other ) ); assertTrue( Hibernate.isInitialized( other ) );
assertThat( other, notNullValue() ); assertThat( other, notNullValue() );
assertThat( other.getId(), equalTo( 2 ) ); assertThat( other.getId(), equalTo( 2 ) );
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
} }
); );

View File

@ -8,9 +8,15 @@ package org.hibernate.orm.test.sql.exec.onetoone;
import java.util.Calendar; import java.util.Calendar;
import org.hibernate.testing.junit5.SessionFactoryBasedFunctionalTest; import org.hibernate.Hibernate;
import org.hibernate.stat.spi.StatisticsImplementor;
import org.hibernate.testing.orm.domain.gambit.EntityWithOneToOne; import org.hibernate.testing.orm.domain.gambit.EntityWithOneToOne;
import org.hibernate.testing.orm.domain.gambit.SimpleEntity; import org.hibernate.testing.orm.domain.gambit.SimpleEntity;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -18,23 +24,26 @@ import org.junit.jupiter.api.Test;
import org.hamcrest.CoreMatchers; import org.hamcrest.CoreMatchers;
import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
/** /**
* @author Andrea Boriero * @author Andrea Boriero
*/ */
public class EntityWithOneToOneTest extends SessionFactoryBasedFunctionalTest { @DomainModel(
annotatedClasses = {
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] {
EntityWithOneToOne.class, EntityWithOneToOne.class,
SimpleEntity.class SimpleEntity.class
}; }
} )
@ServiceRegistry
@SessionFactory(generateStatistics = true)
public class EntityWithOneToOneTest {
@BeforeEach @BeforeEach
public void setUp() { public void setUp(SessionFactoryScope scope) {
EntityWithOneToOne entity = new EntityWithOneToOne( 1, "first", Integer.MAX_VALUE ); EntityWithOneToOne entity = new EntityWithOneToOne( 1, "first", Integer.MAX_VALUE );
SimpleEntity other = new SimpleEntity( SimpleEntity other = new SimpleEntity(
@ -48,30 +57,39 @@ public class EntityWithOneToOneTest extends SessionFactoryBasedFunctionalTest {
entity.setOther( other ); entity.setOther( other );
inTransaction( session -> { scope.inTransaction( session -> {
session.save( other ); session.save( other );
session.save( entity ); session.save( entity );
} ); } );
} }
@AfterEach @AfterEach
public void tearDown() { public void tearDown(SessionFactoryScope scope) {
deleteAll(); deleteAll( scope );
} }
@Test @Test
public void testGet() { public void testGet(SessionFactoryScope scope) {
inTransaction( StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> { session -> {
final EntityWithOneToOne loaded = session.get( EntityWithOneToOne.class, 1 ); final EntityWithOneToOne loaded = session.get( EntityWithOneToOne.class, 1 );
assert loaded != null; assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
assertThat( loaded, notNullValue() );
assertThat( loaded.getName(), equalTo( "first" ) ); assertThat( loaded.getName(), equalTo( "first" ) );
assert loaded.getOther() != null;
assertThat( loaded.getOther().getId(), equalTo( 2 ) ); SimpleEntity other = loaded.getOther();
assertTrue( Hibernate.isInitialized( other ) );
assertThat( other, notNullValue() );
assertThat( other.getId(), equalTo( 2 ) );
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
} }
); );
inTransaction( scope.inTransaction(
session -> { session -> {
final SimpleEntity loaded = session.get( SimpleEntity.class, 2 ); final SimpleEntity loaded = session.get( SimpleEntity.class, 2 );
assert loaded != null; assert loaded != null;
@ -81,7 +99,7 @@ public class EntityWithOneToOneTest extends SessionFactoryBasedFunctionalTest {
} }
@Test @Test
public void testUpdate(){ public void testUpdate(SessionFactoryScope scope) {
SimpleEntity other = new SimpleEntity( SimpleEntity other = new SimpleEntity(
3, 3,
Calendar.getInstance().getTime(), Calendar.getInstance().getTime(),
@ -91,7 +109,7 @@ public class EntityWithOneToOneTest extends SessionFactoryBasedFunctionalTest {
null null
); );
inTransaction( scope.inTransaction(
session -> { session -> {
final EntityWithOneToOne loaded = session.get( EntityWithOneToOne.class, 1 ); final EntityWithOneToOne loaded = session.get( EntityWithOneToOne.class, 1 );
assert loaded != null; assert loaded != null;
@ -104,7 +122,7 @@ public class EntityWithOneToOneTest extends SessionFactoryBasedFunctionalTest {
} }
); );
inTransaction( scope.inTransaction(
session -> { session -> {
final EntityWithOneToOne loaded = session.get( EntityWithOneToOne.class, 1 ); final EntityWithOneToOne loaded = session.get( EntityWithOneToOne.class, 1 );
assert loaded != null; assert loaded != null;
@ -117,59 +135,122 @@ public class EntityWithOneToOneTest extends SessionFactoryBasedFunctionalTest {
} }
@Test @Test
public void testQueryParentAttribute2() { public void testHqlSelectParentAttribute(SessionFactoryScope scope) {
inTransaction( StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> { session -> {
final String value = session.createQuery( final String value = session.createQuery(
"select e.name from EntityWithOneToOne e where e.id = 1", "select e.name from EntityWithOneToOne e where e.id = 1",
String.class String.class
).uniqueResult(); ).uniqueResult();
assertThat( value, equalTo( "first" ) ); assertThat( value, equalTo( "first" ) );
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
} }
); );
} }
@Test @Test
public void testQueryParentAttribute3() { public void testHqlSelect(SessionFactoryScope scope) {
inTransaction( StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> { session -> {
final EntityWithOneToOne value = session.createQuery( final EntityWithOneToOne value = session.createQuery(
"select e from EntityWithOneToOne e where e.id = 1", "select e from EntityWithOneToOne e where e.id = 1",
EntityWithOneToOne.class EntityWithOneToOne.class
).uniqueResult(); ).uniqueResult();
assertThat( value.getName(), equalTo( "first" ) ); assertThat( value.getName(), equalTo( "first" ) );
SimpleEntity other = value.getOther();
assertTrue( Hibernate.isInitialized( other ) );
assertThat( statistics.getPrepareStatementCount(), is( 2L ) );
} }
); );
} }
@Test @Test
public void testQueryParentAttribute() { public void testHqlSelectParentParentAttributeWithImplicitJoin(SessionFactoryScope scope) {
inTransaction( StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> { session -> {
final String value = session.createQuery( final String value = session.createQuery(
"select e.name from EntityWithOneToOne e where e.other.id = 2", "select e.name from EntityWithOneToOne e where e.other.id = 2",
String.class String.class
).uniqueResult(); ).uniqueResult();
assertThat( value, equalTo( "first" ) ); assertThat( value, equalTo( "first" ) );
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
} }
); );
} }
@Test @Test
public void testQueryParent() { public void testHqlSelectWithImplicitJoin(SessionFactoryScope scope) {
inTransaction( StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> { session -> {
final EntityWithOneToOne value = session.createQuery( final EntityWithOneToOne value = session.createQuery(
"select e from EntityWithOneToOne e where e.other.id = 2", "select e from EntityWithOneToOne e where e.other.id = 2",
EntityWithOneToOne.class EntityWithOneToOne.class
).uniqueResult(); ).uniqueResult();
assertThat( value.getName(), equalTo( "first" ) ); assertThat( value.getName(), equalTo( "first" ) );
SimpleEntity other = value.getOther();
assertTrue( Hibernate.isInitialized( other ) );
assertThat( statistics.getPrepareStatementCount(), is( 2L ) );
} }
); );
} }
private void deleteAll() { @Test
inTransaction( public void testHqlSelectWithJoin(SessionFactoryScope scope) {
StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> {
final EntityWithOneToOne value = session.createQuery(
"select e from EntityWithOneToOne e join e.other k where k.id = 2",
EntityWithOneToOne.class
).uniqueResult();
assertThat( value.getName(), equalTo( "first" ) );
SimpleEntity other = value.getOther();
assertTrue( Hibernate.isInitialized( other ) );
assertThat( statistics.getPrepareStatementCount(), is( 2L ) );
}
);
}
@Test
public void testHqlSelectWithJoinFetch(SessionFactoryScope scope) {
StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> {
final EntityWithOneToOne value = session.createQuery(
"select e from EntityWithOneToOne e join fetch e.other k where k.id = 2",
EntityWithOneToOne.class
).uniqueResult();
assertThat( value.getName(), equalTo( "first" ) );
SimpleEntity other = value.getOther();
assertTrue( Hibernate.isInitialized( other ) );
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
}
);
}
private void deleteAll(SessionFactoryScope scope) {
scope.inTransaction(
session -> { session -> {
final EntityWithOneToOne loaded = session.get( EntityWithOneToOne.class, 1 ); final EntityWithOneToOne loaded = session.get( EntityWithOneToOne.class, 1 );
assert loaded != null; assert loaded != null;
@ -179,7 +260,7 @@ public class EntityWithOneToOneTest extends SessionFactoryBasedFunctionalTest {
} }
); );
inTransaction( scope.inTransaction(
session -> { session -> {
final EntityWithOneToOne notfound = session.find( EntityWithOneToOne.class, 1 ); final EntityWithOneToOne notfound = session.find( EntityWithOneToOne.class, 1 );
assertThat( notfound, CoreMatchers.nullValue() ); assertThat( notfound, CoreMatchers.nullValue() );