我有休眠问题。我的域是 Voucher、Offer、ExtraUser、Term 和 TermUserRegistration。他们看起来像:
@Entity(name = "Voucher")
@Table(name = "extra_vouchers")
public class Voucher implements Cloneable, Serializable{
带有 ID 和其他属性... Voucher 是一些优惠券。每个OfferDB(它是offer..例如乘船旅行或其他东西..)都有不止一张优惠券。每个 Extrauser 还可以拥有多张优惠券。
对要约期限(具体日期)也有一些保留 - TermUserRegistration - 每一次注册就意味着一张优惠券。
然后提供:
@Entity
@Table(name = "extra_offers")
public class OfferDB implements Cloneable, Serializable {
@OneToMany (mappedBy="offer")
private List<Voucher> vouchers = new ArrayList<Voucher>();
包括 ID 和其他属性...
然后是额外用户:
@Entity(name = "extrauser")
@Table(name = "extra_users")
public class ExtraUser implements Cloneable, Serializable {
@OneToMany (fetch = FetchType.LAZY, mappedBy="owner")
private List<Voucher> vouchers = new ArrayList<Voucher>();
然后术语:
@Entity(name = "Term")
@Table(name = "extra_term")
public class Term implements Cloneable, Serializable{
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "extra_term_user", joinColumns = { @JoinColumn(name = "term_id") }, inverseJoinColumns = { @JoinColumn(name = "term_user_id") })
private List<TermUserRegistration> users = new ArrayList<TermUserRegistration>();
和 TermUserRegistration:
@Entity(name = "TermUserRegistration")
@Table(name = "extra_term_users")
public class TermUserRegistration implements Cloneable, Serializable{
@OneToOne(cascade = CascadeType.ALL)
private Voucher extraVoucher;
现在我想问你是否有某种方法可以通过 ID 删除 TermUserRegistration 并删除它的关联关系,如 Voucher ......我遇到了一些级联删除异常的问题。
我的方法看起来
@Override
public void unregisterUserFromTerm(Term termToExport, TermUserRegistration termUser) {
Voucher voucherToDisable = voucherDao.findById(termUser.getExtraVoucher().getId());
FreeVoucher freeVoucherToEdit = freeVoucherDao.findVoucherByCodeAndOfferId(termUser.getExtraVoucher().getCode(), termToExport.getOffer().getId());
Long l = Long.parseLong(String.valueOf(termToExport.getOffer().getId()));
Offer offer = offerDao.findById(l);
ExtraUser extraUser = userDao.findById(termUser.getExtraUser().getId());
termToExport.getUsers().remove(termUser);
extraTermDao.update(termToExport);
extraTermUserDao.delete(termUser);
offer.getVouchers().remove(voucherToDisable);
offerDao.update(offer);
extraUser.getVouchers().remove(voucherToDisable);
userDao.update(extraUser);
voucherDao.update(voucherToDisable);
voucherDao.delete(voucherToDisable);
freeVoucherToEdit.setFree(true);
freeVoucherDao.update(freeVoucherToEdit);
}
但我得到
严重:servlet [edit] 的 Servlet.service() 在路径 [/project] 的上下文中抛出异常 [执行失败:已删除的实例传递给 update(): [null entity name#null]],根本原因是 org.hibernate.ObjectDeletedException : 已删除的实例传递给 update(): [null entity name#null]