hibernate 级联删除
配置文件和pojo类如下:
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!--
- Mapping file autogenerated by MyEclipse Persistence Tools
- -->
- <hibernate-mapping>
- <class name="com.hotel.dao.Movietype" table="movietype" catalog="hotelfidelio">
- <id name="id" type="java.lang.Integer">
- <column name="id" />
- <generator class="native"></generator>
- </id>
- <property name="name" type="java.lang.String">
- <column name="name" length="60" not-null="true" />
- </property>
- <property name="field1" type="java.lang.String">
- <column name="field1" length="20" />
- </property>
- <set name="movies"
- table="movie"
- inverse="true"
- lazy="false"
- cascade="all">
- <key column="id_movietype"></key>
- <one-to-many class="com.hotel.dao.Movie"/>
- </set>
- </class>
- </hibernate-mapping>
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!--
- Mapping file autogenerated by MyEclipse Persistence Tools
- -->
- <hibernate-mapping>
- <class name="com.hotel.dao.Movie" table="movie" catalog="hotelfidelio">
- <id name="id" type="java.lang.Integer">
- <column name="id" />
- <generator class="native"></generator>
- </id>
- <property name="moviename" type="java.lang.String">
- <column name="moviename" length="200" not-null="true" />
- </property>
- <property name="movienameSys" type="java.lang.String">
- <column name="moviename_sys" length="300" not-null="true" unique="true" />
- </property>
- <property name="director" type="java.lang.String">
- <column name="director" length="200" />
- </property>
- <property name="impersonator" type="java.lang.String">
- <column name="impersonator" length="200" />
- </property>
- <property name="charge" type="java.lang.Double">
- <column name="charge" precision="22" scale="0" not-null="true" />
- </property>
- <property name="intro" type="java.lang.String">
- <column name="intro" length="16277215" />
- </property>
- <property name="imgsrc" type="java.lang.String">
- <column name="imgsrc" length="300" />
- </property>
- <property name="field1" type="java.lang.String">
- <column name="field1" length="20" />
- </property>
- <property name="field2" type="java.lang.String">
- <column name="field2" length="20" />
- </property>
- <property name="idmovietype" type="java.lang.Integer">
- <column name="id_movietype" precision="3" scale="0" not-null="true" />
- </property>
- <many-to-one
- name="movietype"
- class="com.hotel.dao.Movietype"
- column="id_movietype"
- insert="false"
- update="false"
- outer-join="auto"
- fetch="select"
- access="property"
- not-null="true"
- lazy="false"
- >
- </many-to-one>
- </class>
- </hibernate-mapping>
网上很多帖子都说cascade="all" 就可以级联删除,但是我这里怎么设置都没法成功,观察输出日志
只有 movietype的删除 sql:
Hibernate: delete from hotel.movietype where id=?
之后看到 arthur.3726 文章:http://arthur-3726.javaeye.com/blog/422877 发现只有用hibernate自带的事物执行语句才能执行级联删除,觉得有可能不是配置文件的问题,而是java代码的问题:
Eclipse配置完ssh后,自动生成的代码:
- public void delete(Movietype persistentInstance) {
- log.debug("deleting Movietype instance");
- try {
- getHibernateTemplate().delete(persistentInstance);
- log.debug("delete successful");
- } catch (RuntimeException re) {
- log.error("delete failed", re);
- throw re;
- }
- }
改成直接由hibernate的session来执行:
- public void delete_cascade(final Movietype persistentInstance) {
- log.debug("deleting Movietype instance");
- try {
- getHibernateTemplate().executeFind(new HibernateCallback() {
- public Object doInHibernate(Session s)
- throws HibernateException, SQLException {
- s.setFlushMode(FlushMode.AUTO);
- s.beginTransaction().begin();
- s.delete(persistentInstance);
- s.beginTransaction().commit();
- s.close();
- return null;
- }
- });
- } catch (RuntimeException re) {
- log.error("find all failed", re);
- throw re;
- }
- }
测试删除movietype,输出sql语句:
Hibernate: delete from hotelfidelio.movie where id=?
Hibernate: delete from hotelfidelio.movietype where id=?
删除成功。
参考:http://arthur-3726.javaeye.com/blog/422877
http://docs.jboss.org/hibernate/stable/core/reference/en/html_single/#objectstate-transitive
http://docs.jboss.org/hibernate/stable/core/reference/en/html/example-parentchild.html