Duplicar posibles:
Hibernate: different object with the same identifier value was already associated with the sessionproblema saveOrUpdate objeto de Hibernate (un objeto diferente con el mismo identificador de sesión)
tengo problema cuando quieren actualizar mi objeto a la base de datos usando Hibernate cuando quiere actualizar (branchDao.update (be); ) es throw below exception
a different object with the same identifier value was already associated with the session
esta mi código:
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
y para la actualización:
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");
}
}
y esto mi clase 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;
Algunos donde leí ese problema porque uso @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
pero lo que shoud ¿Lo hago cuando necesito cascada?
o debe usar save/saveOrUpdate si realmente desea crear un nuevo objeto. –