use of javax.persistence.criteria.CriteriaQuery 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.CriteriaQuery in project robo4j by Robo4J.
the class DefaultRepository method findByFields.
@SuppressWarnings("unchecked")
@Override
public <T> List<T> findByFields(Class<T> clazz, Map<String, Object> map, int limit, SortType sort) {
EntityManager em = dataSourceContext.getEntityManager(clazz);
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(clazz);
Root<T> rs = cq.from(clazz);
List<Predicate> predicates = map.entrySet().stream().map(e -> cb.equal(rs.get(e.getKey()), e.getValue())).collect(Collectors.toList());
CriteriaQuery<T> cq2 = cq.where(predicates.toArray(new Predicate[predicates.size()])).orderBy(getOrderById(cb, rs, sort)).select(rs);
TypedQuery<T> tq = em.createQuery(cq2);
//@formatter:off
return tq.setMaxResults(limit).getResultList();
//@formatter:on
}
use of javax.persistence.criteria.CriteriaQuery in project aries by apache.
the class XAJPATransactionTest method testTwoPhaseRollback.
@Test
public void testTwoPhaseRollback() throws Exception {
Object m1 = getMessageEntityFrom(XA_TEST_UNIT_1);
Object m2 = getMessageEntityFrom(XA_TEST_UNIT_2);
Object m3 = getMessageEntityFrom(XA_TEST_UNIT_2);
try {
txControl.required(() -> {
setMessage(m1, "Hello World!");
em1.persist(m1);
setMessage(m2, "Hello 1!");
em2.persist(m2);
txControl.requiresNew(() -> {
setMessage(m3, "Hello 2!");
em2.persist(m3);
return null;
});
txControl.getCurrentContext().registerXAResource(new PoisonResource(), null);
return null;
});
fail("Should roll back");
} catch (TransactionRolledBackException trbe) {
}
assertEquals(0, (int) txControl.notSupported(() -> {
CriteriaBuilder cb = em1.getCriteriaBuilder();
CriteriaQuery<Long> countQuery = cb.createQuery(Long.class);
countQuery.select(cb.count(countQuery.from(m1.getClass())));
return em1.createQuery(countQuery).getSingleResult().intValue();
}));
assertEquals(Arrays.asList("Hello 2!"), txControl.notSupported(() -> {
CriteriaBuilder cb = em2.getCriteriaBuilder();
CriteriaQuery<String> query = cb.createQuery(String.class);
query.select(query.from(m2.getClass()).get("message"));
return em2.createQuery(query).getResultList();
}));
}
use of javax.persistence.criteria.CriteriaQuery in project hibernate-orm by hibernate.
the class TupleCriteriaTest method testIllegalArgumentExceptionBuildingSelectArrayWithSameAliases.
@Test
public void testIllegalArgumentExceptionBuildingSelectArrayWithSameAliases() {
EntityManager em = entityManagerFactory().createEntityManager();
em.getTransaction().begin();
final CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery criteria = builder.createQuery();
Root<Customer> customerRoot = criteria.from(Customer.class);
Path<String> namePath = customerRoot.get(Customer_.name);
Path<Integer> agePath = customerRoot.get(Customer_.age);
try {
CompoundSelection<Object[]> c = builder.array(namePath.alias("SAME"), agePath.alias("SAME"));
criteria.select(c);
fail("Attempt to define multi-select with same aliases should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
em.getTransaction().commit();
em.close();
}
use of javax.persistence.criteria.CriteriaQuery in project hibernate-orm by hibernate.
the class ConcatTest method testConcat.
@Test
public void testConcat() throws Exception {
EntityManager entityManager = getOrCreateEntityManager();
entityManager.getTransaction().begin();
try {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery query = cb.createQuery();
Root<TestEntity> testEntity = query.from(TestEntity.class);
query.select(testEntity).where(cb.equal(testEntity.get("name"), cb.concat("test", cb.literal("_1"))));
final List results = entityManager.createQuery(query).getResultList();
entityManager.getTransaction().commit();
assertThat(results.size(), is(1));
} catch (Exception e) {
if (entityManager.getTransaction().isActive()) {
entityManager.getTransaction().rollback();
}
throw e;
} finally {
entityManager.close();
}
}
Aggregations