Hibernate中配置级联删除

首先,要在建表SQL中加入对级联删除的支持

CREATE TABLE `voteitem` (
  `id` varchar(50) NOT NULL default '',
  `title` varchar(50) default NULL,
  `votenum` varchar(50) default NULL,
  `vote_id` varchar(50) NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `vote_id` (`vote_id`),
  CONSTRAINT `voteitem_ibfk_1` FOREIGN KEY (`vote_id`) REFERENCES `vote` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=gb2312 ROW_FORMAT=COMPRESSED;

其次,在hibernate mapping文件中设置cascade

< set  name ="voteitems"  cascade ="delete"  order-by ="title asc"  inverse ="true" >
        
< key  column ="vote_id" ></ key >
        
< one-to-many  class ="Voteitems" />
     
</ set >


你可能感兴趣的:(sql,Hibernate)