问题saveOrUpdate对象Hibernate(具有相同标识符会话的不同对象)

时间:2009-07-28 08:33:10

标签: java hibernate session

  

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

我想在使用hibernate将对象更新到数据库时遇到问题 当我想要更新时(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)但是当我需要级联时我会怎么做?

1 个答案:

答案 0 :(得分:14)

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

BranchEntity be = new BranchEntity();

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

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