teamService.save(team);
Save方法仅接受临时对象。您可以在这里找到什么是瞬态对象
Transient - an object is transient if it has just been instantiated using thenew operator, and it is not associated with a Hibernate Session. It has nopersistent representation in the database and no identifier value has beenassigned. Transient instances will be destroyed by the garbage collector ifthe application does not hold a reference anymore. Use the Hibernate Sessionto make an object persistent (and let Hibernate take care of the SQLstatements that need to be executed for this transition).
您正在获取Team对象,并尝试将其持久化到数据库中,但是该对象中包含Account对象,并且该Account对象已分离(意味着该对象的实例已保存到DB中,但该对象不在会话中)。Hibernate正在尝试保存它,因为您已指定:
@oneToMany(cascade = CascadeType.ALL, ....
因此,有几种方法可以解决它:
1)不要使用CascadeType.ALL配置。帐户对象可用于团队数(至少域结构允许),并且更新操作可能会更新所有团队的帐户-
这意味着此操作不应在团队更新时启动。如果确实需要使用MERGE /
DELETE配置,我将从那里删除级联参数(默认值是不执行级联操作)。但是,如果您确实需要坚持下去,请参阅选项#2
2)使用“ saveOrUpdate()”方法代替“ save()”。“
saveOrUpdate()”方法接受瞬态和分离的对象。但是这种方法的问题在于设计:在保存团队对象时,您真的需要插入/更新帐户吗?我将其分为两个操作,并阻止从团队更新Account。
希望这可以帮助。



