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));
网友评论