<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Duran's technical life
    踏踏實實學技術,認認真真做研究。
    Suppose we start with a simple <one-to-many> association from Parent to Child.
     1<hibernate-mapping package="org.hibernate.auction.model">
     2 <class name="Parent" table="PARENT">
     3  <id name="id" column="PARENT_ID" type="long">
     4   <generator class="native"></generator>
     5  </id>
     6  <set name="children">
     7   <key column="MY_PARENT_ID" />
     8   <one-to-many class="org.hibernate.auction.model.Child" />
     9  </set>
    10 </class>
    11 <class name="Child" table="CHILD">
    12  <id name="id" column="CHILD_ID" type="long">
    13   <generator class="native"></generator>
    14  </id>
    15  <property name="name" column="NAME" type="string" />
    16 </class>
    17</hibernate-mapping>

    If we were to execute the following code

     1Parent p = new Parent(new HashSet()); 
     2Child c1 = new Child(new String("c1"
    ));
     3

     4try 
    {
     5    session =
     sessionFactory.openSession();            
     6    tx =
     session.beginTransaction();           
     7
        session.save(p);
     8    Long pid =
     p.getId();
     9
        tx.commit();
    10
        session.close();
    11
        
    12    session =
     sessionFactory.openSession();
    13    tx =
     session.beginTransaction();
    14    Parent pp = (Parent) session.load(Parent.class
    , pid); 
    15
        pp.getChildren().add(c1);        
    16
        session.save(c1);
    17
        session.flush();
    18
        tx.commit();
    19

    20}
     catch (HibernateException e) {
    21}

    Hibernate would issue two SQL statements:
    - an INSERT to create the record for c
    - an UPDATE to create the link from p to c

    Hibernate: insert into PARENT values ( )
    Hibernate: select parent0_.PARENT_ID as PARENT_ID0_ from PARENT parent0_ where parent0_.PARENT_ID=?
    Hibernate: select children0_.MY_PARENT_ID as MY_PAREN3___, children0_.CHILD_ID as CHILD_ID__, children0_.CHILD_ID as CHILD_ID0_, children0_.NAME as NAME0_ from CHILD children0_ where children0_.MY_PARENT_ID=?

    - Hibernate: insert into CHILD (NAME) values (?)
    - Hibernate: update CHILD set MY_PARENT_ID=? where CHILD_ID=?

    This is not only inefficient, but also violates any NOT NULL constraint on the
    MY_PARENT_ID column.

    **********1
    The underlying cause is that the link (the foreign key parent_id) from p to c is not considered part of the state of the Child object and is therefore not created in the INSERT. So the solution is to make the link part of the Child mapping.
    **********1

     <many-to-one name="my_parent"
      class
    ="org.hibernate.auction.model.Parent" column="MY_PARENT_ID"
      not-null
    ="true">
     
    </many-to-one>

    **********2
    Now that the Child entity is managing the state of the link, we tell the collection not to update the link. We use the inverse attribute.
    **********2

     <set name="children" inverse="true">
      
    <key column="MY_PARENT_ID" />
      
    <one-to-many class="org.hibernate.auction.model.Child" />
     
    </set>

    The following code would be used to add a new Child

    1Parent pp = (Parent) session.load(Parent.class, pid); 
    2
    pp.getChildren().add(c1);
    3c1.setMy_parent(pp); //the link is part of the Child object's state. @see***1 

    4session.save(c1);

    And now, only one SQL INSERT would be issued!
    To tighten things up a bit, we could create an addChild() method of Parent.

    1public void addChild(Child c) {
    2  c.setParent(this
    );
    3
      children.add(c);
    4}

    Now, the code to add a Child looks like

    1Parent p = (Parent) session.load(Parent.class, pid);
    2Child c = new
     Child();
    3p.addChild(c);  // 1.let both side of the association knows each other.

    4session.save(c); // 2.save the non-verse side

    =========================================================================================
    CONCLUSION:
    1.The non-inverse(inverse="false") side is responsible to save the in-memory representation (object) to the database .
    --- you should save the child: session.save(c);
    2.Changes made only to the inverse end of the association are not persisted.
    --- you needn't update the parent: //session.update(p);

     1session = sessionFactory.openSession();
     2tx =
     session.beginTransaction();
     3Parent pp = (Parent) session.load(Parent.classnew Long(1
    )); 
     4pp.getChildren().add(c1); //the parent(pp) now knowns about the relationship

     5c1.setMy_parent(pp); //the child(c1) now knows about the relationship
     6

     7//
     when cascade="save-update", update(p) will cause all p's
     8//
     referencing children become persistence(cascade 
     9//
     saveOrUpdate() operation to parent's all children).
    10//
     So, it's same as you explicitly call session.save(c1).
    11

    12//
     when cascade="none"(default), update(p) will just cause
    13//
     p become persistence.
    14//
     And inverse="true", update(p) just persists p, but not 
    15// persists the link between p and c1. @see ***2

    16session.update(pp);  
    17//session.save(c1);  

    18session.flush();
    19
    tx.commit();
    20


    Hibernate: select parent0_.PARENT_ID as PARENT_ID0_ from PARENT parent0_ where parent0_.PARENT_ID=?
    Hibernate: select children0_.MY_PARENT_ID as MY_PAREN3___, children0_.CHILD_ID as CHILD_ID__, children0_.CHILD_ID as CHILD_ID0_, children0_.NAME as NAME0_, children0_.MY_PARENT_ID as MY_PAREN3_0_ from CHILD children0_ where children0_.MY_PARENT_ID=?

     1session = sessionFactory.openSession();
     2tx =
     session.beginTransaction();
     3Parent pp = (Parent) session.load(Parent.classnew Long(1
    )); 
     4
    pp.getChildren().add(c1);
     5
    c1.setMy_parent(pp);
     6//
    session.update(pp);
     7

     8//
     when save(c1), because c1 holds a reference to its Parent
     9// (my_parent), so p become persistence too.

    10session.save(c1);  //The relationship will be saved. @see ***1
    11session.flush();
    12
    tx.commit();
    13


    Hibernate: select parent0_.PARENT_ID as PARENT_ID0_ from PARENT parent0_ where parent0_.PARENT_ID=?
    Hibernate: select children0_.MY_PARENT_ID as MY_PAREN3___, children0_.CHILD_ID as CHILD_ID__, children0_.CHILD_ID as CHILD_ID0_, children0_.NAME as NAME0_, children0_.MY_PARENT_ID as MY_PAREN3_0_ from CHILD children0_ where children0_.MY_PARENT_ID=?
    Hibernate: insert into CHILD (NAME, MY_PARENT_ID) values (?, ?)


     

    posted on 2005-05-19 09:28 Duran's technical life 閱讀(582) 評論(0)  編輯  收藏 所屬分類: 技術積累
     
    主站蜘蛛池模板: 91九色老熟女免费资源站 | 久久亚洲精品无码| 91免费精品国自产拍在线不卡| 国产精品视频免费观看| 久久精品视频亚洲| 人妻丰满熟妇无码区免费 | 精品国产日韩亚洲一区91| 女人隐私秘视频黄www免费| 亚洲天堂中文字幕在线| 一区二区三区免费视频播放器| 亚洲一级片免费看| 成全视频免费观看在线看| 亚洲VA中文字幕不卡无码| 99久久久国产精品免费牛牛四川 | 亚洲一区二区三区免费在线观看| 最新亚洲人成网站在线观看| 国产精品公开免费视频| yy一级毛片免费视频| 激情97综合亚洲色婷婷五| 国产精品亚洲精品| 国产网站免费观看| h视频在线观看免费| 亚洲av无码成h人动漫无遮挡| 久久99国产综合精品免费| 在线观看日本亚洲一区 | 无码国产精品一区二区免费16| 久久亚洲成a人片| 性xxxx视频播放免费| 中文字幕亚洲图片| 久久国产色AV免费观看| 亚洲欧美第一成人网站7777| 久久久久高潮毛片免费全部播放 | 亚洲AV无码乱码在线观看富二代| 57pao一国产成视频永久免费| 亚洲天堂免费在线| 自拍偷自拍亚洲精品第1页| h在线观看视频免费网站| 特黄特色大片免费| 亚洲高清不卡视频| 亚洲国产精品无码久久久久久曰| 一级毛片免费毛片一级毛片免费 |