Search in sources :

Example 11 with JpaMetamodel

use of org.hibernate.metamodel.model.domain.JpaMetamodel in project hibernate-orm by hibernate.

the class AbstractCommonQueryContract method applyGraph.

protected void applyGraph(String graphString, GraphSemantic graphSemantic) {
    final int separatorPosition = graphString.indexOf('(');
    final int terminatorPosition = graphString.lastIndexOf(')');
    if (separatorPosition < 0 || terminatorPosition < 0) {
        throw new IllegalArgumentException(String.format(ROOT, "Invalid entity-graph definition `%s`; expected form `${EntityName}( ${property1} ... )", graphString));
    }
    final RuntimeMetamodels runtimeMetamodels = getSession().getFactory().getRuntimeMetamodels();
    final JpaMetamodel jpaMetamodel = runtimeMetamodels.getJpaMetamodel();
    final String entityName = runtimeMetamodels.getImportedName(graphString.substring(0, separatorPosition).trim());
    final String graphNodes = graphString.substring(separatorPosition + 1, terminatorPosition);
    final RootGraphImpl<?> rootGraph = new RootGraphImpl<>(null, jpaMetamodel.entity(entityName), jpaMetamodel);
    GraphParser.parseInto((EntityGraph<?>) rootGraph, graphNodes, getSession().getSessionFactory());
    applyGraph(rootGraph, graphSemantic);
}
Also used : RuntimeMetamodels(org.hibernate.metamodel.RuntimeMetamodels) RootGraphImpl(org.hibernate.graph.internal.RootGraphImpl) JpaMetamodel(org.hibernate.metamodel.model.domain.JpaMetamodel)

Example 12 with JpaMetamodel

use of org.hibernate.metamodel.model.domain.JpaMetamodel in project hibernate-orm by hibernate.

the class CompositeParameterTests method testSimplePredicateCriteria.

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

Example 13 with JpaMetamodel

use of org.hibernate.metamodel.model.domain.JpaMetamodel 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)

Example 14 with JpaMetamodel

use of org.hibernate.metamodel.model.domain.JpaMetamodel in project hibernate-orm by hibernate.

the class BasicCriteriaResultTests method testCompositeSelection.

@Test
public void testCompositeSelection(SessionFactoryScope scope) {
    scope.inTransaction((session) -> {
        final CriteriaBuilder builder = scope.getSessionFactory().getCriteriaBuilder();
        final JpaMetamodel jpaMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getJpaMetamodel();
        final EntityDomainType<SimpleEntity> entityDescriptor = jpaMetamodel.entity(SimpleEntity.class);
        final SingularAttribute<? super SimpleEntity, SimpleComposite> compositeAttribute;
        compositeAttribute = entityDescriptor.getSingularAttribute("composite", SimpleComposite.class);
        final CriteriaQuery<SimpleComposite> criteria = builder.createQuery(SimpleComposite.class);
        final Root<SimpleEntity> root = criteria.from(SimpleEntity.class);
        final Path<SimpleComposite> attributePath = root.get(compositeAttribute);
        criteria.select(attributePath);
        criteria.orderBy(builder.asc(attributePath));
        session.createQuery(criteria).list();
    });
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) JpaMetamodel(org.hibernate.metamodel.model.domain.JpaMetamodel) Test(org.junit.jupiter.api.Test)

Aggregations

JpaMetamodel (org.hibernate.metamodel.model.domain.JpaMetamodel)14 Test (org.junit.jupiter.api.Test)12 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)9 HibernateCriteriaBuilder (org.hibernate.query.criteria.HibernateCriteriaBuilder)3 Tuple (jakarta.persistence.Tuple)2 SqmComparisonPredicate (org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate)2 Path (jakarta.persistence.criteria.Path)1 Predicate (jakarta.persistence.criteria.Predicate)1 SingularAttribute (jakarta.persistence.metamodel.SingularAttribute)1 Field (java.lang.reflect.Field)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 RootGraphImpl (org.hibernate.graph.internal.RootGraphImpl)1 MappingMetamodel (org.hibernate.metamodel.MappingMetamodel)1 RuntimeMetamodels (org.hibernate.metamodel.RuntimeMetamodels)1 EntityDomainType (org.hibernate.metamodel.model.domain.EntityDomainType)1 MappingMetamodelImpl (org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl)1 JpaCriteriaQuery (org.hibernate.query.criteria.JpaCriteriaQuery)1 JpaParameterExpression (org.hibernate.query.criteria.JpaParameterExpression)1