Java Code Examples for javax.persistence.criteria.CriteriaBuilder.createCriteriaUpdate()

The following are Jave code examples for showing how to use createCriteriaUpdate() of the javax.persistence.criteria.CriteriaBuilder class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: dev-courses   File:   Source Code and License Vote up 5 votes
public void updateTask(final ToDoList pToDoList) {
       final CriteriaBuilder lCriteriaBuilder = entityManager.getCriteriaBuilder();
       //Creation de la requête d'update
       final CriteriaUpdate<ToDoList> lCriteriaUpdate = lCriteriaBuilder.createCriteriaUpdate(ToDoList.class);
       final Root<ToDoList> lRoot = lCriteriaUpdate.from(ToDoList.class);
       final Path<ToDoList> lPath = lRoot.get("id");
       //On utilise la variable pToDoList transmise en parametre de la methode
       final Expression<Boolean> lExpression = lCriteriaBuilder.equal(lPath, pToDoList.getId());
       lCriteriaUpdate.set("libelle", pToDoList.getLibelle());
       final Query lQuery = entityManager.createQuery(lCriteriaUpdate);
       final int lRowCount = lQuery.executeUpdate();
       //Si la requête modifie un nombre d'occurrences différent de 1 > erreur
       //Sinon update fait.
       if (lRowCount != 1) {
           final org.hibernate.Query lHQuery = lQuery.unwrap(org.hibernate.Query.class);
           final String lSql = lHQuery.getQueryString();
           throw new RuntimeException("Nombre d'occurences (" + lRowCount + 
                   ") modifiés différent de 1 pour " + lSql);
Example 2
Project: spring-cloud-samples   File:   Source Code and License Vote up 5 votes
public int update(T entity) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaUpdate<T> criteriaUpdate = builder.createCriteriaUpdate(entityClass);
    Root<T> root = criteriaUpdate.from(entityClass);
    Field[] fields = entityClass.getDeclaredFields();
    Object primaryV = null;
    String primaryKey = this.getPrimaryKey();
    for (Field field : fields) {
        Object fieldV = ReflectionUtils.getField(field, entity);
        if (fieldV == null)
        if (primaryKey.equals(field.getName())) {// 主键不参与修改
            primaryV = fieldV;
        } else {
            criteriaUpdate.set(root.get(field.getName()), fieldV);
    criteriaUpdate.where(builder.equal(root.get(primaryKey), primaryV));
    Query query = entityManager.createQuery(criteriaUpdate);
    return query.executeUpdate();