hibernate學習從XML入手,hibernatexml入手

來源:互聯網
上載者:User

hibernate學習從XML入手,hibernatexml入手
上篇部落格簡單介紹了構建Hibernate環境,但是還沒有開始進入持久化設計階段。這篇部落格就開始hibernate的設計工作。

   看hibernate原理圖如下:hibernate的設計包含三步:實體類設計,實體類對應檔編寫,hibernate設定檔編寫。

      

    
     一、實體類設計
    
        以前的面向過程編程,和資料庫打交道,我們使用datatable ,dataset等之類的,逐步的開始接觸物件導向設計,開始運用物件導向的思想來封裝自己的代碼。物件導向的設計理念,一切皆物件導向。所以實體類設計是物件導向的核心。
        通過實體在商務邏輯中的傳遞,才得以將“需求資料”儲存到資料庫中,項目中的實體是將使用者需求和資料(database)串連起來的橋樑。
        
        關於物件導向實體設計,這裡不是重點要說的,hibernate的實體設計就是我們平常的實體設計。

        看unit的一個執行個體:


      

using System;using System.Collections;namespace UIEntity{    public class UnitEntity    {        protected int _unitId;        /// <summary>        /// </summary>        public virtual int unitId        {            get            {                return _unitId;            }            set            {                _unitId = value;            }        }        protected String _unitName;        /// <summary>        /// </summary>        public virtual String unitName        {            get            {                return _unitName;            }            set            {                if (value != null && value.Length > 254)                    throw new ArgumentOutOfRangeException("Invalid value for unitName", value, value.ToString());                _unitName = value;            }        }        protected String _unitDesc;        /// <summary>        /// </summary>        public virtual String unitDesc        {            get            {                return _unitDesc;            }            set            {                if (value != null && value.Length > 254)                    throw new ArgumentOutOfRangeException("Invalid value for unitDesc", value, value.ToString());                _unitDesc = value;            }        }        protected String _address;        /// <summary>        /// </summary>        public virtual String address        {            get            {                return _address;            }            set            {                if (value != null && value.Length > 254)                    throw new ArgumentOutOfRangeException("Invalid value for address", value, value.ToString());                _address = value;            }        }        protected IList _personEntities;        public virtual  IList personEntities        {            get            {                if(_personEntities==null)                {                    _personEntities=new ArrayList();                }                return _personEntities;            }            set            {                _personEntities = value;            }                   }    }}

二、實體類對應檔的編寫
    
        實體類對應檔以“hbm.xml"結尾。區別於設定檔“cfg.xml”。
        為什麼會出現對應檔,上篇部落格也提到:hibernate之所以可以將實體類持久化到資料庫,這些實體對應檔是大功臣,hibernate通過這些“約定”檔案來讀寫實體資訊,然後寫入的資料庫。

       
        原則:實體類對應檔和要映射到資料庫的實體類是對應的。 
        
        也就是說:如果需要把某個實體類持久化到資料庫中,那麼這個類必定存在一個對應檔;但是實體類和對應檔不是一一對應的,多個實體類可以通過一個對應檔來持久化到資料庫。
        
        表現形式
            一個實體類一個對應檔
            多個實體類,一個對應檔       

       1、一個實體用一個對應檔來表示:針對實體類Unit,我們需要一個Unit.hbm.xml。

      

<?xml version="1.0" encoding="utf-8" ?><hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"><class name="UIEntity.UnitEntity, UIEntity" table="UnitEntity"><id name="unitId" type="Int32" unsaved-value="null"><column name="unitId" length="4"  not-null="true" unique="true"/><generator class="native" /></id><property name="unitName" type="String"><column name="unitName" length="254"  not-null="false"/></property><property name="unitDesc" type="String"><column name="unitDesc" length="254"  not-null="false"/></property><property name="address" type="String"><column name="address" length="254"  not-null="false"/></property><bag name="personEntities" inverse="true" lazy="false" cascade="all-delete-orphan"><key column="unitId"/><one-to-many class="UIEntity.PersonEntity, UIEntity"/></bag></class></hibernate-mapping>
大多數情況,我們都使用這種一對一的形式來表示,但是繼承映射中,我們可以有另一種形式(繼承映射有三種形式,這裡不做重點介紹,只是介紹同一個hbm.xml檔案對應多個實體類的例子)。

2、多個實體類,一個對應檔

    背景:
有一個動物的抽象類別Animal,Animal下有兩個子類:pig,bird。我們目的是把具體類映射成資料表。因為Animal是一個抽象的類,所以我們不映射它,映射兩個子類到兩個資料表中。
在這個xml中我們通過“abstract”來標誌Animal這個類不需要映射成資料庫表,而bird,pig則需要映射成資料庫表的寫法
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">         <hibernate-mapping package="cassie.Hibernate"><class name="Animal"  abstract="true" ><id name="id" column="id"><generator class="assigned"/></id><property name="name"/><property name="sex"/><union-subclass name="Pig"  table="t_pig"><property name="weight"/></union-subclass><union-subclass name="Bird" table="t_bird"><property name="height"/></union-subclass></class></hibernate-mapping> 

三、hibernate設定檔的編寫,我們在上篇部落格中提到過就是cfg.xml檔案的編寫。
    
        Hibernate.cfg.xml可包含的內容有很多,主要分為三部分:
        
        1、資料庫相關的————必要資訊

            資料庫驅動:hibernate.connection.driver_class
            資料庫:hibernate.connection.url
            使用者名稱:hibernate.connection.username
            密碼:hibernate.connection.password
            資料庫方言:hibernate.dialect
            
        2、常用配置資訊

            <!-- 配置緩衝供應商 -->
            <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
            
            <!-- 啟用二級緩衝,這也是它的預設配置 -->
            <property name="hibernate.cache.use_second_level_cache">true</property>
            …………
            
        3、對應檔資訊——必要
        
            <mapping resource="cassie/Hibernate/CollectionList.hbm.xml"/>
            
            有了對應檔地址,這個hibernate的配置才算完整,不然,我們的實體類一個都映射不到。
            
        常用執行個體如下:

<span style="font-family:KaiTi_GB2312;font-size:18px;"><!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.url">jdbc:mysql://localhost:3306/Hibernate_collection</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">123</property><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect </property><property name="hibernate.show_sql">true</property><mapping resource="cassie/Hibernate/CollectionList.hbm.xml"/></session-factory></hibernate-configuration></span>

       以上就是使用hibernate的主要三步任務。其實寫完部落格,覺得只有兩步,就是兩中xml檔案的編寫。因為實體類無論如何都是要設計的。而這兩種xml檔案的作用,就是hibernate中最為明顯的規則:約定。Xml的編寫是遵循了hibernate已制定好的規範進行編寫,才能被識別,被映射到資料庫。到此是對hibernate xml檔案的認識,從宏觀上掌握明白hibernate的xml跟以前的用過的xml的區別:就是多幾個有關hibernate的標籤而已。   

下篇部落格介紹這些xml檔案中包含元素的含義。



hibernate xml配置 現在還有必要學?

我現在已經工作了,分享一下經驗
你應該以Annotation為主,但是xml的配置還是多少要熟悉一下,因為用什麼不是你來決定的,你到的那個公司,他們用什麼配置那麼你就得回什麼配置。所以還是順便熟悉一下為好。
 
java ee方向那多的東西要學 比如jsp,servlet,spring,struct,hibernate,XML等等,該從何學起?

JSP-JavaBean-XML-Servlet-JSTL-EL-JTL-Struts-Hibernate-Spring
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.