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");
}
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);
}
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();
}
}
Aggregations