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

Am1*_*3zA 12 java session hibernate

可能重复:
Hibernate:具有相同标识符值的不同对象已与会话关联

当我想要更新时,我想用hibernate将对象更新到数据库时有问题(branchDao.update(be);)它抛出异常

a different object with the same identifier value was already associated with the session
Run Code Online (Sandbox Code Playgroud)

我的代码:

            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
Run Code Online (Sandbox Code Playgroud)

和更新:

 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");
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是我的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;
Run Code Online (Sandbox Code Playgroud)

有些地方我读了那个问题,因为我使用@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)但是当我需要级联时我会做什么?

Tad*_*pec 14

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

BranchEntity be = new BranchEntity();
Run Code Online (Sandbox Code Playgroud)

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

BranchEntity be = branchDao.loadBranchEntity(...);
Run Code Online (Sandbox Code Playgroud)