Search in sources :

Example 11 with BasicEntity

use of org.hibernate.testing.orm.domain.gambit.BasicEntity in project hibernate-orm by hibernate.

the class CriteriaParameterTests method testMultiValuedParameter.

@Test
public void testMultiValuedParameter(SessionFactoryScope scope) {
    scope.inTransaction(session -> {
        final CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
        final ParameterExpression<Collection> parameter = criteriaBuilder.parameter(Collection.class);
        final CriteriaQuery<BasicEntity> criteria = criteriaBuilder.createQuery(BasicEntity.class);
        final Root<BasicEntity> root = criteria.from(BasicEntity.class);
        final CriteriaBuilder.In<Object> inPredicate = criteriaBuilder.in(root.get("data"));
        inPredicate.value(parameter);
        criteria.where(inPredicate);
        final QueryImplementor<BasicEntity> query = session.createQuery(criteria);
        query.setParameter(parameter, Arrays.asList("fe", "fi", "fo", "fum"));
        query.list();
    });
}
Also used : HibernateCriteriaBuilder(org.hibernate.query.criteria.HibernateCriteriaBuilder) CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Collection(java.util.Collection) BasicEntity(org.hibernate.testing.orm.domain.gambit.BasicEntity) Test(org.junit.jupiter.api.Test)

Example 12 with BasicEntity

use of org.hibernate.testing.orm.domain.gambit.BasicEntity in project hibernate-orm by hibernate.

the class CriteriaParameterTests method testMultiValuedParameterBaseline3.

@Test
public void testMultiValuedParameterBaseline3(SessionFactoryScope scope) {
    scope.inTransaction(session -> {
        final HibernateCriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
        final ParameterExpression<String> parameter = criteriaBuilder.parameter(String.class);
        final CriteriaQuery<BasicEntity> criteria = criteriaBuilder.createQuery(BasicEntity.class);
        final Root<BasicEntity> root = criteria.from(BasicEntity.class);
        final CriteriaBuilder.In<Object> inPredicate = criteriaBuilder.in(root.get("data"), parameter);
        criteria.where(inPredicate);
        final QueryImplementor<BasicEntity> query = session.createQuery(criteria);
        query.setParameter(parameter, "fe");
        query.list();
    });
}
Also used : HibernateCriteriaBuilder(org.hibernate.query.criteria.HibernateCriteriaBuilder) CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) HibernateCriteriaBuilder(org.hibernate.query.criteria.HibernateCriteriaBuilder) BasicEntity(org.hibernate.testing.orm.domain.gambit.BasicEntity) Test(org.junit.jupiter.api.Test)

Example 13 with BasicEntity

use of org.hibernate.testing.orm.domain.gambit.BasicEntity in project hibernate-orm by hibernate.

the class MultiSelectTests method multiSelectTupleTest.

@Test
public void multiSelectTupleTest(SessionFactoryScope scope) {
    scope.inTransaction((session) -> {
        final CriteriaBuilder nodeBuilder = session.getFactory().getNodeBuilder();
        final CriteriaQuery<Tuple> criteria = nodeBuilder.createTupleQuery();
        final Root<BasicEntity> root = criteria.from(BasicEntity.class);
        final EntityType<BasicEntity> model = root.getModel();
        criteria.multiselect(root.get(model.getDeclaredSingularAttribute("id", Integer.class)), root.get(model.getDeclaredSingularAttribute("data", String.class)));
        final List<Tuple> results = session.createQuery(criteria).list();
        assertThat(results).hasSize(1);
        final Tuple firstResult = results.get(0);
        assertThat(firstResult.get(0)).isEqualTo(1);
        assertThat(firstResult.get(1)).isEqualTo("abc");
    });
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Tuple(jakarta.persistence.Tuple) BasicEntity(org.hibernate.testing.orm.domain.gambit.BasicEntity) Test(org.junit.jupiter.api.Test)

Example 14 with BasicEntity

use of org.hibernate.testing.orm.domain.gambit.BasicEntity in project hibernate-orm by hibernate.

the class MultiSelectTests method typedArrayTest.

@Test
public void typedArrayTest(SessionFactoryScope scope) {
    scope.inTransaction((session) -> {
        final CriteriaBuilder nodeBuilder = session.getFactory().getNodeBuilder();
        final CriteriaQuery<Object[]> criteria = nodeBuilder.createQuery(Object[].class);
        final Root<BasicEntity> root = criteria.from(BasicEntity.class);
        final EntityType<BasicEntity> model = root.getModel();
        criteria.select(nodeBuilder.array(root.get(model.getDeclaredSingularAttribute("id", Integer.class)), root.get(model.getDeclaredSingularAttribute("data", String.class))));
        final List<Object[]> results = session.createQuery(criteria).list();
        assertThat(results).hasSize(1);
        final Object[] firstResult = results.get(0);
        assertThat(firstResult[0]).isEqualTo(1);
        assertThat(firstResult[1]).isEqualTo("abc");
    });
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) BasicEntity(org.hibernate.testing.orm.domain.gambit.BasicEntity) Test(org.junit.jupiter.api.Test)

Example 15 with BasicEntity

use of org.hibernate.testing.orm.domain.gambit.BasicEntity in project hibernate-orm by hibernate.

the class MultiSelectTests method typedTupleTest.

@Test
public void typedTupleTest(SessionFactoryScope scope) {
    scope.inTransaction((session) -> {
        final CriteriaBuilder nodeBuilder = session.getFactory().getNodeBuilder();
        final CriteriaQuery<Tuple> criteria = nodeBuilder.createQuery(Tuple.class);
        final Root<BasicEntity> root = criteria.from(BasicEntity.class);
        final EntityType<BasicEntity> model = root.getModel();
        criteria.select(nodeBuilder.tuple(root.get(model.getDeclaredSingularAttribute("id", Integer.class)), root.get(model.getDeclaredSingularAttribute("data", String.class))));
        final List<Tuple> results = session.createQuery(criteria).list();
        assertThat(results).hasSize(1);
        final Tuple firstResult = results.get(0);
        assertThat(firstResult.get(0)).isEqualTo(1);
        assertThat(firstResult.get(1)).isEqualTo("abc");
    });
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Tuple(jakarta.persistence.Tuple) BasicEntity(org.hibernate.testing.orm.domain.gambit.BasicEntity) Test(org.junit.jupiter.api.Test)

Aggregations

BasicEntity (org.hibernate.testing.orm.domain.gambit.BasicEntity)44 Test (org.junit.jupiter.api.Test)39 HibernateCriteriaBuilder (org.hibernate.query.criteria.HibernateCriteriaBuilder)23 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)15 CriteriaQuery (jakarta.persistence.criteria.CriteriaQuery)11 List (java.util.List)8 Query (org.hibernate.query.Query)8 Tuple (jakarta.persistence.Tuple)6 BeforeEach (org.junit.jupiter.api.BeforeEach)3 Collection (java.util.Collection)2 JoinedInheritanceTest (org.hibernate.orm.test.mapping.inheritance.joined.JoinedInheritanceTest)2 StatisticsImplementor (org.hibernate.stat.spi.StatisticsImplementor)2 SkipForDialect (org.hibernate.testing.SkipForDialect)2 EntityWithManyToOneJoinTable (org.hibernate.testing.orm.domain.gambit.EntityWithManyToOneJoinTable)2 SimpleEntity (org.hibernate.testing.orm.domain.gambit.SimpleEntity)2 RequiresDialect (org.hibernate.testing.orm.junit.RequiresDialect)2 BeforeAll (org.junit.jupiter.api.BeforeAll)2 Selection (jakarta.persistence.criteria.Selection)1 ArrayList (java.util.ArrayList)1 SingleIdEntityLoaderStandardImpl (org.hibernate.loader.ast.internal.SingleIdEntityLoaderStandardImpl)1