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