Search in sources :

Example 96 with SQLStatementInspector

use of org.hibernate.testing.jdbc.SQLStatementInspector in project hibernate-orm by hibernate.

the class LazyManyToOneEmbeddedIdWithToOneFKTest method testHqlJoin.

@Test
public void testHqlJoin(SessionFactoryScope scope) {
    SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector();
    statementInspector.clear();
    scope.inTransaction(session -> {
        System system = session.createQuery("from System e join e.user where e.id = :id", System.class).setParameter("id", 1).uniqueResult();
        statementInspector.assertExecutedCount(2);
        statementInspector.assertNumberOfOccurrenceInQuery(0, "join", 1);
        statementInspector.assertNumberOfOccurrenceInQuery(1, "join", 0);
        assertFalse(Hibernate.isInitialized(system.getUser()));
        assertThat(system, is(notNullValue()));
        SystemUser user = system.getUser();
        assertThat(user, is(notNullValue()));
    });
}
Also used : SQLStatementInspector(org.hibernate.testing.jdbc.SQLStatementInspector) Test(org.junit.jupiter.api.Test)

Example 97 with SQLStatementInspector

use of org.hibernate.testing.jdbc.SQLStatementInspector in project hibernate-orm by hibernate.

the class ManyToOneEmbeddedIdWithLazyToOneFKTest method testHqlJoin.

@Test
public void testHqlJoin(SessionFactoryScope scope) {
    SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector();
    statementInspector.clear();
    scope.inTransaction(session -> {
        System system = session.createQuery("from System e join e.user where e.id = :id", System.class).setParameter("id", 1).uniqueResult();
        statementInspector.assertExecutedCount(2);
        statementInspector.assertNumberOfOccurrenceInQuery(0, "join", 1);
        statementInspector.assertNumberOfOccurrenceInQuery(1, "join", 0);
        assertThat(system, is(notNullValue()));
        SystemUser user = system.getUser();
        assertThat(user, is(notNullValue()));
        assertTrue(Hibernate.isInitialized(system.getUser()));
        assertFalse(Hibernate.isInitialized(system.getUser().getPk().subsystem));
        statementInspector.clear();
        PK pk = system.getUser().getPk();
        assertThat(pk.username, is("Fab"));
        Subsystem subsystem = pk.getSubsystem();
        assertThat(subsystem.getId(), is(2));
        assertThat(subsystem.getDescription(), is("sub1"));
        statementInspector.assertExecutedCount(1);
        statementInspector.assertNumberOfOccurrenceInQuery(0, "join", 0);
    });
}
Also used : SQLStatementInspector(org.hibernate.testing.jdbc.SQLStatementInspector) Test(org.junit.jupiter.api.Test)

Example 98 with SQLStatementInspector

use of org.hibernate.testing.jdbc.SQLStatementInspector in project hibernate-orm by hibernate.

the class ManyToOneEmbeddedIdWithLazyToOneFKTest method testGet.

@Test
public void testGet(SessionFactoryScope scope) {
    SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector();
    statementInspector.clear();
    scope.inTransaction(session -> {
        System system = session.get(System.class, 1);
        assertThat(system, is(notNullValue()));
        assertThat(system.getId(), is(1));
        assertTrue(Hibernate.isInitialized(system.getUser()));
        PK pk = system.getUser().getPk();
        assertFalse(Hibernate.isInitialized(pk.subsystem));
        SystemUser user = system.getUser();
        assertThat(user, is(notNullValue()));
        statementInspector.assertExecutedCount(1);
        statementInspector.assertNumberOfOccurrenceInQuery(0, "join", 1);
        statementInspector.clear();
        assertThat(pk.username, is("Fab"));
        Subsystem subsystem = pk.getSubsystem();
        assertThat(subsystem.getId(), is(2));
        assertThat(subsystem.getDescription(), is("sub1"));
        statementInspector.assertExecutedCount(1);
        statementInspector.assertNumberOfOccurrenceInQuery(0, "join", 0);
    });
}
Also used : SQLStatementInspector(org.hibernate.testing.jdbc.SQLStatementInspector) Test(org.junit.jupiter.api.Test)

Example 99 with SQLStatementInspector

use of org.hibernate.testing.jdbc.SQLStatementInspector in project hibernate-orm by hibernate.

the class IdClassTest method testHql.

@Test
public void testHql(SessionFactoryScope scope) {
    SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector();
    statementInspector.clear();
    scope.inTransaction(session -> {
        PK pk = new PK("Linux", "admin", 1);
        SystemUser systemUser = session.createQuery("from SystemUser s where s.id = :id", SystemUser.class).setParameter("id", pk).getSingleResult();
        assertThat(systemUser.getName(), is("Andrea"));
        assertThat(systemUser.getSubsystem(), is("Linux"));
        assertThat(systemUser.getUsername(), is("admin"));
        assertThat(systemUser.getRegistrationId(), is(1));
        statementInspector.assertExecutedCount(1);
        statementInspector.assertNumberOfOccurrenceInQuery(0, "join", 0);
    });
    statementInspector.clear();
    scope.inTransaction(session -> {
        SystemUser systemUser = session.createQuery("from SystemUser s where s.username = :username", SystemUser.class).setParameter("username", "admin").getSingleResult();
        assertThat(systemUser.getName(), is("Andrea"));
        assertThat(systemUser.getSubsystem(), is("Linux"));
        assertThat(systemUser.getUsername(), is("admin"));
        assertThat(systemUser.getRegistrationId(), is(1));
        statementInspector.assertExecutedCount(1);
        statementInspector.assertNumberOfOccurrenceInQuery(0, "join", 0);
    });
}
Also used : SQLStatementInspector(org.hibernate.testing.jdbc.SQLStatementInspector) Test(org.junit.jupiter.api.Test)

Example 100 with SQLStatementInspector

use of org.hibernate.testing.jdbc.SQLStatementInspector in project hibernate-orm by hibernate.

the class IdClassWithEagerManyToOneTest method testGet.

@Test
public void testGet(SessionFactoryScope scope) {
    SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector();
    statementInspector.clear();
    scope.inTransaction(session -> {
        PK pk = new PK(new Subsystem("1", "Linux2"), "admin");
        SystemUser systemUser = session.get(SystemUser.class, pk);
        assertThat(systemUser.getName(), is("Andrea"));
        Subsystem subsystem = systemUser.getSubsystem();
        statementInspector.assertExecutedCount(1);
        statementInspector.assertNumberOfOccurrenceInQuery(0, "join", 1);
        statementInspector.clear();
        assertTrue(Hibernate.isInitialized(subsystem));
        assertThat(subsystem.getId(), is("1"));
        assertThat(subsystem.getDescription(), is("Linux"));
        assertThat(systemUser.getUsername(), is("admin"));
        statementInspector.assertExecutedCount(0);
    });
}
Also used : SQLStatementInspector(org.hibernate.testing.jdbc.SQLStatementInspector) Test(org.junit.jupiter.api.Test)

Aggregations

SQLStatementInspector (org.hibernate.testing.jdbc.SQLStatementInspector)212 Test (org.junit.jupiter.api.Test)207 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)32 JiraKey (org.hibernate.testing.orm.junit.JiraKey)32 TestForIssue (org.hibernate.testing.TestForIssue)21 Query (org.hibernate.query.Query)16 List (java.util.List)13 AdoptedChild (org.hibernate.orm.test.sql.exec.onetoone.bidirectional.EntityWithBidirectionalOneToOneTest.AdoptedChild)9 Child (org.hibernate.orm.test.sql.exec.onetoone.bidirectional.EntityWithBidirectionalOneToOneTest.Child)9 Mother (org.hibernate.orm.test.sql.exec.onetoone.bidirectional.EntityWithBidirectionalOneToOneTest.Mother)9 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)7 Statistics (org.hibernate.stat.Statistics)7 EntityManager (jakarta.persistence.EntityManager)6 Male (org.hibernate.orm.test.sql.exec.onetoone.bidirectional.EntityWithBidirectionalAssociationsOneOfWhichIsAJoinTableTest.Male)4 DomainModel (org.hibernate.testing.orm.junit.DomainModel)4 SessionFactory (org.hibernate.testing.orm.junit.SessionFactory)4 SessionFactoryScope (org.hibernate.testing.orm.junit.SessionFactoryScope)4 Entity (jakarta.persistence.Entity)3 Id (jakarta.persistence.Id)3 ManyToOne (jakarta.persistence.ManyToOne)3