美文网首页
JPA CriteriaUpdate example

JPA CriteriaUpdate example

作者: 邮差在行动 | 来源:发表于2020-04-22 22:15 被阅读0次

    If you don't have EntityManager, you can do so by explicitly naming the EntityManager in the @PersistenceContext annotation.

    @PersistenceContext
    private EntityManager entityManager;
    

    Next, here is an example about CriteriaUpdate.

    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaUpdate<MyTest> update = criteriaBuilder.createCriteriaUpdate(MyTest.class);
    Root<MyTest> root = update.from(MyTest.class);
    Predicate idPredicate = criteriaBuilder.equal(root.get("id"), "id123");
    Predicate versionPredicate = criteriaBuilder.equal(root.get("version"), 0);
    Predicate namePredicate = criteriaBuilder.equal(root.get("name"), "old_name");
    update.set(root.get("name"), "new_name")
        .set(root.get("version"), 1)
        .where(namePredicate);//criteriaBuilder.and(idPredicate, versionPredicate));
    int ret = entityManager.createQuery(update).executeUpdate();
    System.out.println(String.format("改变了%s条数据", ret));
    

    相关文章

      网友评论

          本文标题:JPA CriteriaUpdate example

          本文链接:https://www.haomeiwen.com/subject/ciuwihtx.html