Search in sources :

Example 51 with CriteriaBuilder

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

the class SelectCaseTest method selectCaseWithValuesShouldWork.

@Test
public void selectCaseWithValuesShouldWork() {
    EntityManager entityManager = getOrCreateEntityManager();
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaBuilder.Case<EnumValue> selectCase = cb.selectCase();
    Predicate somePredicate = cb.equal(cb.literal(1), 1);
    selectCase.when(somePredicate, 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));
    entityManager.createQuery(query).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Predicate(javax.persistence.criteria.Predicate) Test(org.junit.Test)

Example 52 with CriteriaBuilder

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

the class SelectCaseTest method simpleSelectCaseWithCastedTypeValuesShouldWork.

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

Example 53 with CriteriaBuilder

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

the class BasicSimpleCaseTest method testCaseStringResult.

@Test
@TestForIssue(jiraKey = "HHH-9343")
public void testCaseStringResult() {
    EntityManager em = getOrCreateEntityManager();
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Tuple> query = builder.createTupleQuery();
    Root<Customer> root = query.from(Customer.class);
    Path<String> emailPath = root.get("email");
    CriteriaBuilder.Case<String> selectCase = builder.selectCase();
    selectCase.when(builder.greaterThan(builder.length(emailPath), 13), "Long");
    selectCase.when(builder.greaterThan(builder.length(emailPath), 12), "Normal");
    Expression<String> emailType = selectCase.otherwise("Unknown");
    query.multiselect(emailPath, emailType);
    em.createQuery(query).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Tuple(javax.persistence.Tuple) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 54 with CriteriaBuilder

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

the class BasicSimpleCaseTest method testCaseLiteralResult.

@Test
@TestForIssue(jiraKey = "HHH-9343")
public void testCaseLiteralResult() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Boolean> cq = cb.createQuery(Boolean.class);
    Root<Customer> expense_ = cq.from(Customer.class);
    em.createQuery(cq.distinct(true).where(cb.equal(expense_.get("email"), "@hibernate.com")).multiselect(cb.selectCase().when(cb.gt(cb.count(expense_), cb.literal(0L)), cb.literal(true)).otherwise(cb.literal(false)))).getSingleResult();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 55 with CriteriaBuilder

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

the class BasicSimpleCaseTest method testCaseInOrderBy2.

@Test
public void testCaseInOrderBy2() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Customer> query = builder.createQuery(Customer.class);
    Root<Customer> root = query.from(Customer.class);
    query.select(root);
    Path<String> emailPath = root.get("email");
    SimpleCase<String, String> orderCase = builder.selectCase(emailPath);
    orderCase = orderCase.when("test@test.com", "a");
    orderCase = orderCase.when("test2@test.com", "b");
    query.orderBy(builder.asc(orderCase.otherwise("c")));
    em.createQuery(query);
}
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