Example usage for javax.persistence.criteria CriteriaUpdate from

List of usage examples for javax.persistence.criteria CriteriaUpdate from

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaUpdate from.

Prototype

Root<T> from(EntityType<T> entity);

Source Link

Document

Create and add a query root corresponding to the entity that is the target of the update.

Usage

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

@Transactional
@Override//from  w w  w .j a va2  s  . c om
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}//  ww w.  j  a v a 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);
    }
}