当前位置: 首页>>代码示例>>Java>>正文


Java CriteriaUpdate类代码示例

本文整理汇总了Java中javax.persistence.criteria.CriteriaUpdate的典型用法代码示例。如果您正苦于以下问题:Java CriteriaUpdate类的具体用法?Java CriteriaUpdate怎么用?Java CriteriaUpdate使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


CriteriaUpdate类属于javax.persistence.criteria包,在下文中一共展示了CriteriaUpdate类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: softDelete

import javax.persistence.criteria.CriteriaUpdate; //导入依赖的package包/类
private void softDelete(T entity, LocalDateTime localDateTime) {
	Assert.notNull(entity, "The entity must not be null!");

	CriteriaBuilder cb = em.getCriteriaBuilder();

	CriteriaUpdate<T> update = cb.createCriteriaUpdate((Class<T>) domainClass);

	Root<T> root = update.from((Class<T>) domainClass);

	update.set(DELETED_FIELD, localDateTime);

	final List<Predicate> predicates = new ArrayList<Predicate>();

	if (entityInformation.hasCompositeId()) {
		for (String s : entityInformation.getIdAttributeNames())
			predicates.add(cb.equal(root.<ID>get(s),
					entityInformation.getCompositeIdAttributeValue(entityInformation.getId(entity), s)));
		update.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
	} else
		update.where(cb.equal(root.<ID>get(entityInformation.getIdAttribute().getName()),
				entityInformation.getId(entity)));

	em.createQuery(update).executeUpdate();
}
 
开发者ID:dzinot,项目名称:spring-boot-jpa-data-rest-soft-delete,代码行数:25,代码来源:SoftDeletesRepositoryImpl.java

示例2: performMove

import javax.persistence.criteria.CriteriaUpdate; //导入依赖的package包/类
private void performMove(Sign nodeSign, Long nodeDelta, List nodeIds, Long levelModificator, Class<N> nodeClass) {
    if (!nodeIds.isEmpty()) {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaUpdate<N> update = cb.createCriteriaUpdate(nodeClass);
        Root<N> root = update.from(nodeClass);

        update.set(root.<Long>get(level(nodeClass)), cb.sum(root.<Long>get(level(nodeClass)), levelModificator));
        if (Sign.MINUS.equals(nodeSign)) {
            update.set(root.<Long>get(right(nodeClass)), cb.diff(root.<Long>get(right(nodeClass)), nodeDelta));
            update.set(root.<Long>get(left(nodeClass)), cb.diff(root.<Long>get(left(nodeClass)), nodeDelta));
        } else if (Sign.PLUS.equals(nodeSign)) {
            update.set(root.<Long>get(right(nodeClass)), cb.sum(root.<Long>get(right(nodeClass)), nodeDelta));
            update.set(root.<Long>get(left(nodeClass)), cb.sum(root.<Long>get(left(nodeClass)), nodeDelta));
        }
        update.where(getPredicates(cb, root, root.get(id(nodeClass)).in(nodeIds)));

        em.createQuery(update).executeUpdate();
    }
}
 
开发者ID:eXsio,项目名称:nestedj,代码行数:20,代码来源:NestedNodeMoverImpl.java

示例3: updateFields

import javax.persistence.criteria.CriteriaUpdate; //导入依赖的package包/类
private void updateFields(Sign sign, Long delta, Long start, Long stop, Class<N> nodeClass, String field) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaUpdate<N> update = cb.createCriteriaUpdate(nodeClass);
    Root<N> root = update.from(nodeClass);

    if (Sign.MINUS.equals(sign)) {
        update.set(root.<Long>get(field), cb.diff(root.<Long>get(field), delta));
    } else if (Sign.PLUS.equals(sign)) {
        update.set(root.<Long>get(field), cb.sum(root.<Long>get(field), delta));
    }
    update.where(getPredicates(cb, root,
            cb.greaterThan(root.<Long>get(field), start),
            cb.lessThan(root.<Long>get(field), stop)
    ));

    em.createQuery(update).executeUpdate();
}
 
开发者ID:eXsio,项目名称:nestedj,代码行数:18,代码来源:NestedNodeMoverImpl.java

示例4: update

import javax.persistence.criteria.CriteriaUpdate; //导入依赖的package包/类
public void update() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaUpdate<Post> q = cb.createCriteriaUpdate(Post.class);
    Root<Post> root = q.from(Post.class);
    q.set(root.get("approved"), true)
            .where(root.get("id").in(getCheckedList()));

    int result = em.createQuery(q).executeUpdate();
    log.info("update @" + result);
    load();
}
 
开发者ID:hantsy,项目名称:ee7-sandbox,代码行数:12,代码来源:PostsBean.java

示例5: update

import javax.persistence.criteria.CriteriaUpdate; //导入依赖的package包/类
/**
 * WARNING, order will not be honoured by this method
 * 
 * @param attribute
 * @param value
 * 
 * @return
 */
public <F> int update(SingularAttribute<E, F> attribute, F value)
{
	Preconditions.checkArgument(orders.size() == 0, "Order is not supported for delete");
	CriteriaUpdate<E> updateCriteria = builder.createCriteriaUpdate(entityClass);
	root = updateCriteria.getRoot();
	if (predicate != null)
	{
		updateCriteria.where(predicate);
		updateCriteria.set(attribute, value);
	}
	Query query = getEntityManager().createQuery(updateCriteria);

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

	return result;
}
 
开发者ID:rlsutton1,项目名称:VaadinUtils,代码行数:34,代码来源:JpaDslAbstract.java

示例6: shouldExecuteCriteriaUpdate

import javax.persistence.criteria.CriteriaUpdate; //导入依赖的package包/类
@Test
public void shouldExecuteCriteriaUpdate() throws Exception {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaUpdate<Employee> c =
        builder.createCriteriaUpdate(Employee.class);
    Root<Employee> p = c.from(Employee.class);
    Predicate condition = builder.ge(
        p.get(Employee_.salary), new BigDecimal("50000"));
    c.where(condition);
    TaxCode directorTaxCode = new TaxCode(504, "Director");
    c.set(p.get(Employee_.taxCode), directorTaxCode);

    utx.begin();
    Query query = em.createQuery(c);
    int rowsAffected = query.executeUpdate();
    assertTrue( rowsAffected > 0 );
    utx.commit();
}
 
开发者ID:peterpilgrim,项目名称:javaee7-developer-handbook,代码行数:19,代码来源:EmployeeCriteriaUpdateTest.java

示例7: updateTask

import javax.persistence.criteria.CriteriaUpdate; //导入依赖的package包/类
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.where(lExpression);
       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);
       }
	
}
 
开发者ID:Julien35,项目名称:dev-courses,代码行数:27,代码来源:ToDoListDAO.java

示例8: batchUpdateStatus

import javax.persistence.criteria.CriteriaUpdate; //导入依赖的package包/类
@Override
public int batchUpdateStatus(String zone, AgentStatus status, Set<String> agents, boolean isNot) {
    return execute(session -> {
        CriteriaBuilder builder = session.getCriteriaBuilder();
        CriteriaUpdate<Agent> criteria = builder.createCriteriaUpdate(Agent.class);

        Root<Agent> from = criteria.from(Agent.class);
        criteria.set(from.get("status"), status);

        Predicate whereClause = builder.equal(from.get("path").get("zone"), zone);

        if (agents == null || agents.size() == 0) {
            return session.createQuery(criteria).executeUpdate();
        }

        // set agents to where clause
        Predicate inAgents;
        if (isNot) {
            inAgents = builder.not(from.get("path").get("name").in(agents));
        } else {
            inAgents = from.get("path").get("name").in(agents);
        }

        criteria.where(builder.and(whereClause, inAgents));
        return session.createQuery(criteria).executeUpdate();
    });
}
 
开发者ID:FlowCI,项目名称:flow-platform,代码行数:28,代码来源:AgentDaoImpl.java

示例9: updateNotNullOrEmpty

import javax.persistence.criteria.CriteriaUpdate; //导入依赖的package包/类
@Override
public int updateNotNullOrEmpty(final CmdResult obj) {
    final Map<Field, Object> notNullFields =
        ObjectUtil.findNotNullFieldValue(getEntityClass(),
            obj,
            Sets.newHashSet(Process.class),
            Sets.newHashSet("cmdId"),
            true);

    // all fields are null
    if (notNullFields.isEmpty()) {
        return 0;
    }

    return execute(session -> {
        CriteriaBuilder builder = session.getCriteriaBuilder();
        CriteriaUpdate<CmdResult> update = builder.createCriteriaUpdate(CmdResult.class);
        Root<CmdResult> from = update.from(CmdResult.class);

        for (Map.Entry<Field, Object> entry : notNullFields.entrySet()) {
            Field field = entry.getKey();
            Object value = entry.getValue();

            // for cmd result exception type, adaptor will get item of list.. maybe its bug of hibenrate
            if (value instanceof List) {
                value = BaseAdaptor.GSON.toJson(value);
            }

            update.set(field.getName(), value);
        }

        update.where(builder.equal(from.get(getKeyName()), obj.getCmdId()));
        return session.createQuery(update).executeUpdate();
    });
}
 
开发者ID:FlowCI,项目名称:flow-platform,代码行数:36,代码来源:CmdResultDaoImpl.java

示例10: update

import javax.persistence.criteria.CriteriaUpdate; //导入依赖的package包/类
@Override
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) {
        ReflectionUtils.makeAccessible(field);
        Object fieldV = ReflectionUtils.getField(field, entity);
        if (fieldV == null)
            continue;
        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();

}
 
开发者ID:zhaoqilong3031,项目名称:spring-cloud-samples,代码行数:25,代码来源:BaseRepositoryImpl.java

示例11: updateByCriteria

import javax.persistence.criteria.CriteriaUpdate; //导入依赖的package包/类
/**
 * {@inheritDoc}
 */
@Override
public <T extends BaseEntity> int updateByCriteria(UpdateCriteria<T> criteria) {
    EntityManager em = null;
    EntityTransaction txn = null;
    try {
        em = this.emf.createEntityManager();
        txn = JpaUtil.getTransaction(em);
        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(JpaUtil.getPredicates(criteria.getCriteriaAttributes(), cb, root))))
                .executeUpdate();
        txn.commit();
        LOGGER.debug("No. of rows updated: {}", rowsUpdated);
        return rowsUpdated;
    } catch (RuntimeException ex) {
        JpaUtil.setRollbackOnly(txn);
        LOGGER.error(ex.getMessage(), ex);
        throw new PersistenceException(ex.getMessage(), ex);
    } finally {
        JpaUtil.rollbackTransaction(txn);
        JpaUtil.closeEntityManager(em);
    }
}
 
开发者ID:AdeptJ,项目名称:adeptj-modules,代码行数:30,代码来源:EclipseLinkCrudRepository.java

示例12: updateBookPrices

import javax.persistence.criteria.CriteriaUpdate; //导入依赖的package包/类
@Test
public void updateBookPrices() {
	log.info("... updateBookPrices ...");

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

	logBookPrices(em);
	
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaUpdate<Book> update = cb.createCriteriaUpdate(Book.class);
	Root<Book> root = update.from(Book.class);
	update.set(Book_.price, cb.prod(root.get(Book_.price), 1.1));
	
	Query query = em.createQuery(update);
	query.executeUpdate();

	logBookPrices(em);
	
	em.getTransaction().commit();
	em.close();
}
 
开发者ID:thjanssen,项目名称:HibernateTips,代码行数:23,代码来源:TestCriteriaUpdate.java

示例13: createQuery

import javax.persistence.criteria.CriteriaUpdate; //导入依赖的package包/类
public Query createQuery(CriteriaUpdate criteria) {
    DefaultAccessManager.Instance.register(accessManager);
    FilterResult<CriteriaUpdate> filterResult = entityFilter.filterQuery(criteria);
    if (filterResult.getQuery() == null) {
        return new EmptyResultQuery(super.createQuery(criteria));
    } else {
        return createQuery(super.createQuery(filterResult.getQuery()), filterResult);
    }
}
 
开发者ID:ArneLimburg,项目名称:jpasecurity,代码行数:10,代码来源:DefaultSecureEntityManager.java

示例14: getFrom

import javax.persistence.criteria.CriteriaUpdate; //导入依赖的package包/类
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);
    }
}
 
开发者ID:ArneLimburg,项目名称:jpasecurity,代码行数:10,代码来源:CriteriaHolder.java

示例15: shouldBulkUpdateCriteria

import javax.persistence.criteria.CriteriaUpdate; //导入依赖的package包/类
@Test
public void shouldBulkUpdateCriteria() {
    CriteriaBuilder cb = em.getCriteriaBuilder(); 
    CriteriaUpdate<Address> update = cb.createCriteriaUpdate(Address.class); //<3>
    Root<Address> i = update.from(Address.class);
    update.set(i.get("disable"), true);
    update.where(cb.greaterThan(i.get("id"), 0l));
    int updatedEntities = em.createQuery(update).executeUpdate(); //<4>
    Assertions.assertThat(updatedEntities).isEqualTo(30);
    Assertions.assertThat(addressRepo.findByDisableTrue()).hasSize(30);
}
 
开发者ID:przodownikR1,项目名称:springJpaKata,代码行数:12,代码来源:JpaBulkTest.java


注:本文中的javax.persistence.criteria.CriteriaUpdate类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。