gpt4 book ai didi

java - 问题 saveOrUpdate 对象 Hibernate(具有相同标识符 session 的不同对象)

转载 作者:搜寻专家 更新时间:2023-10-30 21:07:22 24 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
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) 但是当我需要级联时我应该做什么?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com