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

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

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

    Neil的備忘錄

    just do it
    posts - 66, comments - 8, trackbacks - 0, articles - 0

    Making objects persistent

    Posted on 2008-11-01 10:07 Neil's NoteBook 閱讀(124) 評論(0)  編輯  收藏

    Newly instantiated instances of a a persistent class are considered transient by Hibernate. We can make a transient instance persistent by associating it with a session:

    DomesticCat fritz = new DomesticCat();
    fritz.setColor(Color.GINGER);
    fritz.setSex('M');
    fritz.setName("Fritz");
    Long generatedId = (Long) sess.save(fritz);

    If Cat has a generated identifier, the identifier is generated and assigned to the cat when save() is called. If Cat has an assigned identifier, or a composite key, the identifier should be assigned to the cat instance before calling save(). You may also use persist() instead of save(), with the semantics defined in the EJB3 early draft.

    • persist() makes a transient instance persistent. However, it doesn't guarantee that the identifier value will be assigned to the persistent instance immediately, the assignment might happen at flush time. persist() also guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries. This is useful in long-running conversations with an extended Session/persistence context.

    • save() does guarantee to return an identifier. If an INSERT has to be executed to get the identifier ( e.g. "identity" generator, not "sequence"), this INSERT happens immediately, no matter if you are inside or outside of a transaction. This is problematic in a long-running conversation with an extended Session/persistence context.

    Alternatively, you may assign the identifier using an overloaded version of save().

    DomesticCat pk = new DomesticCat();
    pk.setColor(Color.TABBY);
    pk.setSex('F');
    pk.setName("PK");
    pk.setKittens( new HashSet() );
    pk.addKitten(fritz);
    sess.save( pk, new Long(1234) );

    If the object you make persistent has associated objects (e.g. the kittens collection in the previous example), these objects may be made persistent in any order you like unless you have a NOT NULL constraint upon a foreign key column. There is never a risk of violating foreign key constraints. However, you might violate a NOT NULL constraint if you save() the objects in the wrong order.

    Usually you don't bother with this detail, as you'll very likely use Hibernate's transitive persistence feature to save the associated objects automatically. Then, even NOT NULL constraint violations don't occur - Hibernate will take care of everything. Transitive persistence is discussed later in this chapter. 


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲中文字幕无码中文| 亚洲今日精彩视频| 亚洲中文无码mv| 免费国产成人午夜私人影视| 亚洲一卡2卡3卡4卡乱码 在线| 99re免费在线视频| 亚洲国产成人精品无码一区二区| 日韩精品内射视频免费观看| 久久亚洲精品成人AV| 24小时免费看片| 亚洲va在线va天堂成人| 成人免费无毒在线观看网站| 亚洲人成色4444在线观看| 国产免费直播在线观看视频| 午夜免费福利影院| 国产AV无码专区亚洲AV麻豆丫| 国产传媒在线观看视频免费观看 | 国产精品亚洲AV三区| 永久免费av无码网站大全| 青草久久精品亚洲综合专区| 亚洲国产婷婷香蕉久久久久久| 在线观看免费视频一区| 亚洲视频网站在线观看| 国产激情久久久久影院老熟女免费| 国产福利视精品永久免费| 亚洲国产精品一区二区三区在线观看| 久久成人18免费网站| 免费国产成人午夜私人影视| 在线观看免费黄色网址| 亚洲91精品麻豆国产系列在线| 国产成人免费福利网站| 国产又黄又爽胸又大免费视频| 亚洲视频在线观看网站| 国内一级一级毛片a免费| 成年女人A毛片免费视频| 亚洲啪啪免费视频| 亚洲人成无码www久久久| 亚洲黄色免费观看| 青青草国产免费国产是公开| 亚洲男人的天堂在线| 亚洲国产精品自产在线播放|