商品批发信息发布平台之open session in view 中级联删除问题

先看下过程

sort.java

public class Sort implements Serializable {
	private int id;
	private String name;
	private int grade;//类别等级
	private String descr;
	private Sort sort;//父类
	private List sorts=new ArrayList();//子类
	public int getId() {
                 ......
             }
}

 

sort.hbm.xml

 

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration.                   -->
<!-- Created Sun Jan 29 11:50:19 GMT-08:00 2006                         -->
<hibernate-mapping package="po">

    <class name="Sort" table="t_sort">
        <id name="id" column="id" type="int">
            <generator class="native"/>
        </id>
 
        <property name="name" column="name" type="string"  not-null="true" />
		<property name="grade" column="grade" type="int"  not-null="true" />
		<property name="descr" column="descr" type="string"  not-null="true" />
		<many-to-one name="sort" class="Sort">
		<column name="pid"></column>
		</many-to-one>
		<list name="sorts" cascade="all" >
		<key>
		<column name="pid"></column>
		</key>
		<list-index>
		<column name="sort_index" ></column>
		</list-index>
		<one-to-many class="Sort" />
		</list>
		
    </class>
    
</hibernate-mapping>

 

web.xml

 <!-- 解决hiberbate  lazy问题-->
  <filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>util.MyOpenSessionInView</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>hibernateFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

 MyOpenSessionInView.java

public class MyOpenSessionInView extends OpenSessionInViewFilter {

	@Override
	protected Session getSession(SessionFactory sessionFactory)
			throws DataAccessResourceFailureException {
		Session session=SessionFactoryUtils.getSession(sessionFactory, true);
		session.setFlushMode(FlushMode.COMMIT);
		return session;
	}
	protected void closeSession(Session session, SessionFactory factory) {
		session.flush();
		super.closeSession(session, factory);
		}

}

  MyOpenSessionInView.java 类是为了解决下面的错误问题。下面的问题的解决办法也有几种,在我的其他转载文章中有提到。

org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO or remove 'readOnly' marker from transaction definition

 

 在hibernate 删除关联时会出现

eleted object would be re-saved by cascade (remove deleted object from associations)

的异常,结合别人的和自己的经验通常有三种解决的方案:

方法1 删除Set方的cascade:
方法2 解决关联关系后,再删除

    onside.getManys().remove(thisMany);    // 在所关联的一方的set中移走当前要删除的对象
   
thisMany.setOne( null );                           // 设置所对应的一方为空,解除它们之间的关系
   
manyDao.delete(thisMany);

方法3 在many-to-one方增加cascade 但值不能是none

以上是我用的第2种方法的一种改进,因为在第二种方法来做的时候依然会报

eleted object would be re-saved by cascade (remove deleted object from associations)

的异常,所以我采取的办法是

public void deleteByid(int sortid) {
		this.getHibernateTemplate().delete(this.getSort(sortid));
		this.getSession().flush();
	}

 

和上面的的

<list name="sorts" cascade="all" >

这样既解决了删除的级联,也消除了上面的异常。


以上纯属个人的尝试,由于经验较少,错误的地方请指教。

你可能感兴趣的:(DAO,Hibernate,xml,Web,MyEclipse)