Search in sources :

Example 1 with ParameterExpression

use of jakarta.persistence.criteria.ParameterExpression in project hibernate-orm by hibernate.

the class CriteriaGetParametersTest method testGetParameters.

@Test
public void testGetParameters(EntityManagerFactoryScope scope) {
    scope.inEntityManager(entityManager -> {
        final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        final CriteriaQuery<Person> query = criteriaBuilder.createQuery(Person.class);
        final Root<Person> person = query.from(Person.class);
        query.select(person);
        query.where(criteriaBuilder.equal(person.get("age"), 30));
        final Set<ParameterExpression<?>> parameters = query.getParameters();
        entityManager.createQuery(query).getResultList();
        assertThat(parameters, notNullValue());
        assertTrue(parameters.isEmpty());
    });
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) ParameterExpression(jakarta.persistence.criteria.ParameterExpression) Test(org.junit.jupiter.api.Test)

Example 2 with ParameterExpression

use of jakarta.persistence.criteria.ParameterExpression in project hibernate-orm by hibernate.

the class BasicCriteriaUsageTest method testParameterCollection.

@Test
public void testParameterCollection() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Wall> criteria = em.getCriteriaBuilder().createQuery(Wall.class);
    Root<Wall> from = criteria.from(Wall.class);
    ParameterExpression param = em.getCriteriaBuilder().parameter(String.class);
    SingularAttribute<? super Wall, ?> colorAttribute = em.getMetamodel().entity(Wall.class).getDeclaredSingularAttribute("color");
    assertNotNull("metamodel returned null singular attribute", colorAttribute);
    Predicate predicate = em.getCriteriaBuilder().equal(from.get(colorAttribute), param);
    criteria.where(predicate);
    assertEquals(1, criteria.getParameters().size());
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(jakarta.persistence.EntityManager) ParameterExpression(jakarta.persistence.criteria.ParameterExpression) Predicate(jakarta.persistence.criteria.Predicate) Test(org.junit.Test)

Aggregations

ParameterExpression (jakarta.persistence.criteria.ParameterExpression)2 EntityManager (jakarta.persistence.EntityManager)1 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)1 Predicate (jakarta.persistence.criteria.Predicate)1 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1