Search in sources :

Example 36 with SingularAttribute

use of javax.persistence.metamodel.SingularAttribute in project pnc by project-ncl.

the class RSQLPredicateProducerTest method testCriteriaPredicateEmbeded.

@Test
public void testCriteriaPredicateEmbeded() {
    org.jboss.pnc.spi.datastore.repositories.api.Predicate<BuildRecord> criteriaPredicate = producer.getCriteriaPredicate(BuildRecord.class, "environment.name==fooEnv");
    CriteriaBuilder cb = mock(CriteriaBuilder.class);
    Root<BuildRecord> root = mock(Root.class);
    Join<BuildRecord, BuildEnvironment> join = mock(Join.class);
    SingularAttributePath<String> namePath = mock(SingularAttributePath.class);
    when(root.join(BuildRecord_.buildEnvironment)).thenReturn(join);
    when(join.get(BuildEnvironment_.name)).thenReturn(namePath);
    Mockito.doReturn(String.class).when(namePath).getJavaType();
    SingularAttribute pathAttribute = mock(SingularAttribute.class);
    java.lang.reflect.Member javaMember = mock(java.lang.reflect.Member.class);
    Mockito.doReturn(BuildEnvironment.class).when(javaMember).getDeclaringClass();
    Mockito.doReturn(javaMember).when(pathAttribute).getJavaMember();
    Mockito.doReturn("name").when(pathAttribute).getName();
    Mockito.doReturn(pathAttribute).when(namePath).getAttribute();
    criteriaPredicate.apply(root, null, cb);
    Mockito.verify(cb).equal(namePath, "fooEnv");
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) BuildRecord(org.jboss.pnc.model.BuildRecord) SingularAttribute(javax.persistence.metamodel.SingularAttribute) BuildEnvironment(org.jboss.pnc.model.BuildEnvironment) Test(org.junit.Test)

Example 37 with SingularAttribute

use of javax.persistence.metamodel.SingularAttribute in project pnc by project-ncl.

the class RSQLPredicateProducerTest method testCriteriaPredicate.

@Test
public void testCriteriaPredicate() {
    Base32LongID id = new Base32LongID(4L);
    org.jboss.pnc.spi.datastore.repositories.api.Predicate<BuildRecord> criteriaPredicate = producer.getCriteriaPredicate(BuildRecord.class, "id==" + id.getId());
    CriteriaBuilder cb = mock(CriteriaBuilder.class);
    Root<BuildRecord> root = mock(Root.class);
    SingularAttributePath<Base32LongID> idPath = mock(SingularAttributePath.class);
    when(root.get(BuildRecord_.id)).thenReturn(idPath);
    Mockito.doReturn(Base32LongID.class).when(idPath).getJavaType();
    SingularAttribute pathAttribute = mock(SingularAttribute.class);
    java.lang.reflect.Member javaMember = mock(java.lang.reflect.Member.class);
    Mockito.doReturn(BuildRecord.class).when(javaMember).getDeclaringClass();
    Mockito.doReturn(javaMember).when(pathAttribute).getJavaMember();
    Mockito.doReturn("id").when(pathAttribute).getName();
    Mockito.doReturn(pathAttribute).when(idPath).getAttribute();
    criteriaPredicate.apply(root, null, cb);
    Mockito.verify(cb).equal(idPath, id);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Base32LongID(org.jboss.pnc.model.Base32LongID) BuildRecord(org.jboss.pnc.model.BuildRecord) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Test(org.junit.Test)

Example 38 with SingularAttribute

use of javax.persistence.metamodel.SingularAttribute in project infinispan by infinispan.

the class JpaStore method deleteBatch.

@Override
public void deleteBatch(Iterable<Object> keys) {
    // If the iterable is empty, then we can return before creating the EntityManager. Empty collections are not
    // supported by all DB in CriteriaDelete
    List<Object> keyCollection = StreamSupport.stream(keys.spliterator(), false).collect(Collectors.toList());
    if (keyCollection.isEmpty())
        return;
    EntityManager em = emf.createEntityManager();
    try {
        EntityTransaction txn = em.getTransaction();
        long txnBegin = timeService.time();
        try {
            txn.begin();
            CriteriaBuilder cb = em.getCriteriaBuilder();
            CriteriaDelete query = cb.createCriteriaDelete(configuration.entityClass());
            Root root = query.from(configuration.entityClass());
            SingularAttribute id = getEntityId(em, configuration.entityClass());
            query.where(root.get(id).in(keyCollection));
            em.createQuery(query).executeUpdate();
            if (configuration.storeMetadata()) {
                List<MetadataEntityKey> metaKeys = keyCollection.stream().map(this::getMetadataKey).collect(Collectors.toList());
                CriteriaDelete<MetadataEntity> metaQuery = cb.createCriteriaDelete(MetadataEntity.class);
                Root<MetadataEntity> metaRoot = metaQuery.from(MetadataEntity.class);
                id = getEntityId(em, MetadataEntity.class);
                query.where(metaRoot.get(id).in(metaKeys));
                em.createQuery(metaQuery).executeUpdate();
            }
            txn.commit();
            stats.addBatchRemoveTxCommitted(timeService.time() - txnBegin);
        } catch (Exception e) {
            stats.addBatchRemoveTxFailed(timeService.time() - txnBegin);
            if (e instanceof JpaStoreException)
                throw e;
            throw new JpaStoreException("Exception caught in deleteBatch()", e);
        } finally {
            if (txn != null && txn.isActive())
                txn.rollback();
        }
    } finally {
        em.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityTransaction(javax.persistence.EntityTransaction) MetadataEntity(org.infinispan.persistence.jpa.impl.MetadataEntity) Root(javax.persistence.criteria.Root) EntityNotFoundException(javax.persistence.EntityNotFoundException) SingularAttribute(javax.persistence.metamodel.SingularAttribute) EntityManager(javax.persistence.EntityManager) CriteriaDelete(javax.persistence.criteria.CriteriaDelete) MetadataEntityKey(org.infinispan.persistence.jpa.impl.MetadataEntityKey)

Aggregations

SingularAttribute (javax.persistence.metamodel.SingularAttribute)38 Attribute (javax.persistence.metamodel.Attribute)13 Metamodel (javax.persistence.metamodel.Metamodel)13 ListAttribute (javax.persistence.metamodel.ListAttribute)8 ArrayList (java.util.ArrayList)7 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)7 MapAttribute (javax.persistence.metamodel.MapAttribute)7 Test (org.junit.Test)7 Predicate (javax.persistence.criteria.Predicate)5 EntityType (javax.persistence.metamodel.EntityType)5 Root (javax.persistence.criteria.Root)4 PluralAttribute (javax.persistence.metamodel.PluralAttribute)4 EntityManager (javax.persistence.EntityManager)3 Path (javax.persistence.criteria.Path)3 IdentifiableType (javax.persistence.metamodel.IdentifiableType)3 Type (javax.persistence.metamodel.Type)3 VersionedPerson (org.datanucleus.samples.annotations.versioned.VersionedPerson)3 EntityTransaction (javax.persistence.EntityTransaction)2 Query (javax.persistence.Query)2 TypedQuery (javax.persistence.TypedQuery)2