<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 閱讀(590) 評論(0)  編輯  收藏 所屬分類: 技術積累
     
    主站蜘蛛池模板: 国产AV无码专区亚洲AV麻豆丫| 久久精品国产亚洲精品2020| 久久精品国产亚洲AV天海翼| 在线观看免费人成视频| 亚洲国产成人精品久久| ww在线观视频免费观看| 亚洲av成人一区二区三区| 亚洲精品动漫免费二区| 亚洲 欧洲 日韩 综合在线| 日韩欧美一区二区三区免费观看| 国产精品亚洲四区在线观看| 亚洲第一成年免费网站| 亚洲精品无码专区在线| 国产免费看插插插视频| 一级人做人a爰免费视频| 国产成人综合亚洲AV第一页| 久久精品乱子伦免费| 亚洲成人午夜电影| 日本高清免费不卡视频| 成人国产网站v片免费观看| 亚洲不卡中文字幕无码| 国产精品免费网站| 亚洲日本中文字幕天天更新| 又色又污又黄无遮挡的免费视 | 国产亚洲精品免费视频播放| 中文字幕无线码中文字幕免费| 亚洲无线电影官网| 99久久免费精品国产72精品九九 | 国产亚洲免费的视频看| 麻花传媒剧在线mv免费观看| 亚洲日韩看片无码电影| 2048亚洲精品国产| 啦啦啦完整版免费视频在线观看| 亚洲精品无码av片| 久久亚洲中文字幕精品一区四 | 国产婷婷成人久久Av免费高清| 亚洲妓女综合网99| 亚洲精品456播放| 2021在线观看视频精品免费| 久久亚洲AV成人无码国产最大| 国产成人麻豆亚洲综合无码精品|