Hibernate级联删除
最近开发,出了一个需求需要使用级联删除,但是作的时候出现了问题,删除的时候出错了。
现在有Visatype和Visainfo这2个表,表结构如下:
接下来是hibernate生成的实体类和映射文件
Visatype
private Long vistypeid;
private String vistypename;
private String vistypeenname;
private Integer status;
private Long bl;
private Boolean JAmerica;
private Boolean JCanada;
private Boolean JAustralia;
private Boolean JChina;
private String city;
private String up;
private String down;
private String slef;
private List visainfolist;
private Set visinfo=new HashSet();
//get和set方法省略
<hibernate-mapping>
<class name="com.itsum.supertour.bg.passport.beans.dtos.Visatype" table="visatype" schema="dbo" catalog="obs">
<id name="vistypeid" type="java.lang.Long">
<column name="vistypeid" />
<generator class="identity"></generator>
</id>
<property name="vistypename" type="java.lang.String">
<column name="vistypename" length="500" />
</property>
<property name="status" type="java.lang.Integer">
<column name="status" />
</property>
<property name="bl" type="java.lang.Long">
<column name="bl" />
</property>
<property name="JAmerica" type="java.lang.Boolean">
<column name="j_America" />
</property>
<property name="JCanada" type="java.lang.Boolean">
<column name="j_Canada" />
</property>
<property name="JAustralia" type="java.lang.Boolean">
<column name="j_Australia" />
</property>
<property name="JChina" type="java.lang.Boolean">
<column name="j_China" />
</property>
<property name="city" type="java.lang.String">
<column name="city" length="50" />
</property>
<property name="vistypeenname" type="java.lang.String">
<column name="vistypeenname" length="50" />
</property>
<set name="visinfo" cascade="all" inverse="true" lazy="false">
<key column="vistypeid" />
<one-to-many class="com.itsum.supertour.bg.passport.beans.dtos.Visainfo" />
</set>
</class>
</hibernate-mapping>
private Long visid;
private String visaname;
private String visaenname;
private String visahref;
private Boolean JAmerica;
private Boolean JCanada;
private Boolean JAustralia;
private Boolean JChina;
private String city;
private Long bl;
private String up;
private String down;
private String slef;
private Visatype vistype;
<hibernate-mapping>
<class name="com.itsum.supertour.bg.passport.beans.dtos.Visainfo" table="visainfo" schema="dbo" catalog="obs">
<id name="visid" type="java.lang.Long">
<column name="visid" />
<generator class="identity"></generator>
</id>
<property name="visaname" type="java.lang.String">
<column name="visaname" length="500" />
</property>
<property name="visaenname" type="java.lang.String">
<column name="visaenname" length="500" />
</property>
<property name="visahref" type="java.lang.String">
<column name="visahref" length="500" />
</property>
<property name="JAmerica" type="java.lang.Boolean">
<column name="j_America" />
</property>
<property name="JCanada" type="java.lang.Boolean">
<column name="j_Canada" />
</property>
<property name="JAustralia" type="java.lang.Boolean">
<column name="j_Australia" />
</property>
<property name="JChina" type="java.lang.Boolean">
<column name="j_China" />
</property>
<property name="city" type="java.lang.String">
<column name="city" length="50" />
</property>
<property name="bl" type="java.lang.Long">
<column name="bl" />
</property>
<many-to-one name="vistype" column="vistypeid" alt="Hibernate级联删除解决办法" />
但是在删除类别"日本"的时候,出现了错误
Hibernate 数据库 级联删除 异常
[解决办法]
SQL级联删除与级联更新使用格式:
CREATE TABLE A001(ID INT PRIMARY KEY,NAME VARCHAR(20))
CREATE TABLE A002(ID INT REFERENCES A001(ID)ON DELETE CASCADE ON UPDATE CASCADE,AGE TINYINT)