【发布时间】:2015-07-30 14:17:46
【问题描述】:
我有一个 Play 项目,我在其中使用带有 spring @Transactional 注释的以下方法:
@Autowired
Home home;
@Transactional
public Result update() {
try {
JsonNode jsonNode = request().body().asJson();
User user = home.updateFromJsonString(jsonNode.toString());
return ok("Updated successfully.");
} catch (Exception e){
return badRequest("Error updating");
}
}
updateFromJsonString 方法位于另一个项目中,它使用 hibernate 更改 sql 表。问题是,当缺少 @Transactional 注释时,这个“更新”方法可以正常工作,但是当它在那里时,我得到以下异常:
[error] o.h.e.j.s.SqlExceptionHelper - Duplicate entry '1-10' for key 'PRIMARY'
[error] play - Cannot invoke the action, eventually got an error: org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.
exception.ConstraintViolationException: could not execute statement
知道问题出在哪里,为什么@Transactional 会出现这个错误?
【问题讨论】:
-
好吧,你有一个违反约束的异常,这意味着你没有传递足够的数据来更新方法。
-
或者您想要更新与休眠会话分离的实体。在这种情况下,您必须在完全相同的会话中获取该实体并进行更新。
标签: java spring hibernate orm playframework