Search in sources :

Example 1 with CriteriaQuery

use of jakarta.persistence.criteria.CriteriaQuery in project hibernate-orm by hibernate.

the class SingularAttributeJoinTest method testEntityModeMapJoinCriteriaQuery.

@Test
public void testEntityModeMapJoinCriteriaQuery(EntityManagerFactoryScope scope) {
    scope.inEntityManager(entityManager -> {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery criteriaQuery = criteriaBuilder.createQuery();
        jakarta.persistence.metamodel.EntityType distributionEntity = getEntityType(scope, "Distribution");
        From distributionFrom = criteriaQuery.from(distributionEntity);
        From policyJoin = distributionFrom.join("policy");
        Path policyId = policyJoin.get("policyId");
        criteriaQuery.select(policyId);
        TypedQuery typedQuery = entityManager.createQuery(criteriaQuery);
    });
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Path(jakarta.persistence.criteria.Path) TypedQuery(jakarta.persistence.TypedQuery) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) From(jakarta.persistence.criteria.From) Test(org.junit.jupiter.api.Test)

Example 2 with CriteriaQuery

use of jakarta.persistence.criteria.CriteriaQuery in project hibernate-orm by hibernate.

the class CrtiteriaGetResultTypeTest method testEntityResultType.

@Test
public void testEntityResultType(EntityManagerFactoryScope scope) {
    scope.inEntityManager(entityManager -> {
        final CriteriaQuery query = entityManager.getCriteriaBuilder().createQuery(Person.class);
        final Class resultType = query.getResultType();
        assertThat(resultType, notNullValue());
        assertEquals(Person.class.getName(), resultType.getName());
    });
}
Also used : CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) Test(org.junit.jupiter.api.Test)

Example 3 with CriteriaQuery

use of jakarta.persistence.criteria.CriteriaQuery in project hibernate-orm by hibernate.

the class CrtiteriaGetResultTypeTest method testObjectResultType.

@Test
public void testObjectResultType(EntityManagerFactoryScope scope) {
    scope.inEntityManager(entityManager -> {
        final CriteriaQuery query = entityManager.getCriteriaBuilder().createQuery();
        final Class resultType = query.getResultType();
        assertThat(resultType, notNullValue());
        assertEquals(Object.class.getName(), resultType.getName());
    });
}
Also used : CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) Test(org.junit.jupiter.api.Test)

Example 4 with CriteriaQuery

use of jakarta.persistence.criteria.CriteriaQuery in project hibernate-orm by hibernate.

the class QueryHintSQLServer2012Test method testQueryHint.

@Test
public void testQueryHint(SessionFactoryScope scope) {
    Department department = new Department();
    department.name = "Sales";
    Employee employee1 = new Employee();
    employee1.department = department;
    Employee employee2 = new Employee();
    employee2.department = department;
    List result = scope.fromSession(session -> {
        session.getTransaction().begin();
        session.persist(department);
        session.persist(employee1);
        session.persist(employee2);
        try {
            session.getTransaction().commit();
            session.clear();
            // test Query w/ a simple SQLServer2012 optimizer hint
            session.getTransaction().begin();
            Query query = session.createQuery("FROM QueryHintSQLServer2012Test$Employee e WHERE e.department.name = :departmentName").addQueryHint("MAXDOP 2").setParameter("departmentName", "Sales");
            List results = query.list();
            session.getTransaction().commit();
            session.clear();
            assertEquals(2, results.size());
            assertTrue(QueryHintTestSQLServer2012Dialect.getProcessedSql().contains("OPTION (MAXDOP 2)"));
            QueryHintTestSQLServer2012Dialect.resetProcessedSql();
            // test multiple hints
            session.getTransaction().begin();
            query = session.createQuery("FROM QueryHintSQLServer2012Test$Employee e WHERE e.department.name = :departmentName").addQueryHint("MAXDOP 2").addQueryHint("CONCAT UNION").setParameter("departmentName", "Sales");
            results = query.list();
            session.getTransaction().commit();
            session.clear();
            assertEquals(results.size(), 2);
            assertTrue(QueryHintTestSQLServer2012Dialect.getProcessedSql().contains("MAXDOP 2"));
            assertTrue(QueryHintTestSQLServer2012Dialect.getProcessedSql().contains("CONCAT UNION"));
            QueryHintTestSQLServer2012Dialect.resetProcessedSql();
            // ensure the insertion logic can handle a comment appended to the front
            session.getTransaction().begin();
            query = session.createQuery("FROM QueryHintSQLServer2012Test$Employee e WHERE e.department.name = :departmentName").setComment("this is a test").addQueryHint("MAXDOP 2").setParameter("departmentName", "Sales");
            results = query.list();
            session.getTransaction().commit();
            session.clear();
            assertEquals(results.size(), 2);
            assertTrue(QueryHintTestSQLServer2012Dialect.getProcessedSql().contains("OPTION (MAXDOP 2)"));
            QueryHintTestSQLServer2012Dialect.resetProcessedSql();
            // test Criteria
            session.getTransaction().begin();
            CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
            CriteriaQuery<Employee> criteria = criteriaBuilder.createQuery(Employee.class);
            Root<Employee> root = criteria.from(Employee.class);
            Join<Object, Object> departement = root.join("department", JoinType.INNER);
            criteria.select(root).where(criteriaBuilder.equal(departement.get("name"), "Sales"));
            // Criteria criteria = s.createCriteria( Employee.class ).addQueryHint( "MAXDOP 2" ).createCriteria( "department" )
            // .add( Restrictions.eq( "name", "Sales" ) );
            // results = criteria.list();
            Query<Employee> criteriaQuery = session.createQuery(criteria);
            criteriaQuery.addQueryHint("MAXDOP 2");
            results = criteriaQuery.list();
            session.getTransaction().commit();
            return results;
        } finally {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        }
    });
    assertEquals(result.size(), 2);
    assertTrue(QueryHintTestSQLServer2012Dialect.getProcessedSql().contains("OPTION (MAXDOP 2)"));
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) Query(org.hibernate.query.Query) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 5 with CriteriaQuery

use of jakarta.persistence.criteria.CriteriaQuery in project hibernate-orm by hibernate.

the class QueryCacheTest method testGetByCompositeId.

@Test
@TestForIssue(jiraKey = "HHH-4459")
public void testGetByCompositeId(SessionFactoryScope scope) {
    scope.inSession(session -> {
        session.beginTransaction();
        try {
            session.persist(new EntityWithCompositeKey(PK));
            Query query = session.createQuery("FROM EntityWithCompositeKey e WHERE e.pk = :pk");
            query.setCacheable(true);
            query.setParameter("pk", PK);
            assertEquals(1, query.list().size());
        } finally {
            session.getTransaction().rollback();
        }
    });
    scope.inSession(session -> {
        session.beginTransaction();
        try {
            EntityWithStringCompositeKey entity = new EntityWithStringCompositeKey();
            StringCompositeKey key = new StringCompositeKey();
            key.setAnalog("foo1");
            key.setDevice("foo2");
            key.setDeviceType("foo3");
            key.setSubstation("foo4");
            entity.setPk(key);
            session.persist(entity);
            CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
            CriteriaQuery<EntityWithStringCompositeKey> criteria = criteriaBuilder.createQuery(EntityWithStringCompositeKey.class);
            Root<EntityWithStringCompositeKey> root = criteria.from(EntityWithStringCompositeKey.class);
            criteria.where(criteriaBuilder.equal(root.get("pk"), key));
            session.createQuery(criteria).setCacheable(true);
            assertEquals(1, session.createQuery(criteria).list().size());
        // Criteria c = s.createCriteria(
        // EntityWithStringCompositeKey.class ).add( Restrictions.eq(
        // "pk", key ) );
        // c.setCacheable( true );
        // assertEquals( 1, c.list().size() );
        } finally {
            session.getTransaction().rollback();
        }
    });
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) NativeQuery(org.hibernate.query.NativeQuery) Query(org.hibernate.query.Query) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) Test(org.junit.jupiter.api.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

CriteriaQuery (jakarta.persistence.criteria.CriteriaQuery)96 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)82 EntityManager (jakarta.persistence.EntityManager)64 Query (jakarta.persistence.Query)63 List (java.util.List)54 TypedQuery (jakarta.persistence.TypedQuery)35 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)32 Test (org.junit.jupiter.api.Test)24 JpaQuery (org.eclipse.persistence.jpa.JpaQuery)20 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)16 JpaCriteriaBuilder (org.eclipse.persistence.jpa.JpaCriteriaBuilder)14 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)13 ReportQuery (org.eclipse.persistence.queries.ReportQuery)12 Query (org.hibernate.query.Query)12 QuerySQLTracker (org.eclipse.persistence.testing.framework.QuerySQLTracker)11 BasicEntity (org.hibernate.testing.orm.domain.gambit.BasicEntity)11 Root (jakarta.persistence.criteria.Root)8 HibernateCriteriaBuilder (org.hibernate.query.criteria.HibernateCriteriaBuilder)8 DatabasePlatform (org.eclipse.persistence.platform.database.DatabasePlatform)7