Hibernate開發文檔(基礎部分)

          Hibernate開發文檔
          一.基本概念:
          SessionFactory :它是單個數據映射表經編譯后的內存鏡像,是線程安全的,是生成session的工廠.該對象可以在進程或集群的級別上,為那些事務之間可以重用的數據提供可選的二級緩存.
          Session:它是應用程序與持久存儲層之間交互操作的一個單線程對象.所有的持久化對象必須在session管理下才可以進行持久化操作.此對象生存期很短,它隱藏了JDBC連接,也是Transaction的工廠.Session對象有一個一級緩存,顯式執行flush之前,所有持久化操作的數據都緩存在Session對象處.
          持久態:系統創建的pojo對象,一旦與Session關聯起來并對應成數據庫中的記錄,對其所有的操作都相當于對數據庫的操作
          暫態/脫管態:暫態指新創建的未與Session關聯的的對象,其可能是未持久化的對象;脫管態指持久態的對象因Session關閉導致臨時失去持久態的對象
          事務:代表一次原子操作,具有數據庫事務的概念.某些情況下,一個Session之內可能包含多個Transaction對象.雖然事務操作是可選的,但是所有持久化操作都應該在事務管理下進行,即使是只讀操作.
          連接提供者:ConnectionProvider,是生成jdbc連接的工廠,同時具備連接池的作用.它通過抽象將應用從底層的Datasource或DriverManager隔離開.無需直接訪問
          事務工廠:它是生成Transaction對象實例的工廠,無需直接訪問
          二.使用須知;
          1.配置文件
          ①hibernate.properties
            Configuration cfg=new Configuration()
                           .addResource("Item.hbm.xml")
                           .addResource("Bid.hbm.xml");
            還可以
            Configuration cfg=new Configuration()
               .addClass(lee.Item.Class)
                           .addClass(lee.Bid.Class);
            這種可以讓Hibernate自動根據持久化類來搜索配置文件,但是應保證配置文件和pojo同一目錄下
          ②使用 hibernate.cfg.xml
            這種方法可以直接使用
           Configuration cfg=new Configuration().configure()
          ③使用Configuration cfg=new Configuration().addClass("").setProperty("","")
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
          <hibernate-configuration>
          <session-factory>
           <property name="hibernate.connection.driver_class">
            com.mysql.jdbc.Driver
           </property>
           <property name="hibernate.connection.password">32147</property>
           <property name="hibernate.connection.url">
            jdbc:mysql://localhost:3306/hibernate
           </property>
           <property name="hibernate.connection.username">root</property>
           <property name="hibernate.dialect">
            org.hibernate.dialect.MySQLDialect
           </property>
           <property name="show_sql">true</property>
           <property name="hbm2ddl.auto">update</property>
           <mapping resource="News.hbm.xml" />
          </session-factory>
          </hibernate-configuration>

          2.連接池:
          <!—連接池-->
          <property name="hibernate.c3p0.max_size">20</property>
          <property name="hibernate.c3p0.min_size">1</property>
          <property name="hibernate.c3p0.timeout">5000</property>
          <!—緩存Statement的數量-->
          <property name="hibernate.c3p0.max_statements">100</property>
          <property name="hibernate.c3p0.idle_test_period">3000</property>
          <property name="hibernate.c3p0.acquire_increment">2</property>
          <property name="hibernate.c3p0.validate">true</property>
          3.JNDI 無需hibernate自己管理數據源的時候使用,雖然使用JNDI數據源,仍然需要指定數據庫的方言,盡管數據庫方言是可選de.
          <!--配置JNDI數據源的JNDI名-->
          <property name="connection.datasource">java:comp/env/jdbc/dstest</property>
          <!—配置連接庫的方言-->
          <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
          4.其他配置屬性:
          ①hibernate.show_sql 是否輸出sql
          ②hibernate.jdbc.fetch_size  指定jdbc抓取數量的大小,
          ③hibernate.jdbc.batch_size 指定Hibernate使用JDBC批量更新的大小,它可以接受一個整數值,建議5到三十
          ④hibernate.connection.autocommit 設置是否自動提交,通常不建議打開
          ⑤hibernate.hbm2ddl.auto 設置當創建SessionFactory時,是否根據映射文件自動建立數據庫表.該屬性可以為:update create create-drop
          三.持久化類的要求:
          1.getter setter 等
          2.一個無參構造器
          3.一個標示屬性
          4.如果使用一個有public final方法的類,必須通過設置lazy="false"來禁用代理
          5.重寫equals() hashCode()方法(如果需要把pojo類的實例放入Set中的時候) 重寫時,通常建議使用業務鍵值來實現.
          6.狀態轉換:
          //////
           News n=new News();
           n.setName("");
           sess.save(n)
          //////
            News n=Session.load(News.class, new Integer(pk));
            n.setTitle("biaoti");
          ////
            News n=Session.load(News.class, new Integer(pk));
            Session.delete(n);
          四.映射文件
          1.樣本
          <?xml version="1.0"?>
          <!DOCTYPE hibernate-mapping
              PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
              "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
          <hibernate-mapping package="lee">
              <class name="Person">
                  <id name="id" column="personid">
                      <generator class="identity"/>
                  </id>
                  <property name="name"/>
                  <property name="age"/>
              </class> 
          </hibernate-mapping>
          2.主鍵生成器
          increment   對long short int數據列生成自增長主鍵
          identity     對如SQL Server,MySql等支持自增長列的數據庫且數據列的類型long int short
          sequence    對如Oracle DB2等支持sequence的數據庫且數據列的類型為long int short
          uuid        采用128-位UUID算法生成唯一的字符串主鍵
          3.映射集合屬性
          List
          <list name="schools" table="school">
          <key column="personid" not-null="true"/>
          <list-index column="list_order"/>
          <element type="string" column="school_name"/>
          </list>
          Set
          <set name="shools" table="shool">
          <key column="personid" not-null="true"/>
          <element type="string" column="school_name" not-null="true"/>
          </set>
          Bag
          <bag name="shools" table="shool">
          <key column="personid" not-null="true"/>
          <element type="string" column="school_name" not-null="true"/>
          </bag>
          Map 
          <map name="scores" table="score">
          <key column="personid" not-null="true"/>
          <map-key column="xueke" type="string"/>
          <element type="float" column="mark"/>
          </map>
          4.組件及復合主鍵映射
          ①"屬性組件"組件為pojo的一個普通屬性類 private Name name  Name(first,last)
          <component name="name" class="Name" unique="true">
          <property name="first"/>
          <property name="last"/>
          </component>
          ②"集合元素組件"組件為List等集合的一個元素 private List<School> schools 
          School(name, address)
          <list name="schools" table="school">
          <!--映射關聯外鍵-->
          .<key column="personid" not-null="true"/>
          <list-index column="list_order">
          <composite-element class="School">
          <property name="name">
          <property name="address">
          </composite-element>

          ③"屬性組件主鍵" private Name name (用name做主鍵 ,不推薦) Name(firstName,lastName)
          <composite-id name="name" class="Class">
          <!-- key-property元素確定標識屬性包含的屬性-->
          <key-property name="firstName">
          <kry-property name="lastName">
          </composite-id>
          ④"復合主鍵" private String firstName ;private String lastName;
          <composite-id>
          <!-- key-property元素映射復合主鍵的每個元素-->
          <key-property name="firstName">
          <kry-property name="lastName">
          </composite-id>
          5.關聯關系
          單向還是雙向:
          \單向還是雙向取決于對關聯端的訪問要求,從根本上來講雙向和單向是沒有區別的,舉個子如,老師找學生;學生找老師;老師找學生同時學生也找老師.單向也好,雙向也好,都指同一個關聯關系
          一對多還是多對一到底怎么看:
           
          ㈠單向關系
          1. 單向1-1
           
          1.1基于外鍵 many-to-one 相當于 property,區別是該元素映射的關聯的持久化類
          <many-to-one name="address" type ="Address" column="addressid" unique="true" />
          1.2基于外鍵 有連接表的 這里用join顯式確定連接表
          <join table="join_table">
          <key column="personid"/>
          <many-to-one name="address" unique="true"/>
          </join>
          1.3基于主鍵
          <id name="personid">
          <generator class="foreign">
          <param name="property">address</param>
          </generator> </id>
          <!--constrainted表明該類的主鍵根據關聯類生成-->
          <one-to-one name="address" constrained="true"/> 
          2.單向N-1
           
          2.1 無連接表
          <many-to-one name="address" column="addressid"/>
          2.2有連接表
          <join table="join_table">
          <key column="personid"/>
          <many-to-one name="address" />
          </join>
          3.單向1-N
           
          3.1 無連接表的 one-to-many和element類似
          <set name="address">
          <key column="personid"/>
          <one-to-many class="Address"/>
          </set>
          3.2 有連接表 many-to-many 表明要N-N或1-N,unique=true表明這里是1-N關系
          <set name="address">
          <key column="personid"/>
          <many-to-many class="Address" unique="true"/>
          </set>

           

          4.單向N-N


          這與單向1-N沒有區別,配置文件只需將unique="true"改為unique="false"或去掉
          <set name="address">
          <key column="personid"/>
          <many-to-many class="Address" unique="false"/>
          </set>
          ㈡雙向關系
          1.雙向1-N
           
          1.1無連接表
          注意: 兩個配置文件都應該指定外鍵,且不應省略.
          Person.hbm.xml
          <set name="address">
          <key column="personid"/>
          <one-to-many class="Address"/>
          </set>
          Address.hbm.xml
          <many-to-one name="person" column="personid">
          1.2有連接表
          注意:1.兩遍確定連接表的table屬性值應該相等,且不可以省略
               2.兩遍都制定了兩個外鍵列,一定要保證兩邊映射文件的外鍵列名對應相同
          Person.hbm.xml
          <set name="address" table="personAddress">
          <key column="personid"/>
          <many-to-many class="Address" unique="true" column="addressid"/>
          </set>
          Address.hbm.xml  //optional表示是否可選
          <join table="PersonAddress" inverse="true" optional="true">
          <many-to-one name="person" column="personid">
          2.雙向N-N
           
          雙向多對多的兩邊都要指定table 和外鍵列的列名 且它們都要一致
          Person.hbm.xml
          <set name="addresses" table="jointable">
          <key column="personid"/>
          <many-to-many column="addressid" class="Address"/>
          </set>
          Address
          <set name="persons" table="jointable">
          <key column="addressid"/>
          <many-to-many column="personid" class="Person">
          </set>
          3.雙向1-1
           
          3.1基于外鍵
          注意: 基于外鍵的雙向1-1,外鍵可以放在任一端,當然需要增加many-to-one屬性.而另一端就需要使用one-to-one元素,為了不讓系統再為本表增加一列,而是使用外鍵關聯可以用property-ref屬性引用關聯類的自身屬性.注意:one-to-one映射關聯屬性,不會創建外鍵列.
          Person.hbm.xml
          <one-to-one name="address" property-ref="person"/>
          Address.hbm.xml
          <many-to-one name="person" class="Person" unique="true"/>
          3.2基于主鍵
          Person.hbm.xml
          <one-to-one name="address" />
          Address.hbm.xml
          <generator class="foreign">
          <param name="property">address</param></generator>
          < one-to-one name="person" constrained="true"/>
          3.3基于外鍵的強制連接表(不推薦)
          Person.hbm.xml
          <join table="personaddress" optional="true">
          <key column="personid" unique="true"/>
          <many-to-one name="address" column="addressid" not-null="true" unique="true">
          </join>
          Address.hbm.xml
          <join table="personaddress" optional="true" inverse="true">
          <key column=" addressid" unique="true"/>
          <many-to-one name="person" column=" personid" not-null="true" unique="true"/>
          </join>
          length 屬性值得是字符個數,這個與數據庫是不同的

           

           


           

          posted on 2009-03-29 20:47 star11th 閱讀(1033) 評論(0)  編輯  收藏 所屬分類: Hibernate學習手記


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


          網站導航:
           
          <2009年3月>
          22232425262728
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          導航

          統計

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 饶阳县| 久治县| 祁连县| 宁明县| 江源县| 诸暨市| 陇川县| 乃东县| 昭苏县| 澎湖县| 温泉县| 资中县| 全州县| 平远县| 阳谷县| 黎城县| 乌什县| 广水市| 滁州市| 稻城县| 九江县| 高尔夫| 获嘉县| 福泉市| 永泰县| 华蓥市| 克山县| 榕江县| 保德县| 博爱县| 勐海县| 长治市| 庐江县| 泾阳县| 尉犁县| 邮箱| 津南区| 临西县| 浦东新区| 饶河县| 交城县|