Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1650264
  • 博文数量: 13833
  • 博客积分: 1041400
  • 博客等级: 大元帅
  • 技术积分: 127080
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-13 10:12
文章分类

全部博文(13833)

文章存档

2008年(13833)

我的朋友

分类:

2008-06-13 10:26:11

相关代码:===============================================public class VoteQuestion {    private Set options = new HashSet();    。。。  /**  * @hibernate.set cascade = "all" table = "VOTE_OPTION" inverse =  *                "true" lazy = "true" order-by = "id"  *   * @hibernate.collection-one-to-many class =  *                                   "eorg.etoken.pojo.VoteOption"  *   * @hibernate.collection-key column = "QUESTION_ID"  */ public Set getOptions() {        return options; } } public class VoteOption {    private VoteQuestion question;    。。。。  /**  * @hibernate.many-to-one class = "eorg.etoken.pojo.VoteQuestion" cascade =  *                        "none" not-null = "true" column = "QUESTION_ID"  *                        update = "false"  */ public VoteQuestion getQuestion() {       return question; } } public class VoteServiceImpl{ 。。。 public void deleteVoteOption(int optionId) {      Transaction tx = session.beginTransaction();      VoteOption option = (VoteOption) session.load(VoteOption.class, new Long(optionId));      session.delete(option);      tx.commit(); }} =============================================== 当调用VoteServiceImpl的deleteVoteOption方法时,就会抛出下列Exception:org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations) 原因是votequestion与voteoption有连带关系,不能直接删除voteoption,而应该从votequestion里remove voteoption! 因此上面的deleteVoteOption方法只需要修改一行即可,见下面代码 =============================================== public void deleteVoteOption(int optionId) {      Transaction tx = session.beginTransaction();      VoteOption option = (VoteOption) session.load(VoteOption.class, new Long(optionId));      option.getQuestion().getOptions().remove(option);      tx.commit(); }=============================================== 
阅读(62) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~