Search in sources :

Example 66 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.

the class MappedSuperclassComponentWithCollectionTest method testSizeExpressionForTheElementCollectionPropertyOfASubComponent.

@Test
public void testSizeExpressionForTheElementCollectionPropertyOfASubComponent() {
    TransactionUtil.doInJPA(this::entityManagerFactory, entityManager -> {
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Leader> query = builder.createQuery(Leader.class);
        Root<Leader> root = query.from(Leader.class);
        query.where(builder.equal(builder.size(root.get("information").get("infoContactDetail").get("phones")), 1));
        final List<Leader> results = entityManager.createQuery(query).getResultList();
        assertThat(results.size(), is(1));
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Test(org.junit.Test)

Example 67 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.

the class MappedSuperclassComponentWithCollectionTest method testSizeExpressionForTheOneToManyPropertyOfAComponent.

@Test
public void testSizeExpressionForTheOneToManyPropertyOfAComponent() {
    TransactionUtil.doInJPA(this::entityManagerFactory, entityManager -> {
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Manager> query = builder.createQuery(Manager.class);
        Root<Manager> root = query.from(Manager.class);
        query.where(builder.equal(builder.size(root.get("projects").get("previousProjects")), 2));
        final List<Manager> results = entityManager.createQuery(query).getResultList();
        assertThat(results.size(), is(1));
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Test(org.junit.Test)

Example 68 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.

the class LoadGraphFindByIdTest method createTypedQuery.

private TypedQuery<User> createTypedQuery(EntityManager em) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> cq = cb.createQuery(User.class);
    Root<User> root = cq.from(User.class);
    cq.where(cb.equal(root.get("id"), 1L));
    TypedQuery<User> tq = em.createQuery(cq);
    tq.setHint("javax.persistence.loadgraph", createEntityGraph(em));
    return tq;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder)

Example 69 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.

the class QueryLockingTest method testCriteriaWithPessimisticLock.

@Test
@TestForIssue(jiraKey = "HHH-11376")
@RequiresDialect(SQLServerDialect.class)
public void testCriteriaWithPessimisticLock() {
    TransactionUtil.doInJPA(this::entityManagerFactory, entityManager -> {
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Person> criteria = builder.createQuery(Person.class);
        Root<Person> personRoot = criteria.from(Person.class);
        ParameterExpression<Long> personIdParameter = builder.parameter(Long.class);
        personRoot.fetch("parent", JoinType.LEFT);
        criteria.select(personRoot).where(builder.equal(personRoot.get("id"), personIdParameter));
        final List<Person> resultList = entityManager.createQuery(criteria).setParameter(personIdParameter, 1L).setLockMode(LockModeType.PESSIMISTIC_WRITE).getResultList();
        resultList.isEmpty();
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue) RequiresDialect(org.hibernate.testing.RequiresDialect)

Example 70 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder in project metacat by Netflix.

the class IdEntityDaoImpl method get.

@Override
public List<T> get(final Iterable<Long> ids) {
    final EntityManager entityManager = em.get();
    final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    final CriteriaQuery<T> criteriaQuery = cb.createQuery(getEntityClass());
    final Root<T> root = criteriaQuery.from(getEntityClass());
    criteriaQuery.where(root.get("id").in(Lists.newArrayList(ids)));
    return entityManager.createQuery(criteriaQuery).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager)

Aggregations

CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)168 Test (org.junit.Test)121 EntityManager (javax.persistence.EntityManager)79 TestForIssue (org.hibernate.testing.TestForIssue)38 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)30 Customer (org.hibernate.jpa.test.metamodel.Customer)21 Predicate (javax.persistence.criteria.Predicate)15 Tuple (javax.persistence.Tuple)13 Person (org.hibernate.userguide.model.Person)12 List (java.util.List)11 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)11 ArrayList (java.util.ArrayList)10 Order (org.hibernate.jpa.test.metamodel.Order)10 ExecutionException (java.util.concurrent.ExecutionException)9 Message (org.apache.aries.tx.control.itests.entity.Message)6 TypedQuery (javax.persistence.TypedQuery)5 Root (javax.persistence.criteria.Root)4 Query (javax.persistence.Query)3 SkipForDialect (org.hibernate.testing.SkipForDialect)3 HashSet (java.util.HashSet)2