Mögliche Duplizieren:
Hibernate: different object with the same identifier value was already associated with the sessionProblem saveOrUpdate Objekt Hibernate (ein anderes Objekt mit der gleichen Kennung Sitzung)
Ich habe Problem, wenn möchte, dass meine Objekt-Datenbank aktualisieren mit Hibernate, wenn ich will update (branchDao.update (be); ) es ist throw unter ausnahme
a different object with the same identifier value was already associated with the session
dies mein 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
und für 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");
}
}
und diese meine BranchEntity Klasse
@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;
Einige wo ich dieses Problem gelesen, weil ich @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
verwenden, aber was shoud Ich mache, wenn ich Kaskade brauche?
oder Sie sollten save/saveOrUpdate verwenden, wenn Sie tatsächlich ein neues Objekt erstellen möchten. –