在执行这些步骤时,我得到了 org.hibernate.NonUniqueObjectException。
session.beginTransaction();
TransactionEntry te = (TransactionEntry)session.get(TransactionEntry.class, primaryKey);
session.getTransaction().commit();
.
.
.
session.beginTransaction();
session.saveOrUpdate(te.getAccount());
session.delete(te);
session.delete(te.getTransaction());
session.getTransaction().commit();
我的模型快照如下:
TransactionEntry 类
@Entity
public class TransactionEntry {
@Id
@GeneratedValue
private long txnEntryId;
@ManyToOne
@JoinColumn(name = "account")
private Account account;
@ManyToOne
@JoinColumn(name = "txnId")
private TransactionTable transaction;
}
账户类
@Entity
public class Account {
@Id
@GeneratedValue
private long accountId;
}
事务表类
@Entity
public class TransactionTable {
@Id
@GeneratedValue
private long txnId;
@OneToMany(targetEntity = TransactionEntry.class, fetch = FetchType.LAZY,
mappedBy = "transaction", cascade = CascadeType.ALL)
private List<TransactionEntry> transactionEntries;
}
我收到以下异常:
org.hibernate.NonUniqueObjectException: a different object with the same
identifier value was already associated with the
session: [com.pratikabu.pem.model.Account#1]
如果我删除该 session.delete(te.getTransaction());
语句,则代码工作正常,否则将引发上述异常。有什么我想念的吗。