Search in sources :

Example 1 with JpaMetamodel

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

the class BasicCriteriaResultTests method testBasicStatelessSelection.

@Test
public void testBasicStatelessSelection(SessionFactoryScope scope) {
    scope.inStatelessTransaction((session) -> {
        final CriteriaBuilder builder = scope.getSessionFactory().getCriteriaBuilder();
        final JpaMetamodel jpaMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getJpaMetamodel();
        final EntityDomainType<SimpleEntity> entityDescriptor = jpaMetamodel.entity(SimpleEntity.class);
        final SingularPersistentAttribute<? super SimpleEntity, Integer> idAttribute = entityDescriptor.getId(Integer.class);
        final CriteriaQuery<Integer> criteria = builder.createQuery(Integer.class);
        final Root<SimpleEntity> root = criteria.from(SimpleEntity.class);
        // final Path<Integer> idPath = root.get( SimpleEntity_ );
        final Path<Integer> idPath = root.get(idAttribute);
        criteria.select(idPath);
        criteria.orderBy(builder.asc(idPath));
        session.createQuery(criteria).list();
    });
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) JpaMetamodel(org.hibernate.metamodel.model.domain.JpaMetamodel) Test(org.junit.jupiter.api.Test)

Example 2 with JpaMetamodel

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

the class BasicCriteriaResultTests method testBasicAndCompositeTuple.

@Test
public void testBasicAndCompositeTuple(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, Integer> idAttribute = entityDescriptor.getId(Integer.class);
        final SingularAttribute<? super SimpleEntity, SimpleComposite> compositeAttribute = entityDescriptor.getSingularAttribute("composite", SimpleComposite.class);
        final CriteriaQuery<Tuple> criteria = builder.createQuery(Tuple.class);
        final Root<SimpleEntity> root = criteria.from(SimpleEntity.class);
        final Path<Integer> idPath = root.get(idAttribute);
        final Path<SimpleComposite> compositePath = root.get(compositeAttribute);
        criteria.multiselect(idPath.alias("id"), compositePath.alias("composite"));
        criteria.orderBy(builder.asc(idPath));
        final List<Tuple> list = session.createQuery(criteria).list();
        assertThat(list).hasSize(1);
        final Tuple result = list.get(0);
        assertThat(result.get(0)).isEqualTo(1);
        assertThat(result.get("id")).isEqualTo(1);
        assertThat(result.get(0)).isSameAs(result.get("id"));
        assertThat(result.get(1)).isInstanceOf(SimpleComposite.class);
        assertThat(result.get(1)).isSameAs(result.get("composite"));
    });
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) JpaMetamodel(org.hibernate.metamodel.model.domain.JpaMetamodel) Tuple(jakarta.persistence.Tuple) Test(org.junit.jupiter.api.Test)

Example 3 with JpaMetamodel

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

the class BasicCriteriaResultTests method testBasicTupleSelection.

@Test
public void testBasicTupleSelection(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, Integer> idAttribute = entityDescriptor.getId(Integer.class);
        final SingularAttribute<? super SimpleEntity, String> nameAttribute = entityDescriptor.getSingularAttribute("name", String.class);
        final CriteriaQuery<Tuple> criteria = builder.createQuery(Tuple.class);
        final Root<SimpleEntity> root = criteria.from(SimpleEntity.class);
        final Path<Integer> idPath = root.get(idAttribute);
        final Path<String> namePath = root.get(nameAttribute);
        criteria.multiselect(idPath.alias("id"), namePath.alias("name"));
        criteria.orderBy(builder.asc(idPath), builder.asc(namePath));
        final List<Tuple> list = session.createQuery(criteria).list();
        assertThat(list).hasSize(1);
        final Tuple result = list.get(0);
        assertThat(result.get(0)).isEqualTo(1);
        assertThat(result.get("id")).isEqualTo(1);
        assertThat(result.get(1)).isEqualTo("first");
        assertThat(result.get("name")).isEqualTo("first");
    });
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) JpaMetamodel(org.hibernate.metamodel.model.domain.JpaMetamodel) Tuple(jakarta.persistence.Tuple) Test(org.junit.jupiter.api.Test)

Example 4 with JpaMetamodel

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

the class BasicCriteriaResultTests method testBasicSelection.

@Test
public void testBasicSelection(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 SingularPersistentAttribute<? super SimpleEntity, Integer> idAttribute = entityDescriptor.getId(Integer.class);
        final CriteriaQuery<Integer> criteria = builder.createQuery(Integer.class);
        final Root<SimpleEntity> root = criteria.from(SimpleEntity.class);
        // final Path<Integer> idPath = root.get( SimpleEntity_ );
        final Path<Integer> idPath = root.get(idAttribute);
        criteria.select(idPath);
        criteria.orderBy(builder.asc(idPath));
        session.createQuery(criteria).list();
    });
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) JpaMetamodel(org.hibernate.metamodel.model.domain.JpaMetamodel) Test(org.junit.jupiter.api.Test)

Example 5 with JpaMetamodel

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

the class BasicCriteriaResultTests method testBasicStatelessSelectionMixedPathRefs.

@Test
public void testBasicStatelessSelectionMixedPathRefs(SessionFactoryScope scope) {
    scope.inStatelessTransaction((session) -> {
        final CriteriaBuilder builder = scope.getSessionFactory().getCriteriaBuilder();
        final JpaMetamodel jpaMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getJpaMetamodel();
        final EntityDomainType<SimpleEntity> entityDescriptor = jpaMetamodel.entity(SimpleEntity.class);
        final Class<Integer> idType = (Class<Integer>) entityDescriptor.getIdType().getJavaType();
        final CriteriaQuery<Integer> criteria = builder.createQuery(Integer.class);
        final Root<SimpleEntity> root = criteria.from(SimpleEntity.class);
        criteria.select(root.get(entityDescriptor.getId(idType)));
        criteria.orderBy(builder.asc(root.get("id")));
        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