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 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.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.BeforeEach;
import org.junit.jupiter.api.Test;
@ -20,6 +24,7 @@ import org.junit.jupiter.api.Test;
import org.hamcrest.CoreMatchers;
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.junit.jupiter.api.Assertions.assertFalse;
@ -29,18 +34,20 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author Andrea Boriero
*/
public class EntityWithLazyOneToOneTest extends SessionFactoryBasedFunctionalTest {
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] {
@DomainModel(
annotatedClasses = {
EntityWithLazyOneToOne.class,
SimpleEntity.class
};
}
)
@ServiceRegistry
@SessionFactory(generateStatistics = true)
public class EntityWithLazyOneToOneTest {
@BeforeEach
public void setUp() {
public void setUp(SessionFactoryScope scope) {
scope.inTransaction( session -> {
EntityWithLazyOneToOne entity = new EntityWithLazyOneToOne( 1, "first", Integer.MAX_VALUE );
SimpleEntity other = new SimpleEntity(
@ -53,51 +60,77 @@ public class EntityWithLazyOneToOneTest extends SessionFactoryBasedFunctionalTes
);
entity.setOther( other );
session.save( other );
session.save( entity );
} );
inTransaction( session -> {
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( entity );
} );
}
@AfterEach
public void tearDown() {
inTransaction( session -> {
deleteAll();
} );
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete from EntityWithLazyOneToOne" ).executeUpdate();
session.createQuery( "delete from SimpleEntity" ).executeUpdate();
}
);
}
@Test
public void testGet() {
inTransaction(
public void testGet(SessionFactoryScope scope) {
StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> {
final EntityWithLazyOneToOne loaded = session.get( EntityWithLazyOneToOne.class, 1 );
assert loaded != null;
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
assertThat( loaded, notNullValue() );
assertThat( loaded.getName(), equalTo( "first" ) );
SimpleEntity other = loaded.getOther();
assertFalse(
Hibernate.isInitialized( loaded.getOther() ),
Hibernate.isInitialized( other ),
"The lazy association should not be initialized"
);
SimpleEntity loadedOther = loaded.getOther();
assert loadedOther != null;
assertThat( loaded.getOther().getId(), equalTo( 2 ) );
assertThat( other, notNullValue() );
assertThat( other.getId(), equalTo( 2 ) );
assertFalse(
Hibernate.isInitialized( loaded.getOther() ),
Hibernate.isInitialized( other ),
"getId() should not trigger the lazy association initialization"
);
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
loadedOther.getSomeDate();
other.getSomeDate();
assertTrue(
Hibernate.isInitialized( loaded.getOther() ),
Hibernate.isInitialized( other ),
"The lazy association should be initialized"
);
assertThat( statistics.getPrepareStatementCount(), is( 2L ) );
}
);
inTransaction(
scope.inTransaction(
session -> {
final SimpleEntity loaded = session.get( SimpleEntity.class, 2 );
assert loaded != null;
@ -107,22 +140,95 @@ public class EntityWithLazyOneToOneTest extends SessionFactoryBasedFunctionalTes
}
@Test
public void testHqlSelect() {
inTransaction(
public void testHqlSelectParentAttribute(SessionFactoryScope scope) {
StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> {
final String value = session.createQuery(
"select e.name from EntityWithLazyOneToOne e where e.other.id = 2",
String.class
).uniqueResult();
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 -> {
final EntityWithLazyOneToOne loaded = session.get( EntityWithLazyOneToOne.class, 1 );
assert loaded != null;
@ -131,20 +237,13 @@ public class EntityWithLazyOneToOneTest extends SessionFactoryBasedFunctionalTes
}
);
inTransaction(
scope.inTransaction(
session -> {
final EntityWithLazyOneToOne notfound = session.find( EntityWithLazyOneToOne.class, 1 );
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 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.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.BeforeEach;
import org.junit.jupiter.api.Test;
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.junit.Assert.assertTrue;
/**
* @author Andrea Boriero
*/
public class EntityWithOneToOneJoinTableTest extends SessionFactoryBasedFunctionalTest {
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] {
@DomainModel(
annotatedClasses = {
EntityWithOneToOneJoinTable.class,
SimpleEntity.class
};
}
)
@ServiceRegistry
@SessionFactory(generateStatistics = true)
public class EntityWithOneToOneJoinTableTest {
@AfterEach
public void tearDown() {
inTransaction(
session -> {
final EntityWithOneToOneJoinTable loaded = session.get( EntityWithOneToOneJoinTable.class, 1 );
session.delete( loaded );
session.delete( loaded.getOther() );
}
);
}
@Test
public void testOperations() {
@BeforeEach
public void setUp(SessionFactoryScope scope) {
EntityWithOneToOneJoinTable entity = new EntityWithOneToOneJoinTable( 1, "first", Integer.MAX_VALUE );
SimpleEntity other = new SimpleEntity(
@ -57,54 +55,197 @@ public class EntityWithOneToOneJoinTableTest extends SessionFactoryBasedFunction
entity.setOther( other );
inTransaction( session -> {session.save( other );} );
inTransaction( session -> {session.save( entity );} );
scope.inTransaction( session -> {
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 -> {
final EntityWithOneToOneJoinTable loaded = session.get( EntityWithOneToOneJoinTable.class, 1 );
assert loaded != null;
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
assertThat( loaded, notNullValue() );
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 -> {
final SimpleEntity loaded = session.get( SimpleEntity.class, 2 );
assert loaded != null;
assertThat( loaded.getSomeInteger(), equalTo( Integer.MAX_VALUE ) );
}
);
}
inTransaction(
@Test
public void testHqlSelectParentAttribute(SessionFactoryScope scope) {
StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> {
final String value = session.createQuery(
"select e.name from EntityWithOneToOneJoinTable e where e.other.id = 2",
String.class
).uniqueResult();
assertThat( value, equalTo( "first" ) );
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
}
);
}
@Test
public void testUpdate() {
EntityWithOneToOneJoinTable entity = new EntityWithOneToOneJoinTable( 1, "first", Integer.MAX_VALUE );
public void testHqlSelectParentWithImplicitJoin(SessionFactoryScope scope) {
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(
2,
4,
Calendar.getInstance().getTime(),
null,
Integer.MAX_VALUE,
5,
Long.MAX_VALUE,
null
);
entity.setOther( other );
inTransaction(
scope.inTransaction(
session -> {
session.save( other );
session.save( entity );
@ -112,7 +253,7 @@ public class EntityWithOneToOneJoinTableTest extends SessionFactoryBasedFunction
);
SimpleEntity anOther = new SimpleEntity(
3,
5,
Calendar.getInstance().getTime(),
null,
Integer.MIN_VALUE,
@ -120,20 +261,20 @@ public class EntityWithOneToOneJoinTableTest extends SessionFactoryBasedFunction
null
);
inTransaction(
scope.inTransaction(
session -> {
EntityWithOneToOneJoinTable loaded = session.get( EntityWithOneToOneJoinTable.class, 1 );
EntityWithOneToOneJoinTable loaded = session.get( EntityWithOneToOneJoinTable.class, 3 );
session.save( anOther );
loaded.setOther( anOther );
}
);
inTransaction(
scope.inTransaction(
session -> {
EntityWithOneToOneJoinTable loaded = session.get( EntityWithOneToOneJoinTable.class, 1 );
EntityWithOneToOneJoinTable loaded = session.get( EntityWithOneToOneJoinTable.class, 3 );
SimpleEntity loadedOther = loaded.getOther();
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 ) );
assertThat( other, notNullValue() );
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 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.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.BeforeEach;
import org.junit.jupiter.api.Test;
@ -18,23 +24,26 @@ import org.junit.jupiter.api.Test;
import org.hamcrest.CoreMatchers;
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.junit.Assert.assertTrue;
/**
* @author Andrea Boriero
*/
public class EntityWithOneToOneTest extends SessionFactoryBasedFunctionalTest {
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] {
@DomainModel(
annotatedClasses = {
EntityWithOneToOne.class,
SimpleEntity.class
};
}
)
@ServiceRegistry
@SessionFactory(generateStatistics = true)
public class EntityWithOneToOneTest {
@BeforeEach
public void setUp() {
public void setUp(SessionFactoryScope scope) {
EntityWithOneToOne entity = new EntityWithOneToOne( 1, "first", Integer.MAX_VALUE );
SimpleEntity other = new SimpleEntity(
@ -48,30 +57,39 @@ public class EntityWithOneToOneTest extends SessionFactoryBasedFunctionalTest {
entity.setOther( other );
inTransaction( session -> {
scope.inTransaction( session -> {
session.save( other );
session.save( entity );
} );
}
@AfterEach
public void tearDown() {
deleteAll();
public void tearDown(SessionFactoryScope scope) {
deleteAll( scope );
}
@Test
public void testGet() {
inTransaction(
public void testGet(SessionFactoryScope scope) {
StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> {
final EntityWithOneToOne loaded = session.get( EntityWithOneToOne.class, 1 );
assert loaded != null;
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
assertThat( loaded, notNullValue() );
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 -> {
final SimpleEntity loaded = session.get( SimpleEntity.class, 2 );
assert loaded != null;
@ -81,7 +99,7 @@ public class EntityWithOneToOneTest extends SessionFactoryBasedFunctionalTest {
}
@Test
public void testUpdate(){
public void testUpdate(SessionFactoryScope scope) {
SimpleEntity other = new SimpleEntity(
3,
Calendar.getInstance().getTime(),
@ -91,7 +109,7 @@ public class EntityWithOneToOneTest extends SessionFactoryBasedFunctionalTest {
null
);
inTransaction(
scope.inTransaction(
session -> {
final EntityWithOneToOne loaded = session.get( EntityWithOneToOne.class, 1 );
assert loaded != null;
@ -104,7 +122,7 @@ public class EntityWithOneToOneTest extends SessionFactoryBasedFunctionalTest {
}
);
inTransaction(
scope.inTransaction(
session -> {
final EntityWithOneToOne loaded = session.get( EntityWithOneToOne.class, 1 );
assert loaded != null;
@ -117,59 +135,122 @@ public class EntityWithOneToOneTest extends SessionFactoryBasedFunctionalTest {
}
@Test
public void testQueryParentAttribute2() {
inTransaction(
public void testHqlSelectParentAttribute(SessionFactoryScope scope) {
StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> {
final String value = session.createQuery(
"select e.name from EntityWithOneToOne e where e.id = 1",
String.class
).uniqueResult();
assertThat( value, equalTo( "first" ) );
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
}
);
}
@Test
public void testQueryParentAttribute3() {
inTransaction(
public void testHqlSelect(SessionFactoryScope scope) {
StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> {
final EntityWithOneToOne value = session.createQuery(
"select e from EntityWithOneToOne e where e.id = 1",
EntityWithOneToOne.class
).uniqueResult();
assertThat( value.getName(), equalTo( "first" ) );
SimpleEntity other = value.getOther();
assertTrue( Hibernate.isInitialized( other ) );
assertThat( statistics.getPrepareStatementCount(), is( 2L ) );
}
);
}
@Test
public void testQueryParentAttribute() {
inTransaction(
public void testHqlSelectParentParentAttributeWithImplicitJoin(SessionFactoryScope scope) {
StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> {
final String value = session.createQuery(
"select e.name from EntityWithOneToOne e where e.other.id = 2",
String.class
).uniqueResult();
assertThat( value, equalTo( "first" ) );
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
}
);
}
@Test
public void testQueryParent() {
inTransaction(
public void testHqlSelectWithImplicitJoin(SessionFactoryScope scope) {
StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> {
final EntityWithOneToOne value = session.createQuery(
"select e from EntityWithOneToOne e where e.other.id = 2",
EntityWithOneToOne.class
).uniqueResult();
assertThat( value.getName(), equalTo( "first" ) );
SimpleEntity other = value.getOther();
assertTrue( Hibernate.isInitialized( other ) );
assertThat( statistics.getPrepareStatementCount(), is( 2L ) );
}
);
}
private void deleteAll() {
inTransaction(
@Test
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 -> {
final EntityWithOneToOne loaded = session.get( EntityWithOneToOne.class, 1 );
assert loaded != null;
@ -179,7 +260,7 @@ public class EntityWithOneToOneTest extends SessionFactoryBasedFunctionalTest {
}
);
inTransaction(
scope.inTransaction(
session -> {
final EntityWithOneToOne notfound = session.find( EntityWithOneToOne.class, 1 );
assertThat( notfound, CoreMatchers.nullValue() );