Java 问题 saveOrUpdate 对象休眠(具有相同标识符会话的不同对象)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1192691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
problem saveOrUpdate object Hibernate (a different object with the same identifier session)
提问by Am1rr3zA
Possible Duplicate:
Hibernate: different object with the same identifier value was already associated with the session
可能的重复:
休眠:具有相同标识符值的不同对象已经与会话相关联
I have problem when want update my object to database using hibernate when I want update(branchDao.update(be); ) it's throw below exception
当我想要 update(branchDao.update(be); ) 时,我想使用 hibernate 将我的对象更新到数据库时遇到问题,它在异常以下抛出
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
这是我的 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;
Some where I read that problem because I use @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
but what shoud I do when I need cascade?
我在某些地方读到了这个问题,因为我使用@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
但是当我需要级联时我应该怎么做?
采纳答案by Tadeusz Kopec
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
org.hibernate.Session.update() 不是用于临时对象 - 它用于更新持久对象。您引用的消息“具有相同标识符值的不同对象已与会话关联”解释了该问题。您创建了一个全新的对象
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
填写其字段并将其传递给更新。但是 update 需要一个与会话相关联的对象。所以你应该使用你的 dao 加载对象,比如
BranchEntity be = branchDao.loadBranchEntity(...);