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();
}
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();
}
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();
}
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();
}
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);
}
Aggregations