WEB开发网
开发学院WEB开发Jsp Java 中的父子关系及 inverse 详解 阅读

Java 中的父子关系及 inverse 详解

 2008-01-05 09:57:50 来源:WEB开发网   
核心提示:首先以父子关系为例: < 对应生成的DDL drop table PARENT; 代码: drop table CHILD; create table PARENT (ID INTEGER not null generated by default as identity, PRimary key (ID));

  首先以父子关系为例:
  
  <
  
  对应生成的DDL drop table PARENT;
  代码:
  drop table CHILD;
  create table PARENT (ID INTEGER not null generated by default as identity, PRimary key (ID));
  create table CHILD (ID INTEGER not null generated by default as identity, PARENTID INTEGER, primary key (ID));
  alter table CHILD add constraint FK3D1FCFC74B18345 foreign key (PARENTID) references PARENT; 
  
  *大写的部分inverse="true"表示 ParentPO 本身不维护表之间的关系!,而由想反的一方 children来维护,
  
  *CASCADE=“ALL”表示 无论是update,insert ,delete 都保持几连关系
  
  *lazy="true"表示初始化父亲的时候不会把所有的儿子都从数据库中load进来。
  
  下面先看一下几个例子:
  
  生成的SQL:
  
  代码:
  Hibernate: insert into PARENT (ID) values (default)
  Hibernate: insert into CHILD (PARENTID, ID) values (?, default)
  Hibernate: insert into CHILD (PARENTID, ID) values (?, default) 
  
  结果 C:\Myapp\SQLLIB\BIN>db2 select * from child
  
  代码:
  ID     PARENTID
  ----------- -----------
       71     44
       72     44
       73     44
  C:\Myapp\SQLLIB\BIN>db2 select * from parent
   
  ID
  -----------
       44
   
  注重之只有一句:session.save(parent);就把两个儿子保存进了数据库。
  
  *首先讲讲inverse=true作用: 这里关系是由儿子维护的,所以假如只是往父亲里加入儿子,不给儿子设置父亲的话session.save(parent),就不会保存儿子! 看这个例子:注重与例子1的对比
  
  代码:
  *ChildPO child = new ChildPO(parent)---〉ChildPO child = new ChildPO(),
  
          ITxMgr tx = null;
          tx = HibernateTxMgr.beginTrans("Add a new relationships...");
          session = (Session) tx.getSession();
          parent = new ParentPO();
          ChildPO child = new ChildPO();
          ChildPO child2 = new ChildPO();
          List list = new ArrayList();
          list.add(child);
          list.add(child2);
          parent.setChildren(list);
          session.save(parent);
          session.flush();
          System.out.println("dddddddddddddddddddddddddddddddddddddddddddddddddddddd") ;
          ChildPO child3 = new ChildPO();
          child3.setParent(parent);
          session.save(child3);
          session.flush();
          System.out.println("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee") ;
          tx.endTrans(); 
  
  生成的SQL没有变
  
  代码:
  Hibernate: insert into PARENT (ID) values (default)
   
  Hibernate: insert into CHILD (PARENTID, ID) values (?, default)
   
  Hibernate: insert into CHILD (PARENTID, ID) values (?, default)
   
  dddddddddddddddddddddddddddddddddddddddddddddddddddddd
  Hibernate: insert into CHILD (PARENTID, ID) values (?, default)
  eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee 
  
  *注重父子关系丢失了 C:\Myapp\SQLLIB\BIN>db2 select * from child
  
  代码:
  ID     PARENTID
  ----------- -----------
       74      -
       75      -
       76     45
  C:\Myapp\SQLLIB\BIN>db2 select * from parent
   
  ID
  -----------
       45 
  
  *为什么最后一个孩子的父亲没有丢失呢? 就在于child3.setParent(parent);,所以关系是由孩子维护的,假如child不setParent,或者 new childPO(父亲)的话 父子关系就丢失了,parent.setChildren(list);是没有用的!
  *这里就又引入了另外一个问题为什么要用inverse? 用了它维护关系起岂不是很麻烦?,这里给出个例子给大家一个解释:(要害原因在于性能)
  
  下面这个例子和例子一完全一样,所差的就是没有用inverse=true
  
  例子2:
  
  代码:
  hibernate-mapping>
    <class name="com.etech.bm.po.ChildPO" table="CHILD">
          <id name="id" column="ID" type="integer">
              <generator class="identity"/>
          </id>
      <many-to-one name="parent" class="com.etech.bm.po.ParentPO" column="PARENTID"/>
    </class>
    <class name="com.etech.bm.po.ParentPO" table="PARENT">
          <id name="id" column="ID" type="integer">
              <generator class="identity"/>
          </id>
      <bag name="children" CASCADE=“ALL”>
          <key column="PARENTID"/>
          <one-to-many class="com.etech.bm.po.ChildPO"/>
      </bag>
    </class>
   
  </hibernate-mapping> 
  
  代码:
  drop table PARENT;
  drop table CHILD;
  create table PARENT (ID INTEGER not null generated by default as identity, primary key (ID));
  create table CHILD (ID INTEGER not null generated by default as identity, PARENTID INTEGER, primary key (ID));
  alter table CHILD add constraint FK3D1FCFC74B18345 foreign key (PARENTID) references PARENT; 
  
  代码:
  ITxMgr tx = null;
          tx = HibernateTxMgr.beginTrans("Add a new relationships...");
          session = (Session) tx.getSession();
          parent = new ParentPO();
          ChildPO child = new ChildPO(parent);
          ChildPO child2 = new ChildPO(parent);
          List list = new ArrayList();
          list.add(child);
          list.add(child2);
          parent.setChildren(list);
          session.save(parent);
          session.flush();
          System.out.println("dddddddddddddddddddddddddddddddddddddddddddddddddddddd") ;
          ChildPO child3 = new ChildPO();
          child3.setParent(parent);
          session.save(child3);
          session.flush();
          System.out.println("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee") ;
          tx.endTrans();  
  
  hibernate 生成的sql Hibernate: insert into PARENT (ID) values (default)
  
  代码:
  Hibernate: insert into CHILD (PARENTID, ID) values (?, default)
   
  Hibernate: insert into CHILD (PARENTID, ID) values (?, default)
   
  Hibernate: update CHILD set PARENTID=? where ID=?
  dddddddddddddddddddddddddddddddddddddddddddddddddddddd
  Hibernate: insert into CHILD (PARENTID, ID) values (?, default)
  Hibernate: values IDENTITY_VAL_LOCAL() 
  
  结果 C:\Myapp\SQLLIB\BIN>db2 select * from parent
  
  代码:
  ID
  -----------
       46
  ID     PARENTID
  ----------- -----------
       77     46
       78     46
       79     46 
  
  明显比原来多了一句Hibernate: update CHILD set PARENTID=? where ID=?针对每一个孩子都去更新父亲的id明显速度很慢,因为父亲有个孩子的集合,他无法知道哪个孩子的父亲id已经指向自己了,所以对于每一个孩子,都要更新父亲使他只想自己,而这个关系由孩子维护就好多了,每个孩子只有一个父亲,只有设置过的才需要更新,所以显然,这个父子关系由孩子来维护比较省力.减轻了数据库的负担
  
  *现在我们再来看看在没有 inverse=true 的条件下 ChildPO child = new ChildPO(parent)---〉ChildPO child = new ChildPO(),
  
  
  代码:
  ITxMgr tx = null;
          tx = HibernateTxMgr.beginTrans("Add a new relationships...");
          session = (Session) tx.getS

Tags:Java 父子 关系

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接