Hibernate无法合并和持久化实体类的两个实例

时间:2015-02-22 19:46:04

标签: java hibernate

当我尝试更新Team时,Hibernate抛出异常:

    Multiple representations of the same entity [Team#1] are being merged.
 Detached: [Team@49a0aef3]; Managed: [Team@bf505c]

TeamDAOImpl

    public void updateTeam(Team team) {
            Team teamToUpdate = getTeam(team.getId());
            teamToUpdate.setName(team.getName());
            team = null; // doesn't help to avoid exception
            getCurrentSession().merge(teamToUpdate);
        }
    public Team getTeam(int id) {
        Team team = (Team) getCurrentSession().get(Team.class, id);
        return team;
    }

我见过与此异常相关的大多数问题但未找到有效的解决方案。如何解决?

更新

似乎是Hibernate团队必须解决的某种bug问题。在我的情况下,逻辑是非常微不足道的,我无法相信Hibernate无法更新当前实体。必须有办法。

1 个答案:

答案 0 :(得分:1)

通过从源实体中删除 CascadeType 来解决此问题。