热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

hibernate级联删除

配置文件和pojo类如下:Xml代码

配置文件和pojo类如下:

 

Xml代码 
  1. xml version="1.0" encoding="utf-8"?>  
  2. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  3.   
  4. <hibernate-mapping>  
  5.     <class name&#61;"com.hotel.dao.Movietype" table&#61;"movietype" catalog&#61;"hotelfidelio">  
  6.         <id name&#61;"id" type&#61;"java.lang.Integer">  
  7.             <column name&#61;"id" />  
  8.             <generator class&#61;"native">generator>  
  9.         id>  
  10.         <property name&#61;"name" type&#61;"java.lang.String">  
  11.             <column name&#61;"name" length&#61;"60" not-null&#61;"true" />  
  12.         property>  
  13.         <property name&#61;"field1" type&#61;"java.lang.String">  
  14.             <column name&#61;"field1" length&#61;"20" />  
  15.         property>  
  16.         <set name&#61;"movies"   
  17.           table&#61;"movie"  
  18.           inverse&#61;"true"  
  19.           lazy&#61;"false"  
  20.           cascade&#61;"all">  
  21.          <key column&#61;"id_movietype">key>  
  22.          <one-to-many class&#61;"com.hotel.dao.Movie"/>  
  23.         set>  
  24.     class>  
  25. hibernate-mapping>  

 

 

 

 

 

Xml代码 
  1. xml version&#61;"1.0" encoding&#61;"utf-8"?>  
  2. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  3.   
  4. <hibernate-mapping>  
  5.     <class name&#61;"com.hotel.dao.Movie" table&#61;"movie" catalog&#61;"hotelfidelio">  
  6.         <id name&#61;"id" type&#61;"java.lang.Integer">  
  7.             <column name&#61;"id" />  
  8.             <generator class&#61;"native">generator>  
  9.         id>  
  10.         <property name&#61;"moviename" type&#61;"java.lang.String">  
  11.             <column name&#61;"moviename" length&#61;"200" not-null&#61;"true" />  
  12.         property>  
  13.         <property name&#61;"movienameSys" type&#61;"java.lang.String">  
  14.             <column name&#61;"moviename_sys" length&#61;"300" not-null&#61;"true" unique&#61;"true" />  
  15.         property>  
  16.         <property name&#61;"director" type&#61;"java.lang.String">  
  17.             <column name&#61;"director" length&#61;"200" />  
  18.         property>  
  19.         <property name&#61;"impersonator" type&#61;"java.lang.String">  
  20.             <column name&#61;"impersonator" length&#61;"200" />  
  21.         property>  
  22.         <property name&#61;"charge" type&#61;"java.lang.Double">  
  23.             <column name&#61;"charge" precision&#61;"22" scale&#61;"0" not-null&#61;"true" />  
  24.         property>  
  25.         <property name&#61;"intro" type&#61;"java.lang.String">  
  26.             <column name&#61;"intro" length&#61;"16277215" />  
  27.         property>  
  28.         <property name&#61;"imgsrc" type&#61;"java.lang.String">  
  29.             <column name&#61;"imgsrc" length&#61;"300" />  
  30.         property>  
  31.         <property name&#61;"field1" type&#61;"java.lang.String">  
  32.             <column name&#61;"field1" length&#61;"20" />  
  33.         property>  
  34.         <property name&#61;"field2" type&#61;"java.lang.String">  
  35.             <column name&#61;"field2" length&#61;"20" />  
  36.         property>  
  37.     <property name&#61;"idmovietype" type&#61;"java.lang.Integer">  
  38.         <column name&#61;"id_movietype" precision&#61;"3" scale&#61;"0" not-null&#61;"true" />  
  39.     property>  
  40.     <many-to-one   
  41.          name&#61;"movietype"  
  42.          class&#61;"com.hotel.dao.Movietype"  
  43.          column&#61;"id_movietype"  
  44.          insert&#61;"false"  
  45.          update&#61;"false"  
  46.              outer-join&#61;"auto"  
  47.          fetch&#61;"select"  
  48.              access&#61;"property"  
  49.              not-null&#61;"true"  
  50.              lazy&#61;"false"  
  51.          >  
  52.     many-to-one>  
  53.     class>  
  54. 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;

 

 

Java代码 
  1. public void delete(Movietype persistentInstance) {  
  2.     log.debug("deleting Movietype instance");  
  3.     try {  
  4.         getHibernateTemplate().delete(persistentInstance);  
  5.         log.debug("delete successful");  
  6.     } catch (RuntimeException re) {  
  7.         log.error("delete failed", re);  
  8.         throw re;  
  9.     }  
  10. }  

 

 

改成直接由hibernate的session来执行&#xff1a;

 

 

Java代码 
  1. public void delete_cascade(final Movietype persistentInstance) {  
  2.     log.debug("deleting Movietype instance");  
  3.     try {  
  4.         getHibernateTemplate().executeFind(new HibernateCallback() {  
  5.             public Object doInHibernate(Session s)  
  6.                     throws HibernateException, SQLException {  
  7.                 s.setFlushMode(FlushMode.AUTO);  
  8.                 s.beginTransaction().begin();  
  9.                 s.delete(persistentInstance);  
  10.                 s.beginTransaction().commit();  
  11.                 s.close();  
  12.                 return null;  
  13.             }  
  14.         });  
  15.     } catch (RuntimeException re) {  
  16.         log.error("find all failed", re);  
  17.         throw re;  
  18.     }  
  19. }  

 

 测试删除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

转:https://www.cnblogs.com/panjun-Donet/archive/2010/07/29/1787607.html



推荐阅读
author-avatar
霦槟哊禮kc
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有