Search in sources :

Example 71 with CriteriaBuilder

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
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) List(java.util.List) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Predicate(javax.persistence.criteria.Predicate) Map(java.util.Map) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(javax.persistence.criteria.Order) SortType(com.robo4j.db.sql.support.SortType) EntityManager(javax.persistence.EntityManager) Collectors(java.util.stream.Collectors) TypedQuery(javax.persistence.TypedQuery) DataSourceContext(com.robo4j.db.sql.support.DataSourceContext) Root(javax.persistence.criteria.Root) EntityManager(javax.persistence.EntityManager) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Predicate(javax.persistence.criteria.Predicate)

Example 72 with CriteriaBuilder

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();
    }));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) TransactionRolledBackException(org.osgi.service.transaction.control.TransactionRolledBackException) Test(org.junit.Test)

Example 73 with CriteriaBuilder

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);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Message(org.apache.aries.tx.control.itests.entity.Message) Test(org.junit.Test)

Example 74 with CriteriaBuilder

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);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Message(org.apache.aries.tx.control.itests.entity.Message) Test(org.junit.Test)

Example 75 with CriteriaBuilder

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;
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) TypedQuery(javax.persistence.TypedQuery) Selection(javax.persistence.criteria.Selection) QuerySelection(org.apache.deltaspike.data.api.criteria.QuerySelection) Predicate(javax.persistence.criteria.Predicate)

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