簡體   English   中英

Spring @Transactional在Hibernate中造成問題

[英]Spring @Transactional makes problems in Hibernate

我有一個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會導致此錯誤?

那是因為您加載了一個實體,所以它由Hibernate進行管理。因此,在打開Session時發生的任何更改都會被臟檢查機制攔截,並在刷新 Hibernate Session時傳播到數據庫。

我懷疑您將一個新的孩子添加到已經包含該孩子實體的一對多集合中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM