Search in sources :

Example 1 with Root

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

the class SaveOrUpdateTest method getRowCount.

private Long getRowCount(Session s, Class clazz) {
    CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = criteriaBuilder.createQuery(Long.class);
    Root root = criteria.from(clazz);
    criteria.select(criteriaBuilder.count(root));
    return s.createQuery(criteria).uniqueResult();
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Root(jakarta.persistence.criteria.Root)

Example 2 with Root

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

the class AbstractQueryCacheResultTransformerTest method testAliasToEntityMapNoProjectionNullAndNonNullAliasList.

@Test
public void testAliasToEntityMapNoProjectionNullAndNonNullAliasList(SessionFactoryScope scope) throws Exception {
    CriteriaExecutor criteriaExecutor = new CriteriaExecutor() {

        @Override
        protected JpaCriteriaQuery getCriteria(Session s) {
            final CriteriaBuilder builder = s.getSessionFactory().getCriteriaBuilder();
            final JpaCriteriaQuery<Student> criteria = (JpaCriteriaQuery<Student>) builder.createQuery(Student.class);
            final Root<Student> studentRoot = criteria.from(Student.class);
            studentRoot.alias("s");
            studentRoot.join("addresses", JoinType.LEFT).alias("a");
            studentRoot.join("preferredCourse", JoinType.INNER);
            criteria.orderBy(builder.asc(studentRoot.get("studentNumber")));
            return criteria;
        // return s.createCriteria( Student.class, "s" )
        // .createAlias( "s.addresses", "a", CriteriaSpecification.LEFT_JOIN )
        // .setResultTransformer( CriteriaSpecification.ALIAS_TO_ENTITY_MAP )
        // .createCriteria( "s.preferredCourse", CriteriaSpecification.INNER_JOIN )
        // .addOrder( Order.asc( "s.studentNumber") );
        }
    };
    HqlExecutor hqlExecutor = new HqlExecutor() {

        @Override
        public Query getQuery(Session s) {
            return s.createQuery("from Student s left join s.addresses a left join s.preferredCourse order by s.studentNumber").setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        }
    };
    ResultChecker checker = results -> {
        List resultList = (List) results;
        assertEquals(2, resultList.size());
        Map yogiMap1 = (Map) resultList.get(0);
        assertEquals(2, yogiMap1.size());
        Map yogiMap2 = (Map) resultList.get(1);
        assertEquals(2, yogiMap2.size());
        assertEquals(yogiExpected, yogiMap1.get("s"));
        Address yogiAddress1 = (Address) yogiMap1.get("a");
        assertEquals(yogiExpected.getAddresses().get(yogiAddress1.getAddressType()), yogiMap1.get("a"));
        assertEquals(yogiExpected, yogiMap2.get("s"));
        Address yogiAddress2 = (Address) yogiMap2.get("a");
        assertEquals(yogiExpected.getAddresses().get(yogiAddress2.getAddressType()), yogiMap2.get("a"));
        assertSame(yogiMap1.get("s"), yogiMap2.get("s"));
        assertNotEquals(yogiAddress1.getAddressType(), yogiAddress2.getAddressType());
    };
    runTest(hqlExecutor, criteriaExecutor, checker, false, scope);
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) BasicTypeImpl(org.hibernate.type.internal.BasicTypeImpl) ServiceRegistry(org.hibernate.testing.orm.junit.ServiceRegistry) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) JoinType(jakarta.persistence.criteria.JoinType) JpaSelection(org.hibernate.query.criteria.JpaSelection) AvailableSettings(org.hibernate.cfg.AvailableSettings) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Session(org.hibernate.Session) Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) HibernateProxy(org.hibernate.proxy.HibernateProxy) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Map(java.util.Map) Query(org.hibernate.query.Query) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Order(jakarta.persistence.criteria.Order) BigIntJdbcType(org.hibernate.type.descriptor.jdbc.BigIntJdbcType) Transformers(org.hibernate.transform.Transformers) Iterator(java.util.Iterator) ListJoin(jakarta.persistence.criteria.ListJoin) ReflectHelper(org.hibernate.internal.util.ReflectHelper) DomainModel(org.hibernate.testing.orm.junit.DomainModel) AliasToBeanConstructorResultTransformer(org.hibernate.transform.AliasToBeanConstructorResultTransformer) Selection(jakarta.persistence.criteria.Selection) CacheMode(org.hibernate.CacheMode) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) JpaCriteriaQuery(org.hibernate.query.criteria.JpaCriteriaQuery) Test(org.junit.jupiter.api.Test) MapJoin(jakarta.persistence.criteria.MapJoin) List(java.util.List) Root(jakarta.persistence.criteria.Root) SessionFactoryScope(org.hibernate.testing.orm.junit.SessionFactoryScope) CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) JpaRoot(org.hibernate.query.criteria.JpaRoot) LongJavaType(org.hibernate.type.descriptor.java.LongJavaType) Setting(org.hibernate.testing.orm.junit.Setting) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) AliasToEntityMapResultTransformer(org.hibernate.transform.AliasToEntityMapResultTransformer) SessionFactory(org.hibernate.testing.orm.junit.SessionFactory) Path(jakarta.persistence.criteria.Path) Hibernate(org.hibernate.Hibernate) ResultTransformer(org.hibernate.transform.ResultTransformer) Type(org.hibernate.type.Type) Join(jakarta.persistence.criteria.Join) ArrayList(java.util.ArrayList) List(java.util.List) JpaCriteriaQuery(org.hibernate.query.criteria.JpaCriteriaQuery) Map(java.util.Map) Session(org.hibernate.Session) Test(org.junit.jupiter.api.Test)

Example 3 with Root

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

the class AbstractQueryCacheResultTransformerTest method testMultiAggregatedPropProjectionSingleResult.

@Test
public void testMultiAggregatedPropProjectionSingleResult(SessionFactoryScope scope) throws Exception {
    CriteriaExecutor criteriaExecutor = new CriteriaExecutor() {

        @Override
        protected ResultTransformer getResultTransformer() {
            return null;
        }

        @Override
        protected JpaCriteriaQuery getCriteria(Session s) {
            CriteriaBuilder builder = s.getCriteriaBuilder();
            JpaCriteriaQuery criteria = (JpaCriteriaQuery) builder.createQuery(Object[].class);
            Root<Enrolment> root = criteria.from(Enrolment.class);
            criteria.multiselect(builder.min(root.get("studentNumber")), builder.max(root.get("studentNumber")));
            return criteria;
        // return s.createCriteria( Enrolment.class )
        // .setProjection(
        // Projections.projectionList()
        // .add( Projections.min( "studentNumber" ).as( "minStudentNumber" ) )
        // .add( Projections.max( "studentNumber" ).as( "maxStudentNumber" ) )
        // );
        }
    };
    HqlExecutor hqlExecutor = new HqlExecutor() {

        @Override
        public Query getQuery(Session s) {
            return s.createQuery("select min( e.studentNumber ) as minStudentNumber, max( e.studentNumber ) as maxStudentNumber from Enrolment e");
        }
    };
    ResultChecker checker = results -> {
        assertTrue(results instanceof Object[]);
        Object[] resultObjects = (Object[]) results;
        assertEquals(yogiExpected.getStudentNumber(), resultObjects[0]);
        assertEquals(shermanExpected.getStudentNumber(), resultObjects[1]);
    };
    runTest(hqlExecutor, criteriaExecutor, checker, true, scope);
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) BasicTypeImpl(org.hibernate.type.internal.BasicTypeImpl) ServiceRegistry(org.hibernate.testing.orm.junit.ServiceRegistry) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) JoinType(jakarta.persistence.criteria.JoinType) JpaSelection(org.hibernate.query.criteria.JpaSelection) AvailableSettings(org.hibernate.cfg.AvailableSettings) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Session(org.hibernate.Session) Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) HibernateProxy(org.hibernate.proxy.HibernateProxy) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Map(java.util.Map) Query(org.hibernate.query.Query) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Order(jakarta.persistence.criteria.Order) BigIntJdbcType(org.hibernate.type.descriptor.jdbc.BigIntJdbcType) Transformers(org.hibernate.transform.Transformers) Iterator(java.util.Iterator) ListJoin(jakarta.persistence.criteria.ListJoin) ReflectHelper(org.hibernate.internal.util.ReflectHelper) DomainModel(org.hibernate.testing.orm.junit.DomainModel) AliasToBeanConstructorResultTransformer(org.hibernate.transform.AliasToBeanConstructorResultTransformer) Selection(jakarta.persistence.criteria.Selection) CacheMode(org.hibernate.CacheMode) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) JpaCriteriaQuery(org.hibernate.query.criteria.JpaCriteriaQuery) Test(org.junit.jupiter.api.Test) MapJoin(jakarta.persistence.criteria.MapJoin) List(java.util.List) Root(jakarta.persistence.criteria.Root) SessionFactoryScope(org.hibernate.testing.orm.junit.SessionFactoryScope) CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) JpaRoot(org.hibernate.query.criteria.JpaRoot) LongJavaType(org.hibernate.type.descriptor.java.LongJavaType) Setting(org.hibernate.testing.orm.junit.Setting) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) AliasToEntityMapResultTransformer(org.hibernate.transform.AliasToEntityMapResultTransformer) SessionFactory(org.hibernate.testing.orm.junit.SessionFactory) Path(jakarta.persistence.criteria.Path) Hibernate(org.hibernate.Hibernate) ResultTransformer(org.hibernate.transform.ResultTransformer) Type(org.hibernate.type.Type) Join(jakarta.persistence.criteria.Join) JpaCriteriaQuery(org.hibernate.query.criteria.JpaCriteriaQuery) Session(org.hibernate.Session) Test(org.junit.jupiter.api.Test)

Example 4 with Root

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

the class AbstractQueryCacheResultTransformerTest method testOneEntityProjectionUnique.

@Test
public void testOneEntityProjectionUnique(SessionFactoryScope scope) throws Exception {
    CriteriaExecutor criteriaExecutor = new CriteriaExecutor() {

        @Override
        protected ResultTransformer getResultTransformer() {
            return null;
        }

        @Override
        protected JpaCriteriaQuery getCriteria(Session s) {
            CriteriaBuilder builder = s.getCriteriaBuilder();
            JpaCriteriaQuery criteria = (JpaCriteriaQuery) builder.createQuery();
            Root<Enrolment> root = criteria.from(Enrolment.class);
            criteria.select(root.get("student"));
            criteria.where(builder.equal(root.get("studentNumber"), yogiExpected.getStudentNumber()));
            return criteria;
        // should use PassThroughTransformer by default
        // return s.createCriteria( Enrolment.class )
        // .setProjection( Projections.property( "student" ) )
        // .add( Restrictions.eq( "studentNumber", Long.valueOf( yogiExpected.getStudentNumber() ) ) );
        }
    };
    HqlExecutor hqlExecutor = new HqlExecutor() {

        @Override
        public Query getQuery(Session s) {
            return s.createQuery("select e.student from Enrolment e where e.studentNumber = :studentNumber").setParameter("studentNumber", Long.valueOf(yogiExpected.getStudentNumber()));
        }
    };
    ResultChecker checker = results -> {
        assertTrue(results instanceof Student);
        Student student = (Student) results;
        // TODO: following is initialized for hql and uninitialied for criteria; why?
        // assertFalse( Hibernate.isInitialized( student ) );
        assertEquals(yogiExpected.getStudentNumber(), student.getStudentNumber());
    };
    runTest(hqlExecutor, criteriaExecutor, checker, true, scope);
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) BasicTypeImpl(org.hibernate.type.internal.BasicTypeImpl) ServiceRegistry(org.hibernate.testing.orm.junit.ServiceRegistry) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) JoinType(jakarta.persistence.criteria.JoinType) JpaSelection(org.hibernate.query.criteria.JpaSelection) AvailableSettings(org.hibernate.cfg.AvailableSettings) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Session(org.hibernate.Session) Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) HibernateProxy(org.hibernate.proxy.HibernateProxy) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Map(java.util.Map) Query(org.hibernate.query.Query) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Order(jakarta.persistence.criteria.Order) BigIntJdbcType(org.hibernate.type.descriptor.jdbc.BigIntJdbcType) Transformers(org.hibernate.transform.Transformers) Iterator(java.util.Iterator) ListJoin(jakarta.persistence.criteria.ListJoin) ReflectHelper(org.hibernate.internal.util.ReflectHelper) DomainModel(org.hibernate.testing.orm.junit.DomainModel) AliasToBeanConstructorResultTransformer(org.hibernate.transform.AliasToBeanConstructorResultTransformer) Selection(jakarta.persistence.criteria.Selection) CacheMode(org.hibernate.CacheMode) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) JpaCriteriaQuery(org.hibernate.query.criteria.JpaCriteriaQuery) Test(org.junit.jupiter.api.Test) MapJoin(jakarta.persistence.criteria.MapJoin) List(java.util.List) Root(jakarta.persistence.criteria.Root) SessionFactoryScope(org.hibernate.testing.orm.junit.SessionFactoryScope) CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) JpaRoot(org.hibernate.query.criteria.JpaRoot) LongJavaType(org.hibernate.type.descriptor.java.LongJavaType) Setting(org.hibernate.testing.orm.junit.Setting) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) AliasToEntityMapResultTransformer(org.hibernate.transform.AliasToEntityMapResultTransformer) SessionFactory(org.hibernate.testing.orm.junit.SessionFactory) Path(jakarta.persistence.criteria.Path) Hibernate(org.hibernate.Hibernate) ResultTransformer(org.hibernate.transform.ResultTransformer) Type(org.hibernate.type.Type) Join(jakarta.persistence.criteria.Join) JpaCriteriaQuery(org.hibernate.query.criteria.JpaCriteriaQuery) Session(org.hibernate.Session) Test(org.junit.jupiter.api.Test)

Example 5 with Root

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

the class AbstractQueryCacheResultTransformerTest method testListElementsProjectionList.

@Test
public void testListElementsProjectionList(SessionFactoryScope scope) throws Exception {
    CriteriaExecutor criteriaExecutor = new CriteriaExecutor() {

        @Override
        protected ResultTransformer getResultTransformer() {
            return null;
        }

        @Override
        protected JpaCriteriaQuery getCriteria(Session s) {
            CriteriaBuilder builder = s.getCriteriaBuilder();
            JpaCriteriaQuery criteria = (JpaCriteriaQuery) builder.createQuery();
            Root<Enrolment> root = criteria.from(Student.class);
            final ListJoin<Object, Object> secretCodes = root.joinList("secretCodes");
            criteria.select(secretCodes);
            return criteria;
        }
    };
    HqlExecutor hqlExecutor = new HqlExecutor() {

        @Override
        public Query getQuery(Session s) {
            return s.createQuery("select elements(s.secretCodes) from Student s");
        }
    };
    ResultChecker checker = results -> {
        List resultList = (List) results;
        assertEquals(3, resultList.size());
        assertTrue(resultList.contains(yogiExpected.getSecretCodes().get(0)));
        assertTrue(resultList.contains(shermanExpected.getSecretCodes().get(0)));
        assertTrue(resultList.contains(shermanExpected.getSecretCodes().get(1)));
    };
    runTest(hqlExecutor, criteriaExecutor, checker, false, scope);
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) BasicTypeImpl(org.hibernate.type.internal.BasicTypeImpl) ServiceRegistry(org.hibernate.testing.orm.junit.ServiceRegistry) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) JoinType(jakarta.persistence.criteria.JoinType) JpaSelection(org.hibernate.query.criteria.JpaSelection) AvailableSettings(org.hibernate.cfg.AvailableSettings) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Session(org.hibernate.Session) Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) HibernateProxy(org.hibernate.proxy.HibernateProxy) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Map(java.util.Map) Query(org.hibernate.query.Query) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Order(jakarta.persistence.criteria.Order) BigIntJdbcType(org.hibernate.type.descriptor.jdbc.BigIntJdbcType) Transformers(org.hibernate.transform.Transformers) Iterator(java.util.Iterator) ListJoin(jakarta.persistence.criteria.ListJoin) ReflectHelper(org.hibernate.internal.util.ReflectHelper) DomainModel(org.hibernate.testing.orm.junit.DomainModel) AliasToBeanConstructorResultTransformer(org.hibernate.transform.AliasToBeanConstructorResultTransformer) Selection(jakarta.persistence.criteria.Selection) CacheMode(org.hibernate.CacheMode) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) JpaCriteriaQuery(org.hibernate.query.criteria.JpaCriteriaQuery) Test(org.junit.jupiter.api.Test) MapJoin(jakarta.persistence.criteria.MapJoin) List(java.util.List) Root(jakarta.persistence.criteria.Root) SessionFactoryScope(org.hibernate.testing.orm.junit.SessionFactoryScope) CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) JpaRoot(org.hibernate.query.criteria.JpaRoot) LongJavaType(org.hibernate.type.descriptor.java.LongJavaType) Setting(org.hibernate.testing.orm.junit.Setting) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) AliasToEntityMapResultTransformer(org.hibernate.transform.AliasToEntityMapResultTransformer) SessionFactory(org.hibernate.testing.orm.junit.SessionFactory) Path(jakarta.persistence.criteria.Path) Hibernate(org.hibernate.Hibernate) ResultTransformer(org.hibernate.transform.ResultTransformer) Type(org.hibernate.type.Type) Join(jakarta.persistence.criteria.Join) ArrayList(java.util.ArrayList) List(java.util.List) JpaCriteriaQuery(org.hibernate.query.criteria.JpaCriteriaQuery) Session(org.hibernate.Session) Test(org.junit.jupiter.api.Test)

Aggregations

Root (jakarta.persistence.criteria.Root)77 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)71 List (java.util.List)65 Iterator (java.util.Iterator)62 Map (java.util.Map)62 Join (jakarta.persistence.criteria.Join)60 ListJoin (jakarta.persistence.criteria.ListJoin)60 MapJoin (jakarta.persistence.criteria.MapJoin)60 Order (jakarta.persistence.criteria.Order)60 Selection (jakarta.persistence.criteria.Selection)60 Session (org.hibernate.Session)60 JoinType (jakarta.persistence.criteria.JoinType)59 Path (jakarta.persistence.criteria.Path)59 Constructor (java.lang.reflect.Constructor)59 ArrayList (java.util.ArrayList)59 CacheMode (org.hibernate.CacheMode)59 Hibernate (org.hibernate.Hibernate)59 AvailableSettings (org.hibernate.cfg.AvailableSettings)59 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)59 ReflectHelper (org.hibernate.internal.util.ReflectHelper)59