配置文件和pojo类如下:
- xml version="1.0" encoding="utf-8"?>
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name&#61;"com.hotel.dao.Movietype" table&#61;"movietype" catalog&#61;"hotelfidelio">
- <id name&#61;"id" type&#61;"java.lang.Integer">
- <column name&#61;"id" />
- <generator class&#61;"native">generator>
- id>
- <property name&#61;"name" type&#61;"java.lang.String">
- <column name&#61;"name" length&#61;"60" not-null&#61;"true" />
- property>
- <property name&#61;"field1" type&#61;"java.lang.String">
- <column name&#61;"field1" length&#61;"20" />
- property>
- <set name&#61;"movies"
- table&#61;"movie"
- inverse&#61;"true"
- lazy&#61;"false"
- cascade&#61;"all">
- <key column&#61;"id_movietype">key>
- <one-to-many class&#61;"com.hotel.dao.Movie"/>
- set>
- class>
- hibernate-mapping>
- xml version&#61;"1.0" encoding&#61;"utf-8"?>
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name&#61;"com.hotel.dao.Movie" table&#61;"movie" catalog&#61;"hotelfidelio">
- <id name&#61;"id" type&#61;"java.lang.Integer">
- <column name&#61;"id" />
- <generator class&#61;"native">generator>
- id>
- <property name&#61;"moviename" type&#61;"java.lang.String">
- <column name&#61;"moviename" length&#61;"200" not-null&#61;"true" />
- property>
- <property name&#61;"movienameSys" type&#61;"java.lang.String">
- <column name&#61;"moviename_sys" length&#61;"300" not-null&#61;"true" unique&#61;"true" />
- property>
- <property name&#61;"director" type&#61;"java.lang.String">
- <column name&#61;"director" length&#61;"200" />
- property>
- <property name&#61;"impersonator" type&#61;"java.lang.String">
- <column name&#61;"impersonator" length&#61;"200" />
- property>
- <property name&#61;"charge" type&#61;"java.lang.Double">
- <column name&#61;"charge" precision&#61;"22" scale&#61;"0" not-null&#61;"true" />
- property>
- <property name&#61;"intro" type&#61;"java.lang.String">
- <column name&#61;"intro" length&#61;"16277215" />
- property>
- <property name&#61;"imgsrc" type&#61;"java.lang.String">
- <column name&#61;"imgsrc" length&#61;"300" />
- property>
- <property name&#61;"field1" type&#61;"java.lang.String">
- <column name&#61;"field1" length&#61;"20" />
- property>
- <property name&#61;"field2" type&#61;"java.lang.String">
- <column name&#61;"field2" length&#61;"20" />
- property>
- <property name&#61;"idmovietype" type&#61;"java.lang.Integer">
- <column name&#61;"id_movietype" precision&#61;"3" scale&#61;"0" not-null&#61;"true" />
- property>
- <many-to-one
- name&#61;"movietype"
- class&#61;"com.hotel.dao.Movietype"
- column&#61;"id_movietype"
- insert&#61;"false"
- update&#61;"false"
- outer-join&#61;"auto"
- fetch&#61;"select"
- access&#61;"property"
- not-null&#61;"true"
- lazy&#61;"false"
- >
- many-to-one>
- class>
- hibernate-mapping>
网上很多帖子都说cascade&#61;"all" 就可以级联删除&#xff0c;但是我这里怎么设置都没法成功&#xff0c;观察输出日志
只有 movietype的删除 sql&#xff1a;
Hibernate: delete from hotel.movietype where id&#61;?
之后看到 arthur.3726 文章&#xff1a;http://arthur-3726.javaeye.com/blog/422877 发现只有用hibernate自带的事物执行语句才能执行级联删除&#xff0c;觉得有可能不是配置文件的问题&#xff0c;而是java代码的问题&#xff1a;
Eclipse配置完ssh后&#xff0c;自动生成的代码&#xff1a;
- 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来执行&#xff1a;
- 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&#xff0c;输出sql语句&#xff1a;
Hibernate: delete from hotelfidelio.movie where id&#61;?
Hibernate: delete from hotelfidelio.movietype where id&#61;?
删除成功。
参考&#xff1a;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/objectstate.html#objectstate-transitive
http://docs.jboss.org/hibernate/stable/core/reference/en/html/example-parentchild.html