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();
}
}
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);
});
}
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);
}
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;
}
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;
}
Aggregations