views:

7474

answers:

1

Hi Every one, I have problem when want update my object to database using hiberntae when I want update(branchDao.update(be); ) it's throw below exception

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

this my code:

            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

and for update :

 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");
        }
    }

and this my BranchEntity class

@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;

some where i read that problem because i use @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN) bur what shoud i do when i need cascade? can anyone how can i fix this problem

+5  A: 

org.hibernate.Session.update() is not for transient objects - it's for updating persistent objects. The message you quote "a different object with the same identifier value was already associated with the session" explains the problem. You create a brand new object

BranchEntity be = new BranchEntity();

fill its fields and pass it to update. But update expects an object that is associated with the session. So you should load the object using your dao, something like

BranchEntity be = branchDao.loadBranchEntity(...);
Tadeusz Kopec
or you should use save/saveOrUpdate if you actually do want to create a new object.
Michael Wiles