我有奇怪的行爲與@Transactional註釋。該代碼使用@Transactional行之有效的呼叫者:@Transactional上一個名爲Bean內部的公共方法:HHH000437
import org.springframework.transaction.annotation.Transactional;
private javax.persistence.EntityManager em;
@Transactional
public void caller(String login) {
callee(login);
}
public void callee(String login) {
user = new User(login);
em.persist(user);
userInfo = new UserInfo();
userInfo.setUser(user);
em.persist(userInfo);
}
但是下面執行第二em.persist在被叫返回錯誤使用@Transactional:
import org.springframework.transaction.annotation.Transactional;
private javax.persistence.EntityManager em;
public void caller(String login) {
callee(login);
}
@Transactional
public void callee(String login) {
user = new User(login);
em.persist(user);
userInfo = new UserInfo();
userInfo.setUser(user);
em.persist(userInfo); // ERROR: org.hibernate.action.internal.UnresolvedEntityInsertActions : HHH000437: Attempting to save one or more entities that have a non-nullable association with an unsaved transient entity. The unsaved transient entity must be saved in an operation prior to saving these dependent entities.
}
錯誤返回:
org.hibernate.action.internal.UnresolvedEntityInsertActions : HHH000437: Attempting to save one or more entities that have a non-nullable association with an unsaved transient entity. The unsaved transient entity must be saved in an operation prior to saving these dependent entities.
Unsaved transient entity: ([package.entities.User#<null>])
Dependent entities: ([[package.entities.UserInfo#<null>]])
Non-nullable association(s): ([package.entities.UserInfo.user])
有人有想法嗎?
謝謝!
Spring使用代理申請AOP,這基本上意味着,唯一的方法調用到(外部)對象檢索AOP,內部方法通過代理來調用。 – 2015-01-16 06:52:13