use of org.hibernate.orm.test.sql.exec.onetoone.bidirectional.EntityWithBidirectionalOneToOneTest.Child in project hibernate-orm by hibernate.
the class EntityWithBidirectionalOneToOneTest method testGetMother3.
@Test
public void testGetMother3(SessionFactoryScope scope) {
scope.inTransaction(session -> {
Mother mother = new Mother(4, "Catia");
Child child = new Child(5, "Stefano", mother);
AdoptedChild adoptedChild = new AdoptedChild(7, "Luisa", mother);
Mother biologicalMother = new Mother(6, "Rebecca");
adoptedChild.setBiologicalMother(biologicalMother);
Child anotherChild = new Child(8, "Igor", biologicalMother);
session.save(mother);
session.save(biologicalMother);
session.save(child);
session.save(adoptedChild);
session.save(anotherChild);
});
SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector();
statementInspector.clear();
scope.inTransaction(session -> {
final Mother mother = session.get(Mother.class, 4);
assertThat(mother.getName(), equalTo("Catia"));
Child procreatedChild = mother.getBiologicalChild();
assertThat(procreatedChild, notNullValue());
assertTrue(Hibernate.isInitialized(procreatedChild), "The procreatedChild eager OneToOne association is not initialized");
assertThat(procreatedChild.getName(), equalTo("Stefano"));
assertSame(procreatedChild.getMother(), mother);
AdoptedChild adoptedChild = mother.getAdopted();
assertThat(adoptedChild, notNullValue());
assertTrue(Hibernate.isInitialized(adoptedChild), "The adoptedChild eager OneToOne association is not initialized");
assertThat(adoptedChild.getName(), equalTo("Luisa"));
assertSame(adoptedChild.getStepMother(), mother);
Mother biologicalMother = adoptedChild.getBiologicalMother();
assertThat(biologicalMother.getId(), equalTo(6));
assertThat(biologicalMother.getAdopted(), nullValue());
Child anotherChild = biologicalMother.getBiologicalChild();
assertThat(anotherChild.getId(), equalTo(8));
assertThat(anotherChild.getName(), equalTo("Igor"));
assertSame(biologicalMother, anotherChild.getMother());
statementInspector.assertExecutedCount(2);
statementInspector.assertNumberOfOccurrenceInQuery(0, "join", 4);
statementInspector.assertNumberOfOccurrenceInQuery(1, "join", 5);
});
}
use of org.hibernate.orm.test.sql.exec.onetoone.bidirectional.EntityWithBidirectionalOneToOneTest.Child in project hibernate-orm by hibernate.
the class EntityWithBidirectionalOneToOneTest method testGetChild3.
@Test
public void testGetChild3(SessionFactoryScope scope) {
scope.inTransaction(session -> {
Mother mother = new Mother(10, "Strange mom");
session.save(mother);
session.get(AdoptedChild.class, 3).setBiologicalMother(mother);
});
SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector();
statementInspector.clear();
scope.inTransaction(session -> {
final Child child = session.get(Child.class, 2);
Mother mother = child.getMother();
assertTrue(Hibernate.isInitialized(mother), "The mother eager OneToOne association is not initialized");
assertThat(mother, notNullValue());
assertThat(mother.getName(), is("Giulia"));
Child biologicalChild = mother.getBiologicalChild();
assertSame(biologicalChild, child);
assertTrue(Hibernate.isInitialized(biologicalChild), "The child eager OneToOne association is not initialized");
AdoptedChild adoptedChild = mother.getAdopted();
assertThat(adoptedChild, notNullValue());
assertTrue(Hibernate.isInitialized(adoptedChild), "The adoptedChild eager OneToOne association is not initialized");
assertSame(adoptedChild.getStepMother(), mother);
assertThat(adoptedChild.getBiologicalMother(), notNullValue());
assertTrue(Hibernate.isInitialized(adoptedChild.getBiologicalMother()), "The biologicalMother eager OneToOne association is not initialized");
assertThat(adoptedChild.getBiologicalMother().getName(), is("Strange mom"));
statementInspector.assertExecutedCount(2);
statementInspector.assertNumberOfOccurrenceInQuery(0, "join", 3);
statementInspector.assertNumberOfOccurrenceInQuery(1, "join", 5);
});
}
use of org.hibernate.orm.test.sql.exec.onetoone.bidirectional.EntityWithBidirectionalOneToOneTest.Child in project hibernate-orm by hibernate.
the class EntityWithBidirectionalOneToOneTest method testHqlSelectMother.
@Test
public void testHqlSelectMother(SessionFactoryScope scope) {
SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector();
statementInspector.clear();
scope.inTransaction(session -> {
final Mother mother = session.createQuery("SELECT m FROM Mother m JOIN m.biologicalChild WHERE m.id = :id", Mother.class).setParameter("id", 1).getSingleResult();
Child child = mother.getBiologicalChild();
assertThat(child, notNullValue());
assertThat(child.getName(), is("Luis"));
statementInspector.assertExecutedCount(2);
statementInspector.assertNumberOfOccurrenceInQuery(0, "join", 1);
// Mother.biologicalChild
statementInspector.assertNumberOfOccurrenceInQuery(1, "join", 5);
});
}
use of org.hibernate.orm.test.sql.exec.onetoone.bidirectional.EntityWithBidirectionalOneToOneTest.Child in project hibernate-orm by hibernate.
the class EntityWithBidirectionalOneToOneTest method setUp.
@BeforeEach
public void setUp(SessionFactoryScope scope) {
scope.inTransaction(session -> {
Mother mother = new Mother(1, "Giulia");
Child child = new Child(2, "Luis", mother);
AdoptedChild adoptedChild = new AdoptedChild(3, "Fab", mother);
session.save(mother);
session.save(child);
session.save(adoptedChild);
});
}
use of org.hibernate.orm.test.sql.exec.onetoone.bidirectional.EntityWithBidirectionalOneToOneTest.Child in project hibernate-orm by hibernate.
the class EntityWithBidirectionalOneToOneTest method testHqlSelectChild.
@Test
public void testHqlSelectChild(SessionFactoryScope scope) {
SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector();
statementInspector.clear();
scope.inTransaction(session -> {
final String queryString = "SELECT c FROM Child c JOIN c.mother d WHERE d.id = :id";
final Child child = session.createQuery(queryString, Child.class).setParameter("id", 1).getSingleResult();
Mother mother = child.getMother();
assertThat(mother, notNullValue());
assertThat(mother.getName(), is("Giulia"));
statementInspector.assertExecutedCount(2);
statementInspector.assertNumberOfOccurrenceInQuery(0, "join", 1);
statementInspector.assertNumberOfOccurrenceInQuery(1, "join", 4);
});
}
Aggregations