Search in sources :

Example 46 with CriteriaBuilder

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

the class AbstractPathImplTest method testTypeExpression.

@Test
public void testTypeExpression() {
    EntityManager em = getOrCreateEntityManager();
    try {
        CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
        CriteriaQuery<Thing> criteria = criteriaBuilder.createQuery(Thing.class);
        Root<Thing> thingRoot = criteria.from(Thing.class);
        criteria.select(thingRoot);
        assertEquals(em.createQuery(criteria).getResultList().size(), 3);
        criteria.where(criteriaBuilder.equal(thingRoot.type(), criteriaBuilder.literal(Thing.class)));
        assertEquals(em.createQuery(criteria).getResultList().size(), 2);
    } finally {
        em.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Thing(org.hibernate.jpa.test.metamodel.Thing) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 47 with CriteriaBuilder

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

the class FetchAndJoinTest method testImplicitJoinFromExplicitCollectionJoin.

@Test
public void testImplicitJoinFromExplicitCollectionJoin() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    final CriteriaBuilder builder = em.getCriteriaBuilder();
    final CriteriaQuery<Entity1> criteria = builder.createQuery(Entity1.class);
    final Root<Entity1> root = criteria.from(Entity1.class);
    // illegal with fetch join
    final Join<Entity1, Entity2> entity2Join = root.join(Entity1_.entity2, JoinType.INNER);
    // <=== REMOVE
    final Fetch<Entity1, Entity2> entity2Fetch = root.fetch(Entity1_.entity2, JoinType.INNER);
    // <=== REMOVE
    entity2Fetch.fetch(Entity2_.entity3);
    criteria.where(builder.equal(root.get(Entity1_.value), "test"), // illegal with fetch join
    builder.equal(entity2Join.get(Entity2_.value), "test"));
    em.createQuery(criteria).getResultList();
    em.getTransaction().commit();
    em.close();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Entity2(org.hibernate.jpa.test.metamodel.Entity2) Entity1(org.hibernate.jpa.test.metamodel.Entity1) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 48 with CriteriaBuilder

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

the class ImplicitJoinTest method testImplicitJoinFromExplicitCollectionJoin.

@Test
public void testImplicitJoinFromExplicitCollectionJoin() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
    CriteriaQuery<Order> criteria = criteriaBuilder.createQuery(Order.class);
    Root<Order> orderRoot = criteria.from(Order.class);
    Join<Order, LineItem> lineItemsJoin = orderRoot.join(Order_.lineItems);
    criteria.where(criteriaBuilder.lt(lineItemsJoin.get(LineItem_.quantity), 2));
    criteria.select(orderRoot).distinct(true);
    TypedQuery<Order> query = em.createQuery(criteria);
    query.getResultList();
    em.getTransaction().commit();
    em.close();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(org.hibernate.jpa.test.metamodel.Order) EntityManager(javax.persistence.EntityManager) LineItem(org.hibernate.jpa.test.metamodel.LineItem) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 49 with CriteriaBuilder

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

the class SingularAttributeJoinTest method testEntityModeMapJoinCriteriaQuery.

@Test
public void testEntityModeMapJoinCriteriaQuery() throws Exception {
    final EntityManager entityManager = entityManagerFactory().createEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery criteriaQuery = criteriaBuilder.createQuery();
    javax.persistence.metamodel.EntityType distributionEntity = getEntityType("Distribution");
    From distributionFrom = criteriaQuery.from(distributionEntity);
    From policyJoin = distributionFrom.join("policy");
    Path policyId = policyJoin.get("policyId");
    criteriaQuery.select(policyId);
    TypedQuery typedQuery = entityManager.createQuery(criteriaQuery);
//        typedQuery.getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Path(javax.persistence.criteria.Path) EntityManager(javax.persistence.EntityManager) TypedQuery(javax.persistence.TypedQuery) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) From(javax.persistence.criteria.From) Test(org.junit.Test)

Example 50 with CriteriaBuilder

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

the class SelectCaseTest method simpleSelectCaseWithValuesShouldWork.

@Test
public void simpleSelectCaseWithValuesShouldWork() {
    EntityManager entityManager = getOrCreateEntityManager();
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaBuilder.SimpleCase<Integer, EnumValue> selectCase = cb.selectCase(cb.literal(1));
    selectCase.when(1, EnumValue.VALUE_1);
    selectCase.otherwise(EnumValue.VALUE_2);
    CriteriaQuery<Entity> query = cb.createQuery(Entity.class);
    Root<Entity> from = query.from(Entity.class);
    query.select(from).where(cb.equal(from.get("value"), selectCase));
    List<?> result = entityManager.createQuery(query).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test)

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