Search in sources :

Example 1 with JpaParameterExpression

use of org.hibernate.query.criteria.JpaParameterExpression in project hibernate-orm by hibernate.

the class CompositeParameterTests method testDeTypedInPredicateCriteria.

@Test
public void testDeTypedInPredicateCriteria(SessionFactoryScope scope) {
    final HibernateCriteriaBuilder builder = scope.getSessionFactory().getCriteriaBuilder();
    final JpaMetamodel jpaMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getJpaMetamodel();
    final EntityDomainType entityDescriptor = jpaMetamodel.entity(SimpleEntity.class);
    final SingularAttribute attribute = entityDescriptor.getSingularAttribute("composite");
    scope.inTransaction((session) -> {
        final JpaCriteriaQuery criteria = builder.createQuery(SimpleEntity.class);
        final JpaRoot root = criteria.from(entityDescriptor);
        final Path attrPath = root.get(attribute);
        final JpaParameterExpression parameter = builder.parameter(SimpleComposite.class);
        criteria.where(builder.in(attrPath, parameter));
        final QueryImplementor query = session.createQuery(criteria);
        query.setParameter(parameter, new SimpleComposite());
        query.list();
    });
}
Also used : Path(jakarta.persistence.criteria.Path) SingularAttribute(jakarta.persistence.metamodel.SingularAttribute) JpaRoot(org.hibernate.query.criteria.JpaRoot) HibernateCriteriaBuilder(org.hibernate.query.criteria.HibernateCriteriaBuilder) EntityDomainType(org.hibernate.metamodel.model.domain.EntityDomainType) JpaMetamodel(org.hibernate.metamodel.model.domain.JpaMetamodel) JpaCriteriaQuery(org.hibernate.query.criteria.JpaCriteriaQuery) JpaParameterExpression(org.hibernate.query.criteria.JpaParameterExpression) QueryImplementor(org.hibernate.query.spi.QueryImplementor) Test(org.junit.jupiter.api.Test)

Aggregations

Path (jakarta.persistence.criteria.Path)1 SingularAttribute (jakarta.persistence.metamodel.SingularAttribute)1 EntityDomainType (org.hibernate.metamodel.model.domain.EntityDomainType)1 JpaMetamodel (org.hibernate.metamodel.model.domain.JpaMetamodel)1 HibernateCriteriaBuilder (org.hibernate.query.criteria.HibernateCriteriaBuilder)1 JpaCriteriaQuery (org.hibernate.query.criteria.JpaCriteriaQuery)1 JpaParameterExpression (org.hibernate.query.criteria.JpaParameterExpression)1 JpaRoot (org.hibernate.query.criteria.JpaRoot)1 QueryImplementor (org.hibernate.query.spi.QueryImplementor)1 Test (org.junit.jupiter.api.Test)1