<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 閱讀(583) 評論(0)  編輯  收藏 所屬分類: 技術積累
     
    主站蜘蛛池模板: 国产精品亚洲一区二区麻豆| 久久久久久亚洲AV无码专区| 亚洲AV日韩综合一区尤物| 国产免费一区二区三区| 亚洲精品午夜视频| 2020因为爱你带字幕免费观看全集 | 亚洲韩国精品无码一区二区三区| 国产成人亚洲精品蜜芽影院| 免费无码成人AV片在线在线播放| 四虎必出精品亚洲高清| 日韩免费一区二区三区| 久久无码av亚洲精品色午夜| 国产福利免费在线观看| 免费人成再在线观看网站| 亚洲欭美日韩颜射在线二| 久久免费线看线看| 亚洲成人免费网站| 免费a级毛片无码a∨蜜芽试看| 中文字幕在线日亚洲9| 可以免费观看一级毛片黄a| 国产国产人免费人成成免视频| 亚洲伦另类中文字幕| 999国内精品永久免费观看| 亚洲午夜福利在线视频| www亚洲精品少妇裸乳一区二区| 中文字幕一区二区三区免费视频| 亚洲AV无码久久| 在线看片免费不卡人成视频| 精品一区二区三区免费毛片| 久久亚洲av无码精品浪潮| 99热这里只有精品6免费| 亚洲日韩中文字幕无码一区| 精品亚洲一区二区三区在线播放| 久久久久免费精品国产| 亚洲国产成人99精品激情在线| 免费成人午夜视频| 久久九九AV免费精品| 亚洲综合色婷婷在线观看| 国产L精品国产亚洲区久久| 久久午夜羞羞影院免费观看| 日韩国产欧美亚洲v片|