Search in sources :

Example 1 with SQLStatementInspector

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

the class NotOptionalManyToOneTest method testInnerJoinIsUsed.

@Test
public void testInnerJoinIsUsed(SessionFactoryScope scope) {
    SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector();
    statementInspector.clear();
    scope.inTransaction(session -> {
        session.get(Parent.class, 2);
        statementInspector.assertNumberOfJoins(0, SqlAstJoinType.INNER, 1);
    });
}
Also used : SQLStatementInspector(org.hibernate.testing.jdbc.SQLStatementInspector) Test(org.junit.jupiter.api.Test)

Example 2 with SQLStatementInspector

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

the class EntityGraphWithFetchAnnotationTest method testWithEntityGraph.

@Test
@TestForIssue(jiraKey = "HHH-10485")
void testWithEntityGraph(SessionFactoryScope scope) {
    SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector();
    statementInspector.clear();
    scope.inTransaction(session -> {
        EntityManager entityManager = session.unwrap(EntityManager.class);
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class);
        criteriaQuery.from(Order.class);
        EntityGraph<Order> entityGraph = entityManager.createEntityGraph(Order.class);
        entityGraph.addAttributeNodes("products");
        entityManager.createQuery(criteriaQuery).setFirstResult(10).setMaxResults(20).setHint(GraphSemantic.FETCH.getJpaHintName(), entityGraph).getResultList();
        statementInspector.assertExecutedCount(1);
        String sql = statementInspector.getSqlQueries().get(0);
        assertThat(sql, containsString("left join"));
    });
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) SQLStatementInspector(org.hibernate.testing.jdbc.SQLStatementInspector) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.jupiter.api.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 3 with SQLStatementInspector

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

the class LoadAndFetchGraphTest method testQueryByIdWithLoadGraph.

@Test
void testQueryByIdWithLoadGraph(SessionFactoryScope scope) {
    SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector();
    statementInspector.clear();
    scope.inTransaction(session -> {
        EntityManager entityManager = session.unwrap(EntityManager.class);
        EntityGraph<CEntity> entityGraph = entityManager.createEntityGraph(CEntity.class);
        entityGraph.addAttributeNodes("a", "b");
        entityGraph.addSubgraph("dList").addAttributeNodes("e");
        TypedQuery<CEntity> query = entityManager.createQuery("select c from CEntity as c where c.id = :cid ", CEntity.class);
        query.setHint(GraphSemantic.LOAD.getJpaHintName(), entityGraph);
        query.setParameter("cid", 1);
        CEntity cEntity = query.getSingleResult();
        assertThat(cEntity.getA(), isInitialized());
        assertThat(cEntity.getB(), isInitialized());
        assertThat(cEntity.getC(), isNotInitialized());
        assertThat(cEntity.getDList(), isInitialized());
        cEntity.getDList().forEach(dEntity -> assertThat(dEntity.getE(), isInitialized()));
        assertThat(statementInspector.getSqlQueries(), hasSize(1));
    });
}
Also used : EntityManager(jakarta.persistence.EntityManager) SQLStatementInspector(org.hibernate.testing.jdbc.SQLStatementInspector) Test(org.junit.jupiter.api.Test)

Example 4 with SQLStatementInspector

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

the class LoadAndFetchGraphTest method testQueryById.

@Test
void testQueryById(SessionFactoryScope scope) {
    SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector();
    statementInspector.clear();
    scope.inTransaction(session -> {
        EntityManager entityManager = session.unwrap(EntityManager.class);
        TypedQuery<CEntity> query = entityManager.createQuery("select c from CEntity as c where c.id = :cid ", CEntity.class);
        query.setParameter("cid", 1);
        CEntity cEntity = query.getSingleResult();
        assertThat(cEntity.getA(), isNotInitialized());
        assertThat(cEntity.getB(), isNotInitialized());
        assertThat(cEntity.getC(), isNotInitialized());
        assertThat(cEntity.getDList(), isNotInitialized());
        assertThat(statementInspector.getSqlQueries(), hasSize(1));
    });
}
Also used : EntityManager(jakarta.persistence.EntityManager) SQLStatementInspector(org.hibernate.testing.jdbc.SQLStatementInspector) Test(org.junit.jupiter.api.Test)

Example 5 with SQLStatementInspector

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

the class LoadAndFetchGraphTest method testQueryByIdWithFetchGraph.

@Test
void testQueryByIdWithFetchGraph(SessionFactoryScope scope) {
    SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector();
    statementInspector.clear();
    scope.inTransaction(session -> {
        EntityManager entityManager = session.unwrap(EntityManager.class);
        EntityGraph<CEntity> entityGraph = entityManager.createEntityGraph(CEntity.class);
        entityGraph.addAttributeNodes("a", "b");
        entityGraph.addSubgraph("dList").addAttributeNodes("e");
        TypedQuery<CEntity> query = entityManager.createQuery("select c from CEntity as c where c.id = :cid ", CEntity.class);
        query.setHint(GraphSemantic.FETCH.getJpaHintName(), entityGraph);
        query.setParameter("cid", 1);
        CEntity cEntity = query.getSingleResult();
        assertThat(cEntity.getA(), isInitialized());
        assertThat(cEntity.getB(), isInitialized());
        assertThat(cEntity.getC(), isNotInitialized());
        assertThat(cEntity.getDList(), isInitialized());
        cEntity.getDList().forEach(dEntity -> assertThat(dEntity.getE(), isInitialized()));
        assertThat(statementInspector.getSqlQueries(), hasSize(1));
    });
}
Also used : EntityManager(jakarta.persistence.EntityManager) SQLStatementInspector(org.hibernate.testing.jdbc.SQLStatementInspector) Test(org.junit.jupiter.api.Test)

Aggregations

SQLStatementInspector (org.hibernate.testing.jdbc.SQLStatementInspector)187 Test (org.junit.jupiter.api.Test)182 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)32 TestForIssue (org.hibernate.testing.TestForIssue)20 Query (org.hibernate.query.Query)16 List (java.util.List)13 JiraKey (org.hibernate.testing.orm.junit.JiraKey)10 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 FailureExpected (org.hibernate.testing.orm.junit.FailureExpected)7 EntityManager (jakarta.persistence.EntityManager)6 Male (org.hibernate.orm.test.sql.exec.onetoone.bidirectional.EntityWithBidirectionalAssociationsOneOfWhichIsAJoinTableTest.Male)4 Tuple (jakarta.persistence.Tuple)3 CriteriaQuery (jakarta.persistence.criteria.CriteriaQuery)3 HashSet (java.util.HashSet)3 Parent (org.hibernate.orm.test.sql.exec.onetoone.bidirectional.EntityWithBidirectionalAssociationsOneOfWhichIsAJoinTableTest.Parent)3 DomainModel (org.hibernate.testing.orm.junit.DomainModel)3