Search in sources :

Example 1 with Path

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

the class AbstractPathImplTest method testIllegalDereference.

@Test
public void testIllegalDereference() {
    EntityManager em = getOrCreateEntityManager();
    try {
        CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
        CriteriaQuery<Order> criteria = criteriaBuilder.createQuery(Order.class);
        Root<Order> orderRoot = criteria.from(Order.class);
        Path simplePath = orderRoot.get("totalPrice");
        // this should cause an ISE...
        try {
            simplePath.get("yabbadabbado");
            fail("Attempt to dereference basic path should throw IllegalStateException");
        } catch (IllegalStateException expected) {
        }
    } finally {
        em.close();
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Order(org.hibernate.orm.test.jpa.metamodel.Order) Path(jakarta.persistence.criteria.Path) EntityManager(jakarta.persistence.EntityManager) AbstractMetamodelSpecificTest(org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest) Test(org.junit.jupiter.api.Test)

Example 2 with Path

use of jakarta.persistence.criteria.Path 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 3 with Path

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

the class AbstractQueryCacheResultTransformerTest method testAliasToBeanDtoMultiArgList.

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

        @Override
        protected ResultTransformer getResultTransformer() {
            return Transformers.aliasToBean(StudentDTO.class);
        }

        @Override
        protected JpaCriteriaQuery getCriteria(Session s) {
            CriteriaBuilder builder = s.getCriteriaBuilder();
            JpaCriteriaQuery criteria = (JpaCriteriaQuery) builder.createQuery();
            Root<Enrolment> root = criteria.from(Enrolment.class);
            final Path<Object> student = root.get("student");
            criteria.multiselect(student.get("name").alias("studentName"), root.get("course").get("description").alias("courseDescription"));
            criteria.orderBy(builder.asc(root.get("studentNumber")));
            return criteria;
        // return s.createCriteria( Enrolment.class, "e" )
        // .createAlias( "e.student", "st" )
        // .createAlias( "e.course", "co" )
        // .setProjection(
        // Projections.projectionList()
        // .add( Property.forName( "st.name" ).as( "studentName" ) )
        // .add( Property.forName( "co.description" ).as( "courseDescription" ) )
        // )
        // .addOrder( Order.asc( "e.studentNumber" ) )
        // .setResultTransformer( Transformers.aliasToBean( StudentDTO.class ) );
        }
    };
    HqlExecutor hqlExecutor = new HqlExecutor() {

        @Override
        public Query getQuery(Session s) {
            return s.createQuery("select st.name as studentName, co.description as courseDescription from Enrolment e join e.student st join e.course co order by e.studentNumber").setResultTransformer(Transformers.aliasToBean(StudentDTO.class));
        }
    };
    ResultChecker checker = results -> {
        List resultList = (List) results;
        assertEquals(2, resultList.size());
        StudentDTO dto = (StudentDTO) resultList.get(0);
        assertEquals(courseExpected.getDescription(), dto.getDescription());
        assertEquals(yogiExpected.getName(), dto.getName());
        dto = (StudentDTO) resultList.get(1);
        assertEquals(courseExpected.getDescription(), dto.getDescription());
        assertEquals(shermanExpected.getName(), dto.getName());
    };
    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)

Example 4 with Path

use of jakarta.persistence.criteria.Path in project eclipselink by eclipse-ee4j.

the class CriteriaBuilderImpl method treat.

@Override
public <X, T, E extends T> ListJoin<X, E> treat(ListJoin<X, T> join, Class<E> type) {
    ListJoinImpl parentJoin = (ListJoinImpl) join;
    ListJoin joinImpl = null;
    if (join instanceof BasicListJoinImpl) {
        joinImpl = new BasicListJoinImpl<X, E>(parentJoin, this.metamodel, type, parentJoin.currentNode.treat(type), parentJoin.getModel(), parentJoin.getJoinType());
    } else {
        joinImpl = new ListJoinImpl<X, E>((Path) join, this.metamodel.managedType(type), this.metamodel, type, parentJoin.currentNode.treat(type), parentJoin.getModel(), parentJoin.getJoinType());
    }
    parentJoin.joins.add(joinImpl);
    ((FromImpl) joinImpl).isJoin = parentJoin.isJoin;
    parentJoin.isJoin = false;
    return joinImpl;
}
Also used : Path(jakarta.persistence.criteria.Path) ListJoin(jakarta.persistence.criteria.ListJoin)

Example 5 with Path

use of jakarta.persistence.criteria.Path in project eclipselink by eclipse-ee4j.

the class CriteriaBuilderImpl method treat.

@Override
public <X, K, T, V extends T> MapJoin<X, K, V> treat(MapJoin<X, K, T> join, Class<V> type) {
    MapJoinImpl parentJoin = (MapJoinImpl) join;
    MapJoin joinImpl = null;
    if (join instanceof BasicMapJoinImpl) {
        joinImpl = new BasicMapJoinImpl<X, K, V>(parentJoin, this.metamodel, type, parentJoin.currentNode.treat(type), parentJoin.getModel(), parentJoin.getJoinType());
    } else {
        joinImpl = new MapJoinImpl<X, K, V>((Path) join, this.metamodel.managedType(type), this.metamodel, type, parentJoin.currentNode.treat(type), parentJoin.getModel(), parentJoin.getJoinType());
    }
    parentJoin.joins.add(joinImpl);
    ((FromImpl) joinImpl).isJoin = parentJoin.isJoin;
    parentJoin.isJoin = false;
    return joinImpl;
}
Also used : Path(jakarta.persistence.criteria.Path) MapJoin(jakarta.persistence.criteria.MapJoin)

Aggregations

Path (jakarta.persistence.criteria.Path)8 Test (org.junit.jupiter.api.Test)4 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)3 ListJoin (jakarta.persistence.criteria.ListJoin)2 MapJoin (jakarta.persistence.criteria.MapJoin)2 JpaCriteriaQuery (org.hibernate.query.criteria.JpaCriteriaQuery)2 JpaRoot (org.hibernate.query.criteria.JpaRoot)2 EntityManager (jakarta.persistence.EntityManager)1 TypedQuery (jakarta.persistence.TypedQuery)1 CollectionJoin (jakarta.persistence.criteria.CollectionJoin)1 CriteriaQuery (jakarta.persistence.criteria.CriteriaQuery)1 From (jakarta.persistence.criteria.From)1 Join (jakarta.persistence.criteria.Join)1 JoinType (jakarta.persistence.criteria.JoinType)1 Order (jakarta.persistence.criteria.Order)1 Root (jakarta.persistence.criteria.Root)1 Selection (jakarta.persistence.criteria.Selection)1 SetJoin (jakarta.persistence.criteria.SetJoin)1 SingularAttribute (jakarta.persistence.metamodel.SingularAttribute)1 Constructor (java.lang.reflect.Constructor)1