use of javax.persistence.criteria.CriteriaBuilder 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.CriteriaBuilder 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.CriteriaBuilder in project aries by apache.
the class AbstractSimpleTransactionTest method testRequiredInheritsTx.
@Test
public void testRequiredInheritsTx() {
Message message = new Message();
message.message = "Hello World!";
Message message2 = new Message();
message2.message = "Hello Nested World!";
txControl.required(() -> {
em.persist(message);
txControl.required(() -> {
em.persist(message2);
return null;
});
return null;
});
List<Message> results = txControl.notSupported(() -> {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Message> query = cb.createQuery(Message.class);
query.orderBy(cb.asc(query.from(Message.class).get("message")));
return em.createQuery(query).getResultList();
});
System.out.println(results);
assertEquals(2, results.size());
assertEquals("Hello Nested World!", results.get(0).message);
assertEquals("Hello World!", results.get(1).message);
}
use of javax.persistence.criteria.CriteriaBuilder in project aries by apache.
the class AbstractSimpleTransactionTest method testNestedTxInnerRollback.
@Test
public void testNestedTxInnerRollback() {
Message message = new Message();
message.message = "Hello World!";
Message message2 = new Message();
message2.message = "Hello Nested World!";
txControl.required(() -> {
// This will end up in the database
em.persist(message);
// This nested transaction will not commit
txControl.requiresNew(() -> {
em.persist(message2);
txControl.setRollbackOnly();
return null;
});
return null;
});
List<Message> results = txControl.notSupported(() -> {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Message> query = cb.createQuery(Message.class);
query.orderBy(cb.asc(query.from(Message.class).get("message")));
return em.createQuery(query).getResultList();
});
System.out.println(results);
assertEquals(1, results.size());
assertEquals("Hello World!", results.get(0).message);
}
use of javax.persistence.criteria.CriteriaBuilder in project deltaspike by apache.
the class QueryCriteria method createQuery.
@Override
public TypedQuery<R> createQuery() {
try {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<R> query = createCriteriaQuery(builder);
From<C, C> root = query.from(entityClass);
if (selections.size() == 1) {
Selection<?>[] selections = prepareSelections(query, builder, root);
query.select((Selection<? extends R>) selections[0]);
}
if (selections.size() > 1) {
query.multiselect(prepareSelections(query, builder, root));
}
List<Predicate> predicates = predicates(builder, root);
query.distinct(distinct);
if (!predicates.isEmpty()) {
query.where(predicates.toArray(new Predicate[predicates.size()]));
}
applyProcessors(query, builder, root);
return (TypedQuery<R>) entityManager.createQuery(query);
} catch (RuntimeException e) {
log.log(Level.SEVERE, "Exception while creating JPA query", e);
throw e;
}
}
Aggregations