我尝试使用UpdateCriteria / Hibernate根据同一表的另一列的值更新表的列,但未成功。
我的实体看起来像这样:
@Entity
@Table(name="user_session")
public class UserSession{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "last_activity")
private Date lastActivity;
@Column(name = "session_start")
private Date sessionStart;
@Column(name = "session_end")
private Date sessionEnd;
}
我想构建一个与以下内容相对应的UpdateCriteria:
UPDATE user_session SET session_end = last_activity WHERE session_end IS NULL;
我尝试了以下代码的多个版本:
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaUpdate<UserSession> criteriaUpdate = builder.createCriteriaUpdate(UserSession.class);
Root<UserSession> root = criteriaUpdate.from(UserSession.class);
criteriaUpdate.set("sessionEnd", root.get("lastActivity"));
criteriaUpdate.where(root.get("sessionEnd").isNull());
session.createQuery(criteriaUpdate).executeUpdate();
谢谢