Search in sources :

Example 16 with CriteriaQuery

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Path(javax.persistence.criteria.Path) EntityManager(javax.persistence.EntityManager) TypedQuery(javax.persistence.TypedQuery) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) From(javax.persistence.criteria.From) Test(org.junit.Test)

Example 17 with CriteriaQuery

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
}
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 18 with CriteriaQuery

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

Example 19 with CriteriaQuery

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Customer(org.hibernate.jpa.test.metamodel.Customer) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 20 with CriteriaQuery

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();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) List(java.util.List) Test(org.junit.Test)

Aggregations

CriteriaQuery (javax.persistence.criteria.CriteriaQuery)47 EntityManager (javax.persistence.EntityManager)41 Query (javax.persistence.Query)36 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)9 Test (org.junit.Test)9 List (java.util.List)3 TypedQuery (javax.persistence.TypedQuery)3 Root (javax.persistence.criteria.Root)3 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)3 MwAssetTagCertificate (com.intel.mtwilson.as.data.MwAssetTagCertificate)2 MwCertificateX509 (com.intel.mtwilson.as.data.MwCertificateX509)2 MwKeystore (com.intel.mtwilson.as.data.MwKeystore)2 MwMleSource (com.intel.mtwilson.as.data.MwMleSource)2 TblEventType (com.intel.mtwilson.as.data.TblEventType)2 TblHostSpecificManifest (com.intel.mtwilson.as.data.TblHostSpecificManifest)2 TblHosts (com.intel.mtwilson.as.data.TblHosts)2 TblLocationPcr (com.intel.mtwilson.as.data.TblLocationPcr)2 TblMle (com.intel.mtwilson.as.data.TblMle)2 TblModuleManifest (com.intel.mtwilson.as.data.TblModuleManifest)2 TblModuleManifestLog (com.intel.mtwilson.as.data.TblModuleManifestLog)2