/** * {@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); } }
@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(); }
/** * 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; }
/** * 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; }
@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(); }
@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(); }
@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(); }
@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(); }
@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(); }
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(); }
@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(); }); }
/** * 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(); }
@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); }
@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); }
/** * 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); }
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); } }
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); } }
@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); }
/** * @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(); }
/** * * * @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(); }
/** * {@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); }
@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(); }
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 ); }
@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))); }
@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(); }
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(); }
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(); }
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(); }
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(); }
public void deleteAll() { CriteriaBuilder criteriaBuilder = entityManagerFactory.getCriteriaBuilder(); CriteriaDelete<E> criteriaDelete = criteriaBuilder.createCriteriaDelete(getEntityClass()); criteriaDelete.from(getEntityClass()); entityManager.createQuery(criteriaDelete).executeUpdate(); }
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(); }
@Override public Query createQuery(final CriteriaDelete deleteQuery) { final Timer timer = Op.createQuery.start(this.timer, this); try { return getEntityManager().createQuery(deleteQuery); } finally { timer.stop(); } }
@Override public Query createQuery(@SuppressWarnings("rawtypes") CriteriaDelete deleteQuery) { logger.error("Thread: {} using entity Manager {}", Thread.currentThread().getId(), emid); return em.createQuery(deleteQuery); }
/** * 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])); } }
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); }