Hbernateエンティティオブジェクトの3つの状態と困惑


公式文書
マンul
によると、hibernateエンティティは、Transient、Persistent、Detachedの3つの状態があります。この文章は公式サイトのマニュアルと
http://blog.csdn.net/hgd250/article/details/2775943このブログ
1、Transient状態
    An object is tranient if it has just been instantiated using the new operator、and it is not assiociated with a hibersnate Session.It hasのpersistent representation in the database andのidentifer value has bened.Transient instancs will be destreyed bythe garbage collect the appine the appine(and let Hbernate tare of the SQL statements that need to be executed for this tranition)
//    Transient  
User user=new User();
user.setName("root");
user.setPassword("root");
//   user   Transient  ,                  .
特徴:
  • newによって作成され、実装されており、IDの自動割当値はない
  • データベースには、エンティティオブジェクトに対応するレコード
  • がない。
  • は、ヒップホップのSessionに何らかの関連があり、つまりSessionの中の方法
  • を呼び出していないということである。
    2、
    Persistent状態
        A persistent instance has a representation in the database and n dentifer value.It might jushave been saved or loaded、however、it is by by definition in the scope of a Session.Hbebenate will dededededededededetectany changenenenenenenenenenese thethethethesasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasaststststststststststininininininininininininininininininininininininininininininininドノートexecute manaual UPDATE statemens、or DELETE statemens when n abject shuld be made tranient.
    User user=new User();
    user.setName("username");
    user.setPassword("password");
    //   user   Transient  ,                  .
     
    Session session = sessionFactory.openSession();
    Transaction tx=session.beginTransaction();
    //   user    Transient  
    session.save( user );
    //   user Persistent
    tx.commit();
    //              .
    Transaction tx2=session.beginTransaction();
    user.setPassword("abc");
    tx2.commit();
    //            Session save()     user  
    //   user    persistent  ,   user            
    //     ,               abc;
    session.close();
    特徴:
  • 各persistent状態のエンティティオブジェクトは、一つのsessionオブジェクトの例に関連している
  • Persistent状態にあるエンティティオブジェクトは、データベース内のレコードに関連付けられている。
  • Hibernateは、persistent状態のエンティティオブジェクトの属性変化に応じてデータベースに対応するレコード
  • を変更する。
    3、Detached状態
       A detached instance is an object that has been persistent、but its Session has been closed.The reference to the object is still valid、of course、and the detached instance might modified in state.
    //    Transient  
    User user=new User();
    user.setName("username");
    user.setPassword("password");
    //   user   Transient  ,                  .
    Session session = sessionFactory.openSession();
    Transaction tx=session.beginTransaction();
    //   user    Transient  
    session.save( user );
    //   user Persistent
    tx.commit();
    //              .
     session.close();
    //  user Detached   ,   user        ,            .
    特徴
  • 遊離状態は、持久状態エンティティオブジェクトから変化したものであり、session.close()になると、持久状態は遊離状態となります。
  • 自由状態エンティティは、もはやセッションオブジェクトに関連しない。
  • フリーランスエンティティオブジェクトは、データベース内のレコードと直接にリンクされていません。変更は、データベース内のデータに影響を与えません。
  • 自由状態エンティティオブジェクトは、まだ一意の識別子を有しており、データベースには依然として対応するレコード
  • が記録されている。
    本体の状態や変換は比較的簡単ですが、なぜこの3つの状態を区別するのか分かりません。まず瞬時状態とヒベルナは何の関連もなく、もちろん存在する必要はありません。持久状態は自動的にデータベースに変更できますが、この特性的な意味はあまりありません。実際に開発したDAO層は業務ロジックを書かないからです。シリーズの、sessionはdao層の方法で呼び出されてから閉じられます。遊離状態は概念上のもので、実際には意味がないと思います。私たちは一般的にDAOから検索結果を返してから、これらの対象の状態が気になりません。
    つまり、私たちはヒップホップを使って開発する時、これらの状態を気にしないということです。弟はこの3つの状態が本当に分かりません。本当の価値は何ですか?