Java 类javax.persistence.criteria.CriteriaDelete 实例源码

项目:adeptj-modules    文件:EclipseLinkCrudRepository.java   
/**
 * {@inheritDoc}
 */
@Override
public <T extends BaseEntity> int deleteByCriteria(DeleteCriteria<T> criteria) {
    EntityManager em = null;
    EntityTransaction txn = null;
    try {
        em = this.emf.createEntityManager();
        txn = JpaUtil.getTransaction(em);
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaDelete<T> cd = cb.createCriteriaDelete(criteria.getEntity());
        Root<T> root = cd.from(criteria.getEntity());
        int rowsDeleted = em
                .createQuery(cd.where(cb.and(JpaUtil.getPredicates(criteria.getCriteriaAttributes(), cb, root))))
                .executeUpdate();
        txn.commit();
        LOGGER.debug("deleteByCriteria: No. of rows deleted: [{}]", rowsDeleted);
        return rowsDeleted;
    } catch (RuntimeException ex) {
        JpaUtil.setRollbackOnly(txn);
        LOGGER.error(ex.getMessage(), ex);
        throw new PersistenceException(ex.getMessage(), ex);
    } finally {
        JpaUtil.rollbackTransaction(txn);
        JpaUtil.closeEntityManager(em);
    }
}
项目:HibernateTips    文件:TestCriteriaDelete.java   
@Test
public void deleteBooks() {
    log.info("... deleteBooks ...");

    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();

    logBooks(em);

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaDelete<Book> delete = cb.createCriteriaDelete(Book.class);
    delete.from(Book.class);

    Query query = em.createQuery(delete);
    query.executeUpdate();

    logBooks(em);

    em.getTransaction().commit();
    em.close();
}
项目:opencucina    文件:ClusterControlRepositoryImpl.java   
/**
 * JAVADOC Method Level Comments
 *
 * @param eventName JAVADOC.
 *
 * @return JAVADOC.
 */
@Override
public boolean deleteByEventName(String eventName) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaDelete<ClusterControl> cq = cb.createCriteriaDelete(ClusterControl.class);
    Root<ClusterControl> root = cq.from(ClusterControl.class);
    Predicate ep = cb.equal(root.get("event"), eventName);
    Predicate cp = cb.equal(root.get("complete"), false);

    int rows = entityManager.createQuery(cq.where(cb.and(ep, cp))).executeUpdate();

    if (LOG.isDebugEnabled()) {
        LOG.debug("Deleted " + rows + " for event " + eventName);
    }

    return rows > 0;
}
项目:VaadinUtils    文件:JpaDslAbstract.java   
/**
 * WARNING, order will not be honoured by this method
 * 
 * @return
 */
public int delete()
{
    Preconditions.checkArgument(orders.size() == 0, "Order is not supported for delete");
    CriteriaDelete<E> deleteCriteria = builder.createCriteriaDelete(entityClass);
    root = deleteCriteria.getRoot();
    if (predicate != null)
    {
        deleteCriteria.where(predicate);
    }
    Query query = getEntityManager().createQuery(deleteCriteria);

    if (limit != null)
    {
        query.setMaxResults(limit);
    }
    if (startPosition != null)
    {
        query.setFirstResult(startPosition);
    }
    int result = query.executeUpdate();
    getEntityManager().getEntityManagerFactory().getCache().evict(entityClass);

    return result;
}
项目:javaee7-developer-handbook    文件:EmployeeCriteriaDeleteTest.java   
@Test
public void shouldExecuteCriteriaDelete() throws Exception {
    assertNotNull(em);
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaDelete<Employee> c =
        builder.createCriteriaDelete(Employee.class);
    Root<Employee> p = c.from(Employee.class);
    Predicate condition1 = builder.ge(
        p.get(Employee_.salary),
            new BigDecimal("50000"));
    Predicate condition2 = builder.equal(
        p.get(Employee_.taxCode),
            new TaxCode(504, "Director"));
    c.where(condition1, condition2);
    utx.begin();
    Query query = em.createQuery(c);
    int rowsAffected = query.executeUpdate();
    assertTrue( rowsAffected > 0);
    utx.commit();
}
项目:tasfe-framework    文件:BaseRepositoryImpl.java   
@Override
public Integer deleteById(String id) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaDelete<T> delete = cb.createCriteriaDelete(getDomainClass());
    Root<T> e = delete.from(getDomainClass());
    delete.where(cb.equal(e.get("id"), id));
    return entityManager.createQuery(delete).executeUpdate();
}
项目:tasfe-framework    文件:BaseRepositoryImpl.java   
@Override
public Integer deleteByFieldEq(String Field, Object value) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaDelete<T> delete = cb.createCriteriaDelete(getDomainClass());
    Root<T> e = delete.from(getDomainClass());
    delete.where(cb.equal(e.get(Field), value));
    return entityManager.createQuery(delete).executeUpdate();
}
项目:tasfe-framework    文件:BaseRepositoryImpl.java   
@Override
public Integer deleteByFieldLike(String Field, String value) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaDelete<T> delete = cb.createCriteriaDelete(getDomainClass());
    Root<T> e = delete.from(getDomainClass());
    delete.where(cb.like(e.get(Field).as(String.class), value));
    return entityManager.createQuery(delete).executeUpdate();
}
项目:tasfe-framework    文件:BaseRepositoryImpl.java   
@Override
public Integer deleteByFieldIn(String Field, Iterable<?> values) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaDelete<T> delete = cb.createCriteriaDelete(getDomainClass());
    Root<T> e = delete.from(getDomainClass());
    delete.where(cb.in(e.get(Field)).value(values));
    return entityManager.createQuery(delete).executeUpdate();
}
项目:Pedidex    文件:JpaCriteriaHelper.java   
public void delete() {
    CriteriaDelete<T> criteriaDelete = criteriaBuilder.createCriteriaDelete(entityClass);

    Root<T> root = criteriaDelete.from(entityClass);

    if (!wheres.isEmpty()) {
        criteriaDelete.where( getPredicates(root, wheres) );
    }

    em.createQuery(criteriaDelete).executeUpdate();
}
项目:flow-platform    文件:AbstractBaseDao.java   
@Override
public int deleteAll() {
    return execute(session -> {
        CriteriaBuilder builder = session.getCriteriaBuilder();
        CriteriaDelete<T> delete = builder.createCriteriaDelete(getEntityClass());
        delete.from(getEntityClass());
        return session.createQuery(delete).executeUpdate();
    });
}
项目:crudlet    文件:CrudService.java   
/**
 * Deletes all entities which match the predicates provided.<p/>
 * 
 * <b>Performance note:</b> This implementation is not optimized as it is built from a dynamic query.
 * For production environments, implement this method explicitly by using named queries.
 */
public void deleteBy(Map<String, String> predicates) {
    CriteriaDelete<T> query = em.getCriteriaBuilder().createCriteriaDelete(getModelClass());
    Root<T> from = query.from(getModelClass());
    if (predicates != null) {
        query.where(createPredicates(em.getCriteriaBuilder(), from, predicates));
    }
    em.createQuery(query).executeUpdate();
    em.flush();
}
项目:mycore    文件:MCRMetadataHistoryCommands.java   
@MCRCommand(syntax = "clear metadata history of base {0}",
    help = "clears metadata history of all objects with base id {0}")
public static void clearHistory(String baseId) {
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaDelete<MCRMetaHistoryItem> delete = cb.createCriteriaDelete(MCRMetaHistoryItem.class);
    Root<MCRMetaHistoryItem> item = delete.from(MCRMetaHistoryItem.class);
    int rowsDeleted = em.createQuery(
        delete.where(
            cb.like(item.get(MCRMetaHistoryItem_.id).as(String.class), baseId + "_".replace("_", "$_") + '%', '$')))
        .executeUpdate();
    LogManager.getLogger().info("Deleted {} items in history of {}", rowsDeleted, baseId);
}
项目:mycore    文件:MCRMetadataHistoryCommands.java   
@MCRCommand(syntax = "clear metadata history of id {0}",
    help = "clears metadata history of object/derivate with id {0}")
public static void clearSingleHistory(String mcrId) {
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaDelete<MCRMetaHistoryItem> delete = cb.createCriteriaDelete(MCRMetaHistoryItem.class);
    Root<MCRMetaHistoryItem> item = delete.from(MCRMetaHistoryItem.class);
    int rowsDeleted = em.createQuery(
        delete.where(cb.equal(item.get(MCRMetaHistoryItem_.id).as(String.class), mcrId)))
        .executeUpdate();
    LogManager.getLogger().info("Deleted {} items in history of {}", rowsDeleted, mcrId);
}
项目:mojito    文件:CurrentVariantRollbackService.java   
/**
 * Builds the query to delete {@link com.box.l10n.mojito.entity.TMTextUnitCurrentVariant}s that will be rolled back
 *
 * @param tmId            ID of the TM the {@link TMTextUnitCurrentVariant}s to be rolled back should belong to
 * @param extraParameters Extra parameters to filter what to rollback
 * @return The delete query
 */
protected Query buildDeleteQuery(Long tmId, CurrentVariantRollbackParameters extraParameters) {

    logger.trace("Building the delete tmTextUnitCurrentVariants query");

    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaDelete<TMTextUnitCurrentVariant> deleteCriteria = criteriaBuilder.createCriteriaDelete(TMTextUnitCurrentVariant.class);
    Root<TMTextUnitCurrentVariant> root = deleteCriteria.from(TMTextUnitCurrentVariant.class);

    Predicate whereClause = criteriaBuilder.conjunction();

    Predicate tmPredicate = criteriaBuilder.equal(root.get(TMTextUnitCurrentVariant_.tm), tmId);
    whereClause = criteriaBuilder.and(whereClause, tmPredicate);

    List<Long> localeIdsToRollback = extraParameters.getLocaleIds();
    if (localeIdsToRollback != null && !localeIdsToRollback.isEmpty()) {
        Predicate localesPredicate = criteriaBuilder.isTrue(root.get(TMTextUnitCurrentVariant_.locale).in(localeIdsToRollback));
        whereClause = criteriaBuilder.and(whereClause, localesPredicate);
    }

    List<Long> tmTextUnitIdsToRollback = extraParameters.getTmTextUnitIds();
    if (tmTextUnitIdsToRollback != null && !tmTextUnitIdsToRollback.isEmpty()) {
        Predicate tmTextUnitPredicate = criteriaBuilder.isTrue(root.get(TMTextUnitCurrentVariant_.tmTextUnit).in(tmTextUnitIdsToRollback));
        whereClause = criteriaBuilder.and(whereClause, tmTextUnitPredicate);
    }

    deleteCriteria.where(whereClause);

    return entityManager.createQuery(deleteCriteria);
}
项目:jpasecurity    文件:DefaultSecureEntityManager.java   
public Query createQuery(CriteriaDelete criteria) {
    DefaultAccessManager.Instance.register(accessManager);
    FilterResult<CriteriaDelete> filterResult = entityFilter.filterQuery(criteria);
    if (filterResult.getQuery() == null) {
        return new EmptyResultQuery(super.createQuery(criteria));
    } else {
        return createQuery(super.createQuery(filterResult.getQuery()), filterResult);
    }
}
项目:jpasecurity    文件:CriteriaHolder.java   
private From<?, ?> getFrom(CommonAbstractCriteria query, Alias alias) {
    if (query instanceof CriteriaUpdate) {
        return ((CriteriaUpdate<?>)query).getRoot();
    } else if (query instanceof CriteriaDelete) {
        return ((CriteriaUpdate<?>)query).getRoot();
    } else {
        return getFrom((AbstractQuery<?>)query, alias);
    }
}
项目:springJpaKata    文件:JpaBulkTest.java   
@Test  // jpql em.createQuery("delete Address a where a.city like 'w%'").executeUpdate();
public void shouldBulkDeleteCriteria() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaDelete<Address> delete = cb.createCriteriaDelete(Address.class); //<5>
    Root<Address> from = delete.from(Address.class);
    delete.where(cb.like(from.get("city"), "w%"));
    int deleted = em.createQuery(delete).executeUpdate(); //<6>
    Assertions.assertThat(deleted).isEqualTo(6);
}
项目:JpaCriteriaWithLambdaExpressions    文件:AbstractDao.java   
/**
 * @param predicateBuilders Restricting query conditions. If you supply more than one predicate, they will be joined by conjunction.
 */
protected void deleteWhere(PredicateBuilder<T>... predicateBuilders) {
    CriteriaBuilder cb = em().getCriteriaBuilder();
    CriteriaDelete<T> delete = cb.createCriteriaDelete(entityClass);
    Root<T> root = delete.from(entityClass);

    if (predicateBuilders != null && predicateBuilders.length > 0) {
        delete.where(buildPredicates(cb, root, predicateBuilders));
    }
    em().createQuery(delete).executeUpdate();
}
项目:opencucina    文件:WorkflowRepositoryImpl.java   
/**
 *
 *
 * @param workflowId .
 */
@Override
@Transactional
public void delete(String workflowId) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaDelete<Workflow> cd = cb.createCriteriaDelete(Workflow.class);
    Root<Workflow> token = cd.from(Workflow.class);

    cd.where(cb.equal(token.get("workflowId"), workflowId));
    entityManager.createQuery(cd).executeUpdate();
}
项目:kc-rice    文件:NativeJpaQueryTranslator.java   
/**
    * {@inheritDoc}
    */
   @Override
public Query createDeletionQuery(Class queryClazz, TranslationContext criteria) {
       CriteriaDelete jpaQuery = entityManager.getCriteriaBuilder().createCriteriaDelete(queryClazz);

       if (!criteria.predicates.isEmpty()) {
           jpaQuery = jpaQuery.where(criteria.getCriteriaPredicate());
       }

       return entityManager.createQuery(jpaQuery);
   }
项目:disqus-synchronization    文件:JpaDisqusSynchronizationRepository.java   
@Override
public int delete(String commentId) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaDelete<DisqusComment> cd = cb.createCriteriaDelete(DisqusComment.class);
    Root<DisqusComment> disqusComment = cd.from(DisqusComment.class);
    cd.where(cb.equal(disqusComment.get(DisqusComment_.commentId), commentId));
    Query q = em.createQuery(cd);
    return q.executeUpdate();
}
项目:hibernate-semantic-query    文件:CriteriaInterpreter.java   
public static <E> SqmDeleteStatement interpretDeleteCriteria(CriteriaDelete<E> criteria, ParsingContext parsingContext) {
    if ( !JpaCriteriaDelete.class.isInstance( criteria ) ) {
        throw new IllegalArgumentException( "CriteriaDelete to interpret must implement SqmDeleteStatement" );
    }

    final CriteriaInterpreter interpreter = new CriteriaInterpreter( parsingContext );
    return interpreter.visitDeleteCriteria( (JpaCriteriaDelete<E>) criteria );
}
项目:sdcct    文件:SdcctCriteriaImpl.java   
@Nonnegative
@Override
@SuppressWarnings({ CompilerWarnings.UNCHECKED })
public long delete(EntityManager entityManager) {
    CriteriaDelete<T> criteriaQuery = ((CriteriaDelete<T>) this.criteriaBuilder.createCriteriaDelete(this.entityImplClass));

    return this.delete(entityManager, criteriaQuery, criteriaQuery.from(((Class<T>) this.entityImplClass)));
}
项目:sdcct    文件:SdcctCriteriaImpl.java   
@Nonnegative
@Override
@SuppressWarnings({ CompilerWarnings.UNCHECKED })
public <U> long delete(EntityManager entityManager, CriteriaDelete<U> criteriaQuery, Root<T> root) {
    Query query = this.buildQuery(entityManager, criteriaQuery, root, this.maxResults);

    if (query == null) {
        return 0L;
    }

    return query.executeUpdate();
}
项目:imcms    文件:DocRepository.java   
public void deleteFileDocContent(DocRef docIdentity) {
    List<FileDocFile> fileDocFile = fileDocFileRepository.findByDocIdAndVersionNo(docIdentity.getId(), docIdentity.getVersionNo());

    if (fileDocFile.size() == 0) {
        return;
    }

    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaDelete<FileDocFile> query = cb.createCriteriaDelete(FileDocFile.class);
    query.where(query.from(FileDocFile.class).get("id").in(fileDocFile.stream().map(FileDocFile::getId).collect(Collectors.toList())));

    entityManager.createQuery(query).executeUpdate();
}
项目:nestedj    文件:NestedNodeRemoverImpl.java   
private void performSingleDeletion(NestedNodeInfo<N> node, Class<N> nodeClass) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaDelete<N> delete = cb.createCriteriaDelete(nodeClass);
    Root<N> root = delete.from(nodeClass);
    delete.where(getPredicates(cb, root,
            cb.equal(root.<Long>get(id(nodeClass)), node.getId())
    ));
    em.createQuery(delete).executeUpdate();
}
项目:nestedj    文件:NestedNodeRemoverImpl.java   
private void performBatchDeletion(NestedNodeInfo<N> node, Class<N> nodeClass) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaDelete<N> delete = cb.createCriteriaDelete(nodeClass);
    Root<N> root = delete.from(nodeClass);
    delete.where(getPredicates(cb, root,
            cb.greaterThanOrEqualTo(root.<Long>get(left(nodeClass)), node.getLeft()),
            cb.lessThanOrEqualTo(root.<Long>get(right(nodeClass)), node.getRight())
    ));

    em.createQuery(delete).executeUpdate();
}
项目:JavaIncrementalParser    文件:MovieBean.java   
public void deleteMovie() {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaDelete<Movie> deleteCriteria = builder.createCriteriaDelete(Movie.class);
    Root<Movie> updateRoot = deleteCriteria.from(Movie.class);
    deleteCriteria.where(builder.equal(updateRoot.get(Movie_.name), "The Matrix"));
    Query q = em.createQuery(deleteCriteria);
    q.executeUpdate();
    em.flush();
}
项目:batchers    文件:AbstractRepository.java   
public void deleteAll() {
    CriteriaBuilder criteriaBuilder = entityManagerFactory.getCriteriaBuilder();
    CriteriaDelete<E> criteriaDelete = criteriaBuilder.createCriteriaDelete(getEntityClass());

    criteriaDelete.from(getEntityClass());

    entityManager.createQuery(criteriaDelete).executeUpdate();
}
项目:rice    文件:NativeJpaQueryTranslator.java   
/**
    * {@inheritDoc}
    */
   @Override
public Query createDeletionQuery(Class queryClazz, TranslationContext criteria) {
       CriteriaDelete jpaQuery = entityManager.getCriteriaBuilder().createCriteriaDelete(queryClazz);

       if (!criteria.predicates.isEmpty()) {
           jpaQuery = jpaQuery.where(criteria.getCriteriaPredicate());
       }

       return entityManager.createQuery(jpaQuery);
   }
项目:ee7-sandbox    文件:PostsBean.java   
public void delete() {
    final List<Long> checkedList = getCheckedList();
    for (Long id : checkedList) {
        checked.remove(id);
    }

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaDelete<Post> q = cb.createCriteriaDelete(Post.class);
    Root<Post> root = q.from(Post.class);
    q.where(root.get("id").in(checkedList));

    int result = em.createQuery(q).executeUpdate();
    log.info("delete @" + result);
    load();
}
项目:ee7-sandbox    文件:PostsBean.java   
public void delete() {
    final List<Long> checkedList = getCheckedList();
    for (Long id : checkedList) {
        checked.remove(id);
    }

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaDelete<Post> q = cb.createCriteriaDelete(Post.class);
    Root<Post> root = q.from(Post.class);
    q.where(root.get("id").in(checkedList));

    int result = em.createQuery(q).executeUpdate();
    log.info("delete @" + result);
    load();
}
项目:ee7-sandbox    文件:PostsBean.java   
public void delete() {
    final List<Long> checkedList = getCheckedList();
    for (Long id : checkedList) {
        checked.remove(id);
    }

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaDelete<Post> q = cb.createCriteriaDelete(Post.class);
    Root<Post> root = q.from(Post.class);
    q.where(root.get("id").in(checkedList));

    int result = em.createQuery(q).executeUpdate();
    log.info("delete @" + result);
    load();
}
项目:ee7-sandbox    文件:PostsBean.java   
public void delete() {
    final List<Long> checkedList = getCheckedList();
    for (Long id : checkedList) {
        checked.remove(id);
    }

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaDelete<Post> q = cb.createCriteriaDelete(Post.class);
    Root<Post> root = q.from(Post.class);
    q.where(root.get("id").in(checkedList));

    int result = em.createQuery(q).executeUpdate();
    log.info("delete @" + result);
    load();
}
项目:ee7-sandbox    文件:PostsBean.java   
public void delete() {
    final List<Long> checkedList = getCheckedList();
    for (Long id : checkedList) {
        checked.remove(id);
    }

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaDelete<Post> q = cb.createCriteriaDelete(Post.class);
    Root<Post> root = q.from(Post.class);
    q.where(root.get("id").in(checkedList));

    int result = em.createQuery(q).executeUpdate();
    log.info("delete @" + result);
    load();
}
项目:tomee    文件:JtaEntityManager.java   
@Override
public Query createQuery(final CriteriaDelete deleteQuery) {
    final Timer timer = Op.createQuery.start(this.timer, this);
    try {
        return getEntityManager().createQuery(deleteQuery);
    } finally {
        timer.stop();
    }
}
项目:VaadinUtils    文件:EntityManagerWrapper.java   
@Override
public Query createQuery(@SuppressWarnings("rawtypes") CriteriaDelete deleteQuery)
{
    logger.error("Thread: {} using entity Manager {}", Thread.currentThread().getId(), emid);

    return em.createQuery(deleteQuery);
}
项目:activejpa    文件:Filter.java   
/**
 * Constructs the delete criteria query
 * 
 * @param builder
 * @param query
 * @param root
 */
public <T extends Model> void constructQuery(CriteriaBuilder builder, CriteriaDelete<?> query, Root<T> root) {
    if (conditions != null || !conditions.isEmpty()) {
        List<Predicate> predicates = new ArrayList<Predicate>();
        for (Condition condition : conditions) {
            predicates.add(condition.constructQuery(builder, root));
        }
        query.where(predicates.toArray(new Predicate[0]));
    }
}
项目:activejpa    文件:Model.java   
protected static <T extends Model> void deleteAll(final Class<T> clazz, final Filter filter) {
    execute(manager -> {
        CriteriaBuilder builder = manager.getCriteriaBuilder();
        CriteriaDelete<T> deleteQuery = builder.createCriteriaDelete(clazz);
        Root<T> root = deleteQuery.from(clazz);
        filter.constructQuery(builder, deleteQuery, root);
        Query query = createQuery(deleteQuery, filter);
        return query.executeUpdate();
    }, false);
}