首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Hibernate级联操作Cascade学之-delete

2012-09-22 
Hibernate级联操作Cascade学之---delete所在cascade,就是说我在更新一方的时候,可以根据这一方对象之间的

Hibernate级联操作Cascade学之---delete

所在cascade,就是说我在更新一方的时候,可以根据这一方对象之间的关联关系,去对被关联方进行持久化,比如说Team和Student之间的1对多关系,使用cascade,可以在team方维护其持有的student集合时,自动对其新增,修改,删除的student对象进行持久化,而没必要显示的进行session.save(student)操作,cascade="delete"的意思就是说,我在session.delete(team)的时候,可以一并删除其所有的student

数据库脚本:

Hibernate级联操作Cascade学之-deletecreate?table?student(id?varchar(32)?primary?key,
Hibernate级联操作Cascade学之-delete?????????????????????team_id?varchar(32),
Hibernate级联操作Cascade学之-delete?????????????????????name?varchar(32),
Hibernate级联操作Cascade学之-delete?????????????????????cardid?varchar(32),
Hibernate级联操作Cascade学之-delete?????????????????????age?int);
Hibernate级联操作Cascade学之-delete
Hibernate级联操作Cascade学之-deletecreate?table?team(id?varchar(32)?primary?key,
Hibernate级联操作Cascade学之-delete??????????????????team_id?varchar(32),
Hibernate级联操作Cascade学之-delete??????????????????teamname?varchar(32));
Hibernate级联操作Cascade学之-delete
Hibernate级联操作Cascade学之-delete?????????????????????????
Hibernate级联操作Cascade学之-deleteinsert?into?team?values("1","1","team1");
Hibernate级联操作Cascade学之-deleteinsert?into?student?values("1","1","stu1","20070101",22);
Hibernate级联操作Cascade学之-deleteinsert?into?student?values("2","1","stu2","20070102",23);
Hibernate级联操作Cascade学之-delete
Hibernate级联操作Cascade学之-delete

持久化JavaBean

Hibernate级联操作Cascade学之-deletepackage?Cascade.saveUpdate;
Hibernate级联操作Cascade学之-delete
Hibernate级联操作Cascade学之-delete
Hibernate级联操作Cascade学之-deletepublic?class?Student?{
Hibernate级联操作Cascade学之-delete??private?String?id;
Hibernate级联操作Cascade学之-delete??private?String?cardid;
Hibernate级联操作Cascade学之-delete??private?String?name;
Hibernate级联操作Cascade学之-delete??private?int?age;
Hibernate级联操作Cascade学之-delete??private?Team?team;
Hibernate级联操作Cascade学之-deletepublic?String?getId()?{
Hibernate级联操作Cascade学之-delete????return?id;
Hibernate级联操作Cascade学之-delete}
Hibernate级联操作Cascade学之-deletepublic?void?setId(String?id)?{
Hibernate级联操作Cascade学之-delete????this.id?=?id;
Hibernate级联操作Cascade学之-delete}
Hibernate级联操作Cascade学之-deletepublic?String?getCardid()?{
Hibernate级联操作Cascade学之-delete????return?cardid;
Hibernate级联操作Cascade学之-delete}
Hibernate级联操作Cascade学之-deletepublic?void?setCardid(String?cardid)?{
Hibernate级联操作Cascade学之-delete????this.cardid?=?cardid;
Hibernate级联操作Cascade学之-delete}
Hibernate级联操作Cascade学之-deletepublic?String?getName()?{
Hibernate级联操作Cascade学之-delete????return?name;
Hibernate级联操作Cascade学之-delete}
Hibernate级联操作Cascade学之-deletepublic?void?setName(String?name)?{
Hibernate级联操作Cascade学之-delete????this.name?=?name;
Hibernate级联操作Cascade学之-delete}
Hibernate级联操作Cascade学之-deletepublic?int?getAge()?{
Hibernate级联操作Cascade学之-delete????return?age;
Hibernate级联操作Cascade学之-delete}
Hibernate级联操作Cascade学之-deletepublic?void?setAge(int?age)?{
Hibernate级联操作Cascade学之-delete????this.age?=?age;
Hibernate级联操作Cascade学之-delete}
Hibernate级联操作Cascade学之-delete
Hibernate级联操作Cascade学之-deletepublic?Team?getTeam()?{
Hibernate级联操作Cascade学之-delete????return?team;
Hibernate级联操作Cascade学之-delete}
Hibernate级联操作Cascade学之-deletepublic?void?setTeam(Team?team)?{
Hibernate级联操作Cascade学之-delete????this.team?=?team;
Hibernate级联操作Cascade学之-delete}
Hibernate级联操作Cascade学之-delete}
Hibernate级联操作Cascade学之-delete
Hibernate级联操作Cascade学之-delete
Hibernate级联操作Cascade学之-deletepackage?Cascade.saveUpdate;
Hibernate级联操作Cascade学之-delete
Hibernate级联操作Cascade学之-delete
Hibernate级联操作Cascade学之-deleteimport?java.util.HashSet;
Hibernate级联操作Cascade学之-deleteimport?java.util.Set;
Hibernate级联操作Cascade学之-delete
Hibernate级联操作Cascade学之-deletepublic?class?Team?{
Hibernate级联操作Cascade学之-delete???private?String?id;
Hibernate级联操作Cascade学之-delete???private?Set?students=new?HashSet();
Hibernate级联操作Cascade学之-delete???private?String?teamName;
Hibernate级联操作Cascade学之-deletepublic?String?getId()?{
Hibernate级联操作Cascade学之-delete????return?id;
Hibernate级联操作Cascade学之-delete}
Hibernate级联操作Cascade学之-deletepublic?void?setId(String?id)?{
Hibernate级联操作Cascade学之-delete????this.id?=?id;
Hibernate级联操作Cascade学之-delete}
Hibernate级联操作Cascade学之-deletepublic?Set?getStudents()?{
Hibernate级联操作Cascade学之-delete????return?students;
Hibernate级联操作Cascade学之-delete}
Hibernate级联操作Cascade学之-deletepublic?void?setStudents(Set?students)?{
Hibernate级联操作Cascade学之-delete????this.students?=?students;
Hibernate级联操作Cascade学之-delete}
Hibernate级联操作Cascade学之-deletepublic?String?getTeamName()?{
Hibernate级联操作Cascade学之-delete????return?teamName;
Hibernate级联操作Cascade学之-delete}
Hibernate级联操作Cascade学之-deletepublic?void?setTeamName(String?teamName)?{
Hibernate级联操作Cascade学之-delete????this.teamName?=?teamName;
Hibernate级联操作Cascade学之-delete}
Hibernate级联操作Cascade学之-delete
Hibernate级联操作Cascade学之-delete
Hibernate级联操作Cascade学之-delete}
Hibernate级联操作Cascade学之-delete

Hibernate.cfg.xml

Hibernate级联操作Cascade学之-delete<?xml?version='1.0'?encoding='UTF-8'?>
Hibernate级联操作Cascade学之-delete<!DOCTYPE?hibernate-configuration?PUBLIC
Hibernate级联操作Cascade学之-delete??????????"-//Hibernate/Hibernate?Configuration?DTD?3.0//EN"
Hibernate级联操作Cascade学之-delete??????????"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
Hibernate级联操作Cascade学之-delete
Hibernate级联操作Cascade学之-delete<!--?Generated?by?MyEclipse?Hibernate?Tools.???????????????????-->
Hibernate级联操作Cascade学之-delete<hibernate-configuration>
Hibernate级联操作Cascade学之-delete
Hibernate级联操作Cascade学之-delete<session-factory>
Hibernate级联操作Cascade学之-delete????<property?name="connection.username">root</property>
Hibernate级联操作Cascade学之-delete????<property?name="connection.url">
Hibernate级联操作Cascade学之-delete????????jdbc:mysql://localhost:3306/schoolproject?characterEncoding=gb2312&amp;useUnicode=true
Hibernate级联操作Cascade学之-delete????</property>
Hibernate级联操作Cascade学之-delete????<property?name="dialect">
Hibernate级联操作Cascade学之-delete????????org.hibernate.dialect.MySQLDialect
Hibernate级联操作Cascade学之-delete????</property>
Hibernate级联操作Cascade学之-delete????<property?name="myeclipse.connection.profile">mysql</property>
Hibernate级联操作Cascade学之-delete????<property?name="connection.password">1234</property>
Hibernate级联操作Cascade学之-delete????<property?name="connection.driver_class">
Hibernate级联操作Cascade学之-delete????????com.mysql.jdbc.Driver
Hibernate级联操作Cascade学之-delete????</property>
Hibernate级联操作Cascade学之-delete????<property?name="hibernate.dialect">
Hibernate级联操作Cascade学之-delete????????org.hibernate.dialect.MySQLDialect
Hibernate级联操作Cascade学之-delete????</property>
Hibernate级联操作Cascade学之-delete????<property?name="hibernate.show_sql">true</property>
Hibernate级联操作Cascade学之-delete????<property?name="current_session_context_class">thread</property>
Hibernate级联操作Cascade学之-delete????<property?name="jdbc.batch_size">15</property>
Hibernate级联操作Cascade学之-delete????<mapping?resource="Cascade/delete/Student.hbm.xml"?/>
Hibernate级联操作Cascade学之-delete????<mapping?resource="Cascade/delete/Team.hbm.xml"?/>
Hibernate级联操作Cascade学之-delete
Hibernate级联操作Cascade学之-delete
Hibernate级联操作Cascade学之-delete
Hibernate级联操作Cascade学之-delete</session-factory>
Hibernate级联操作Cascade学之-delete
Hibernate级联操作Cascade学之-delete</hibernate-configuration>

Student.hbm.xml

Hibernate级联操作Cascade学之-delete<?xml?version="1.0"?encoding="utf-8"?>
Hibernate级联操作Cascade学之-delete<!DOCTYPE?hibernate-mapping?PUBLIC?"-//Hibernate/Hibernate?Mapping?DTD?3.0//EN"
Hibernate级联操作Cascade学之-delete"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
Hibernate级联操作Cascade学之-delete<!--?
Hibernate级联操作Cascade学之-delete????Mapping?file?autogenerated?by?MyEclipse?-?Hibernate?Tools
Hibernate级联操作Cascade学之-delete-->
Hibernate级联操作Cascade学之-delete<hibernate-mapping>
Hibernate级联操作Cascade学之-delete<class?name="Cascade.delete.Student"?table="student">
Hibernate级联操作Cascade学之-delete??<id?name="id"?unsaved-value="null">
Hibernate级联操作Cascade学之-delete????<generator?class="uuid.hex"></generator>
Hibernate级联操作Cascade学之-delete??</id>
Hibernate级联操作Cascade学之-delete??<property?name="cardid"?type="string"/>
Hibernate级联操作Cascade学之-delete??<property?name="name"?type="string"/>
Hibernate级联操作Cascade学之-delete??<property?name="age"?type="int"/>
Hibernate级联操作Cascade学之-delete??<many-to-one?name="team"?
Hibernate级联操作Cascade学之-delete???????????????column="team_id"
Hibernate级联操作Cascade学之-delete???????????????class="Cascade.delete.Team"
Hibernate级联操作Cascade学之-delete???????????????lazy="no-proxy">
Hibernate级联操作Cascade学之-delete??</many-to-one>
Hibernate级联操作Cascade学之-delete</class>
Hibernate级联操作Cascade学之-delete
Hibernate级联操作Cascade学之-delete</hibernate-mapping>
Hibernate级联操作Cascade学之-delete

Team.hbm.xml

Hibernate级联操作Cascade学之-delete<?xml?version="1.0"?encoding="utf-8"?>
Hibernate级联操作Cascade学之-delete<!DOCTYPE?hibernate-mapping?PUBLIC?"-//Hibernate/Hibernate?Mapping?DTD?3.0//EN"
Hibernate级联操作Cascade学之-delete"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
Hibernate级联操作Cascade学之-delete<!--?
Hibernate级联操作Cascade学之-delete????Mapping?file?autogenerated?by?MyEclipse?-?Hibernate?Tools
Hibernate级联操作Cascade学之-delete-->
Hibernate级联操作Cascade学之-delete<hibernate-mapping>
Hibernate级联操作Cascade学之-delete<class?name="Cascade.delete.Team"?table="team"?lazy="true">
Hibernate级联操作Cascade学之-delete???????<id?name="id"?column="id">
Hibernate级联操作Cascade学之-delete?????????<generator?class="uuid.hex"></generator>
Hibernate级联操作Cascade学之-delete???????</id>
Hibernate级联操作Cascade学之-delete???????<property?name="teamName"?column="teamname"></property>
Hibernate级联操作Cascade学之-delete??????
Hibernate级联操作Cascade学之-delete???????<set?name="students"?lazy="true"?inverse="false"?cascade="delete">
Hibernate级联操作Cascade学之-delete?????????<key?column="team_id"></key>
Hibernate级联操作Cascade学之-delete?????????<one-to-many?class="Cascade.delete.Student"/>
Hibernate级联操作Cascade学之-delete???????</set>
Hibernate级联操作Cascade学之-delete??????</class>
Hibernate级联操作Cascade学之-delete</hibernate-mapping>
Hibernate级联操作Cascade学之-delete

测试代码:

Hibernate级联操作Cascade学之-deletepackage?Cascade.delete;
Hibernate级联操作Cascade学之-delete
Hibernate级联操作Cascade学之-delete
Hibernate级联操作Cascade学之-deleteimport?java.io.File;
Hibernate级联操作Cascade学之-delete
Hibernate级联操作Cascade学之-deleteimport?org.hibernate.Session;
Hibernate级联操作Cascade学之-deleteimport?org.hibernate.SessionFactory;
Hibernate级联操作Cascade学之-deleteimport?org.hibernate.Transaction;
Hibernate级联操作Cascade学之-deleteimport?org.hibernate.cfg.Configuration;
Hibernate级联操作Cascade学之-delete
Hibernate级联操作Cascade学之-deletepublic?class?Test?{
Hibernate级联操作Cascade学之-delete
Hibernate级联操作Cascade学之-delete
Hibernate级联操作Cascade学之-delete????public?static?void?main(String[]?args)?{
Hibernate级联操作Cascade学之-delete
Hibernate级联操作Cascade学之-delete????????String?filePath=System.getProperty("user.dir")+File.separator+"src/Cascade/delete"+File.separator+"hibernate.cfg.xml";
Hibernate级联操作Cascade学之-delete????????File?file=new?File(filePath);
Hibernate级联操作Cascade学之-delete????????System.out.println(filePath);
Hibernate级联操作Cascade学之-delete????????SessionFactory?sessionFactory=new?Configuration().configure(file).buildSessionFactory();
Hibernate级联操作Cascade学之-delete????????Session?session=sessionFactory.openSession();
Hibernate级联操作Cascade学之-delete????????Transaction?t=session.beginTransaction();
Hibernate级联操作Cascade学之-delete????????Student?newStu=new?Student();??//建立一个student对象,瞬态
Hibernate级联操作Cascade学之-delete????????newStu.setCardid("12345");
Hibernate级联操作Cascade学之-delete????????newStu.setAge(22);
Hibernate级联操作Cascade学之-delete????????newStu.setName("newStu");
Hibernate级联操作Cascade学之-delete????????Team?team=(Team)session.get(Team.class,?"1");//获得team对象
Hibernate级联操作Cascade学之-delete????????session.delete(team);//删除team并一并删除其所有拥有的student
Hibernate级联操作Cascade学之-delete????????/*
Hibernate级联操作Cascade学之-delete?????????*?设置cascade="delete"?只能通过session.delete()删除其所有的学生
Hibernate级联操作Cascade学之-delete?????????*?设置cascade="delete-orphan"?可以通过team.getStudents().remove(stu)方式删除学生
Hibernate级联操作Cascade学之-delete?????????*/
Hibernate级联操作Cascade学之-delete????????t.commit();
Hibernate级联操作Cascade学之-delete????????
Hibernate级联操作Cascade学之-delete????}
Hibernate级联操作Cascade学之-delete
Hibernate级联操作Cascade学之-delete}
Hibernate级联操作Cascade学之-delete

运行结果:

Hibernate: select team0_.id as id1_0_, team0_.teamname as teamname1_0_ from team team0_ where team0_.id=?

Hibernate: select students0_.team_id as team5_1_, students0_.id as id1_, students0_.id as id0_0_, students0_.cardid as cardid0_0_, students0_.name as name0_0_, students0_.age as age0_0_, students0_.team_id as team5_0_0_ from student students0_ where students0_.team_id=?

Hibernate: update student set team_id=null where team_id=?
Hibernate: delete from student where id=?
Hibernate: delete from student where id=?
Hibernate: delete from team where id=?

需要注意的是红色部分的update,照说应该只有select和delete操作,这是因为我们再Team方设置inverse="false"|
这就要求Team要维护Team和Student之间的1对多关系,所以Hibernat将Student的team_id设置成null以断开他么之间的联系,其实这个update是多余的,因为修改后还是被删除了,这点可以通过设置inverse="true"来时实现,可以提高一些效率

热点排行