这个是我做的用例。测试cascade和inverse.分析结果以及具体hibernate sql都在,比较容易看明白。
希望不是很明白的看看,加深印象。
小弟学识浅薄,如果有不对的地方,请指出来,谢谢!~
测试环境:Eclispe,用断点测试。
数据库是SQLServer2000.
Area.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd";; >
<hibernate-mapping>
<class name="net.villion.model.test.Area" table="r_area">
<id column="ID" type="long" name = "id" length="10" unsaved-value="null">
<generator class = "native"/>
</id>
<property column="name" name="name" type="string"/>
<many-to-one name="state" class="net.villion.model.test.State" cascade="all">
<column name="stateid" index="index_area_state"/>
</many-to-one>
</class>
</hibernate-mapping>
State.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd";; >
<hibernate-mapping>
<class name="net.villion.model.test.State" table="r_state">
<id column="ID" type="long" name = "id" length="10" unsaved-value="null">
<generator class = "native"/>
</id>
<property column="name" name="name" type="string"/>
<set name="areas" lazy="true" cascade="all" inverse="false">
<key column="stateid"/>
<one-to-many class="net.villion.model.test.Area"/>
</set>
</class>
</hibernate-mapping>
我们主要看inverse,cascade级联的设置。
当State.hbm.xml inverse = "false" cascade = "all"
//添加
//code
StateDAO sdao =(StateDAO) ctx.getBean("stateDAO");
AreaDAO adao = (AreaDAO)ctx.getBean("areaDAO");
Area a3 = new Area();
a3.setName("NewYork");
Area a4 = new Area();
a4.setName("Frolida");
State s2 = new State();
s2.setName("America");
Set set = new HashSet();
set.add(a3);
set.add(a4);
s2.setAreas(set);
sdao.saveObject(s2);
//sql:
Hibernate: insert into r_state (name) values (?)
Hibernate: insert into r_area (name, stateid) values (?, ?)
Hibernate: insert into r_area (name, stateid) values (?, ?)
Hibernate: update r_area set stateid=? where ID=?
Hibernate: update r_area set stateid=? where ID=?
//当State.hbm.xml inverse = "false" cascade = "none"(注意我们操作的是State)
//sql: insert into r_state (name) values (?)
Hibernate: update r_area set stateid=? where ID=?
org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance
这时候更新area的时候由于area,并未级联插入所以找不到报错。所以这样设置还是会更新Area的stateid
接着上面的说,我们要看当设置改变时的sql变化。
上面就说明了cascade="all" 时候能级联插入,而none的时候只是维持两者之间的关系(inverse="false")。
上面两种情况的删除又不一样了,但是一个道理
cascade="all"时,两个表数据都级联删除了。
而cascade="none"时
Hibernate: update r_area set stateid=null where stateid=?
Hibernate: delete from r_state where ID=?说明它只是维持关系。
而当上面的inverse ="true"时 cascade="none"时
sql: insert into r_state (name) values (?)
只插入一条State,不会插入Area表了。
删除时
Hibernate: delete from r_state where ID=?
org.springframework.dao.DataIntegrityViolationException: (Hibernate operation): data integrity violated by SQL ''; nested exception is com.jnetdirect.jsql.u: DELETE 语句与 COLUMN REFERENCE 约束 'FKC7F1003A8EEBF16C' 冲突。该冲突发生于数据库 'hd',表 'r_area', column 'stateid'。
com.jnetdirect.jsql.u: DELETE 语句与 COLUMN REFERENCE 约束 'FKC7F1003A8EEBF16C' 冲突
由于没有维护数据库关系,引起外键冲突报错。
inverse ="true"时 cascade="all"时
sql:
Hibernate: insert into r_state (name) values (?)
Hibernate: insert into r_area (name, stateid) values (?, ?)
Hibernate: insert into r_area (name, stateid) values (?, ?)
全部级联插入,但r_area表中stateid是null。
删除时。
Hibernate: delete from r_area where ID=?
Hibernate: delete from r_area where ID=?
Hibernate: delete from r_state where ID=?
级联删除。
所以总结一下 ,cascade 是级联的程度,而inverse="false|ture" 是指是否维持两个实体的关系。(两个表的外键)
水平有限,请各位看客口下留情。
如果耽误了你的宝贵,我在这里道歉了。 :(
希望不是很明白的看看,加深印象。
小弟学识浅薄,如果有不对的地方,请指出来,谢谢!~
测试环境:Eclispe,用断点测试。
数据库是SQLServer2000.
Area.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd";; >
<hibernate-mapping>
<class name="net.villion.model.test.Area" table="r_area">
<id column="ID" type="long" name = "id" length="10" unsaved-value="null">
<generator class = "native"/>
</id>
<property column="name" name="name" type="string"/>
<many-to-one name="state" class="net.villion.model.test.State" cascade="all">
<column name="stateid" index="index_area_state"/>
</many-to-one>
</class>
</hibernate-mapping>
State.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd";; >
<hibernate-mapping>
<class name="net.villion.model.test.State" table="r_state">
<id column="ID" type="long" name = "id" length="10" unsaved-value="null">
<generator class = "native"/>
</id>
<property column="name" name="name" type="string"/>
<set name="areas" lazy="true" cascade="all" inverse="false">
<key column="stateid"/>
<one-to-many class="net.villion.model.test.Area"/>
</set>
</class>
</hibernate-mapping>
我们主要看inverse,cascade级联的设置。
当State.hbm.xml inverse = "false" cascade = "all"
//添加
//code
StateDAO sdao =(StateDAO) ctx.getBean("stateDAO");
AreaDAO adao = (AreaDAO)ctx.getBean("areaDAO");
Area a3 = new Area();
a3.setName("NewYork");
Area a4 = new Area();
a4.setName("Frolida");
State s2 = new State();
s2.setName("America");
Set set = new HashSet();
set.add(a3);
set.add(a4);
s2.setAreas(set);
sdao.saveObject(s2);
//sql:
Hibernate: insert into r_state (name) values (?)
Hibernate: insert into r_area (name, stateid) values (?, ?)
Hibernate: insert into r_area (name, stateid) values (?, ?)
Hibernate: update r_area set stateid=? where ID=?
Hibernate: update r_area set stateid=? where ID=?
//当State.hbm.xml inverse = "false" cascade = "none"(注意我们操作的是State)
//sql: insert into r_state (name) values (?)
Hibernate: update r_area set stateid=? where ID=?
org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance
这时候更新area的时候由于area,并未级联插入所以找不到报错。所以这样设置还是会更新Area的stateid
接着上面的说,我们要看当设置改变时的sql变化。
上面就说明了cascade="all" 时候能级联插入,而none的时候只是维持两者之间的关系(inverse="false")。
上面两种情况的删除又不一样了,但是一个道理
cascade="all"时,两个表数据都级联删除了。
而cascade="none"时
Hibernate: update r_area set stateid=null where stateid=?
Hibernate: delete from r_state where ID=?说明它只是维持关系。
而当上面的inverse ="true"时 cascade="none"时
sql: insert into r_state (name) values (?)
只插入一条State,不会插入Area表了。
删除时
Hibernate: delete from r_state where ID=?
org.springframework.dao.DataIntegrityViolationException: (Hibernate operation): data integrity violated by SQL ''; nested exception is com.jnetdirect.jsql.u: DELETE 语句与 COLUMN REFERENCE 约束 'FKC7F1003A8EEBF16C' 冲突。该冲突发生于数据库 'hd',表 'r_area', column 'stateid'。
com.jnetdirect.jsql.u: DELETE 语句与 COLUMN REFERENCE 约束 'FKC7F1003A8EEBF16C' 冲突
由于没有维护数据库关系,引起外键冲突报错。
inverse ="true"时 cascade="all"时
sql:
Hibernate: insert into r_state (name) values (?)
Hibernate: insert into r_area (name, stateid) values (?, ?)
Hibernate: insert into r_area (name, stateid) values (?, ?)
全部级联插入,但r_area表中stateid是null。
删除时。
Hibernate: delete from r_area where ID=?
Hibernate: delete from r_area where ID=?
Hibernate: delete from r_state where ID=?
级联删除。
所以总结一下 ,cascade 是级联的程度,而inverse="false|ture" 是指是否维持两个实体的关系。(两个表的外键)
水平有限,请各位看客口下留情。
如果耽误了你的宝贵,我在这里道歉了。 :(