【问题标题】:problem saveOrUpdate object Hibernate (a different object with the same identifier session) [duplicate]问题 saveOrUpdate 对象休眠(具有相同标识符会话的不同对象)[重复]
【发布时间】:2010-11-14 15:05:32
【问题描述】:

可能重复:
Hibernate: different object with the same identifier value was already associated with the session

当我想使用休眠将我的对象更新到数据库时遇到问题 当我想要更新(branchDao.update(be); ) 它抛出异常

a different object with the same identifier value was already associated with the session

这是我的代码:

            LoginEntity le = loginDao.getSpecificLogin(pkId);
            le.setPassword(password);
            le.setRoles(role);
            loginDao.update(le);               
            HumanEntity he = humanDao.getHumanById(humanID);  // humanEntuty farde morede nazar ro bar hasbe Email taraf load mikonad
            he.setHumanEmail(oemail);
            he.setHumanFamily(olname);
            he.setHumanName(ofname);
            humanDao.update(he);
            superBranchUsername = branch.getFatherUsername();
            int superBranchId = branchDao.getBranchIdByUserName(superBranchUsername);
            BranchEntity superBranch = branchDao.load(superBranchId);
            BranchEntity be = new BranchEntity();
            setBranchEntity(be, he, pkId, bname, confirmed, level, studentCount, uname, superBranch, le);
            branchDao.update(be); //this line throw exception

以及更新:

 public void update(T transientObject) throws DatabaseException {
        Session s = HibernateUtil.getSession();
        Transaction tx = null;
        try {
            tx = s.beginTransaction();
            s.update(transientObject);
            s.flush();
            tx.commit();
        } catch (HibernateException e) {
            System.out.println(e.getMessage());
            tx.rollback();
            e.printStackTrace();

            throw new DatabaseException("cant't update object");
        }
    }

这是我的 BranchEntity 类

@Entity
@Table(name = "branch", uniqueConstraints = {@UniqueConstraint(columnNames = {"bname", "branch_fk"})})
public class BranchEntity implements Serializable {

    @Id
    @GeneratedValue
    private int id;
    @Column(name = "username", length = 64, nullable = false)
    private String userName;
    @Column(name = "bname", length = 64)
    private String branchName;
    @Column(name = "studcount")
    private int studCount;
    @Column(name = "blevel", columnDefinition = "int default 0")
    private int level;
    @Column(name = "confirmed", columnDefinition = "tinyint default 0")
    private int confirmed;
    @OneToMany(mappedBy = "branch", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @OnDelete(action = OnDeleteAction.CASCADE)
    @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
    private Set<BranchBuildingEntity> branchbuilding = new HashSet<BranchBuildingEntity>();
    @OneToMany(mappedBy = "branch", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @OnDelete(action = OnDeleteAction.CASCADE)
    @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
    private Set<BranchPictureEntity> branchPicture = new HashSet<BranchPictureEntity>();
    @OneToOne(fetch = FetchType.EAGER)    
    @JoinColumn(name = "login_fk", nullable = true)
    private LoginEntity login;
    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "human_fk", nullable = true)
    private HumanEntity human;        
    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "branch_fk", referencedColumnName = "id", nullable = true)
    private BranchEntity superBranch;

因为我使用@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN),所以我在某些地方读到了这个问题,但是当我需要级联时我应该怎么做?

【问题讨论】:

    标签: java hibernate session


    【解决方案1】:

    org.hibernate.Session.update() 不适用于瞬态对象 - 它用于更新持久对象。您引用的消息“具有相同标识符值的不同对象已与会话相关联”解释了该问题。您创建了一个全新的对象

    BranchEntity be = new BranchEntity();
    

    填写其字段并将其传递给更新。但 update 需要一个与会话关联的对象。所以你应该使用你的 dao 来加载对象,比如

    BranchEntity be = branchDao.loadBranchEntity(...);
    

    【讨论】:

    • 或者如果你确实想创建一个新对象,你应该使用 save/saveOrUpdate。
    猜你喜欢
    • 2011-04-02
    • 2011-04-06
    • 2013-04-21
    • 2010-11-07
    • 2012-06-06
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多