相关代码:===============================================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(); }===============================================