Search in sources :

Example 26 with BasicEntity

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

the class CriteriaParameterTests method testMultiValuedParameterBaseline.

@Test
public void testMultiValuedParameterBaseline(SessionFactoryScope scope) {
    scope.inTransaction(session -> {
        final CriteriaBuilder 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"));
        inPredicate.value("fe");
        inPredicate.value("fi");
        inPredicate.value("fo");
        inPredicate.value("fum");
        criteria.where(inPredicate);
        final QueryImplementor<BasicEntity> query = session.createQuery(criteria);
        query.list();
    });
}
Also used : HibernateCriteriaBuilder(org.hibernate.query.criteria.HibernateCriteriaBuilder) CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) BasicEntity(org.hibernate.testing.orm.domain.gambit.BasicEntity) Test(org.junit.jupiter.api.Test)

Example 27 with BasicEntity

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

the class CriteriaParameterTests method testNamedParameterBaseline.

@Test
public void testNamedParameterBaseline(SessionFactoryScope scope) {
    scope.inTransaction(session -> {
        final CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
        final ParameterExpression<Collection> parameter = criteriaBuilder.parameter(Collection.class, "datas");
        final CriteriaQuery<BasicEntity> criteria = criteriaBuilder.createQuery(BasicEntity.class);
        final Root<BasicEntity> root = criteria.from(BasicEntity.class);
        Path<?> property = root.get("data");
        criteria.where(property.in(parameter));
        final QueryImplementor<BasicEntity> query = session.createQuery(criteria);
        List<String> parameterValue = new ArrayList<>();
        parameterValue.add("fe");
        query.setParameter("datas", parameterValue);
        query.list();
    });
}
Also used : HibernateCriteriaBuilder(org.hibernate.query.criteria.HibernateCriteriaBuilder) CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) ArrayList(java.util.ArrayList) Collection(java.util.Collection) BasicEntity(org.hibernate.testing.orm.domain.gambit.BasicEntity) Test(org.junit.jupiter.api.Test)

Example 28 with BasicEntity

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

the class CriteriaParameterTests method testMultiValuedParameterBaseline2.

@Test
public void testMultiValuedParameterBaseline2(SessionFactoryScope scope) {
    scope.inTransaction(session -> {
        final CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
        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"));
        final ParameterExpression<String> p1 = criteriaBuilder.parameter(String.class);
        inPredicate.value(p1);
        final ParameterExpression<String> p2 = criteriaBuilder.parameter(String.class);
        inPredicate.value(p2);
        criteria.where(inPredicate);
        final QueryImplementor<BasicEntity> query = session.createQuery(criteria);
        query.setParameter(p1, "fe");
        query.setParameter(p2, "fi");
        query.list();
    });
}
Also used : HibernateCriteriaBuilder(org.hibernate.query.criteria.HibernateCriteriaBuilder) CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) BasicEntity(org.hibernate.testing.orm.domain.gambit.BasicEntity) Test(org.junit.jupiter.api.Test)

Example 29 with BasicEntity

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

the class CriteriaParameterTests method testParameterBaseline.

@Test
public void testParameterBaseline(SessionFactoryScope scope) {
    scope.inTransaction(session -> {
        final CriteriaBuilder 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);
        criteria.where(criteriaBuilder.equal(root.get("data"), parameter));
        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) BasicEntity(org.hibernate.testing.orm.domain.gambit.BasicEntity) Test(org.junit.jupiter.api.Test)

Example 30 with BasicEntity

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

the class MultiSelectTests method arrayTupleTest.

@Test
public void arrayTupleTest(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.select((Selection) nodeBuilder.array(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