Example usage for javax.persistence.criteria CriteriaBuilder createCriteriaUpdate

List of usage examples for javax.persistence.criteria CriteriaBuilder createCriteriaUpdate

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaBuilder createCriteriaUpdate.

Prototype

<T> CriteriaUpdate<T> createCriteriaUpdate(Class<T> targetEntity);

Source Link

Document

Create a CriteriaUpdate query object to perform a bulk update operation.

Usage

From source file:com.toptal.dao.UserDaoImpl.java

@Transactional
@Override/* w w w . j  a va 2  s  .c  o m*/
public final User saveIgnoringPassword(final User user) {
    final CriteriaBuilder builder = this.manager.getCriteriaBuilder();
    final CriteriaUpdate<User> update = builder.createCriteriaUpdate(User.class);
    final Root<User> root = update.from(User.class);
    update.set(root.get(NAME_FIELD), user.getName());
    update.set(root.get("role"), user.getRole());
    update.where(builder.equal(root.get("id"), user.getId()));
    this.manager.createQuery(update).executeUpdate();
    return this.manager.find(User.class, user.getId());
}

From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java

/**
 * {@inheritDoc}/*w  w  w.  j ava  2 s.com*/
 */
@Override
public <T extends BaseEntity> int updateByCriteria(UpdateCriteria<T> criteria) {
    EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory());
    try {
        em.getTransaction().begin();
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaUpdate<T> cu = cb.createCriteriaUpdate(criteria.getEntity());
        criteria.getUpdateAttributes().forEach(cu::set);
        Root<T> root = cu.from(criteria.getEntity());
        int rowsUpdated = em
                .createQuery(cu.where(cb.and(Predicates.from(criteria.getCriteriaAttributes(), cb, root))))
                .executeUpdate();
        em.getTransaction().commit();
        LOGGER.debug("No. of rows updated: {}", rowsUpdated);
        return rowsUpdated;
    } catch (Exception ex) { // NOSONAR
        Transactions.markRollback(em);
        throw new JpaException(ex);
    } finally {
        Transactions.rollback(em);
        JpaUtil.closeEntityManager(em);
    }
}