问题1.
     当我删除主控方时,没有持久化,直接new一个主控方然后setID。可以删除中间表数据和主控方数据,被控方数据没有改变。 问题:当我去删除一条没有的数据,会抛出org.hibernate.StaleStateException: Unexpected row count: 0 expected: 1,解释是:使用的是hibernate的saveOrUpdate方法保存实例。saveOrUpdate方法要求ID为null时才执行SAVE,在其它情况下执行UPDATE。在保存实例的时候是新增,但你的ID不为null,所以使用的是UPDATE,但是数据库里没有主键相关的值,所以出现异常。很奇怪不知道为什么.应该会抛出org.hibernate.ObjectNotFoundException异常才对.
删除的代码:
public void removeUser(Integer userID) {
if(userID != null){
userDao.remove(new User(userID));
}
}
问题2.
      当去查找一个不存在的数据时,不执行user.getUserID();就不会抛出ObjectNotFoundException异常。只有到调用get/set方法,才会抛出。所以当我们取出这个user时,实际对象是空的,当在去执行remove操作时,spring会抛出 嵌套着org.hibernate.ObjectNotFoundException的org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException。意思是:Hibernate-specific subclass of ObjectRetrievalFailureException. Converts Hibernate's UnresolvableObjectException and WrongClassException.
public void removeUser(Integer userID) {
if(userID != null){
User user = userDao.findByPK(userID);
                        user.getUserID();//如果不执行此操作,Hibernate就不会抛出ObjectNotFoundException.
userDao.remove(user);
}
}谁能给解释一下?