2016-12-19 54 views
1

我有一個實體誰有boolean財產。如何使用CriteriaUpdate設置true?

class MyEntity { 
    @Basic(optional = false) 
    @Column(name = "FLAG") 
    private boolean flag; 
} 

我怎麼能執行批量更新設置flagtrue

// UPDATE MyEntity AS e SET e.flag = TRUE 
final CriteriaBuilder builder = entityManager.getCriteriaBuilder(); 
final CriteriaUpdate<MyEntity> update 
    = builder.createCriteriaUpdate(MyEntity.class); 
final Root<MyEntity> root = criteriaUpdate.from(MyEntity.class); 
update.set(root.get(ProfileClue_.flag), ???); 

回答

1

使用純JPA我會做類似

final CriteriaBuilder builder = entityManager.getCriteriaBuilder(); 
final CriteriaUpdate<MyEntity> update = builder.createCriteriaUpdate(MyEntity.class); 
final Root<MyEntity> root = criteriaUpdate.from(MyEntity.class); 
Expression trueLiteral = builder.literal(Boolean.TRUE); 
update.set(root.get(ProfileClue_.flag), trueLiteral); 

使用支持查詢比JPA規範任務的其他部分參數更先進的JPA提供商(通過DataNucleus將JPA支持,也許其他),你可以把它作爲一個參數。

final CriteriaBuilder builder = entityManager.getCriteriaBuilder(); 
final CriteriaUpdate<MyEntity> update = builder.createCriteriaUpdate(MyEntity.class); 
final Root<MyEntity> root = criteriaUpdate.from(MyEntity.class); 
ParameterExpression<String> param = builder.parameter(Boolean.class, "myParam"); 
update.set(root.get(ProfileClue_.flag), param); 
相關問題