Hibernate基礎映射

來源:互聯網
上載者:User

標籤:hibernate

   在說Hibernate映射前,我們先來瞭解下對象關係映射 ORM。ORM的實現思想就是將關聯式資料庫中表的資料對應成對象,以對象的形式展現。這樣開發人員就可以把對資料庫的操作轉化為對這些對象的操作。我們來看一張圖

通過該圖,我們可以看出業務實體,在資料庫中表現為關係資料,而在記憶體中表現為對象。應用程式處理對象很容易,但是很難處理關係資料。ORM做到了關係資料和對象資料之間的映射,可以通過映射關係自動產生SQL語句,在商務邏輯層和資料層之間充當橋樑。

Hibernate映射

 

Hibernate檔案
  1.  映射類(*.java):它是描述資料庫表的結構,表中的欄位在類中被描述成屬性,將來就可以實現把表中的記錄映射成為該類的對象了。
  2.  對應檔(*.hbm.xml):它是指定資料庫表和映射類之間的關係,包括映射類和資料庫表的對應關係、表欄位和類屬性類型的對應關係以及表欄位和類屬性名稱的對應關係等。
  3.  資料庫設定檔(*.properties/*.cfg.xml):它是指定與資料庫連接時需要的串連資訊,比如串連哪種資料庫、登入資料庫的使用者名稱、登入密碼以及連接字串等。當然還可以把映射類的地址映射資訊放在這裡。

 

基本映射:

具體看操作

1映射實體類

//預設空建構函式的重要性public class User {public User() {// TODO Auto-generated constructor stub}public User(String id,String name){this.id=id;this.name=name;}private String id;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public Date getCreateTime() {return createTime;}public void setCreateTime(Date createTime) {this.createTime = createTime;}public Date getExprieTime() {return exprieTime;}public void setExprieTime(Date exprieTime) {this.exprieTime = exprieTime;}private String name;private String password;private Date createTime;private Date exprieTime;}

實體類的設計原則:

* 實現無參的預設的建構函式

* 提供一個標識

*建議不要使用final修飾實體類(因為採用load延時載入資料的時候會繼承實體類組建代理程式對象)

*建議為實體類產生getter和setter方法(如果不使用,需要用屬性field標識)

2對應檔User.hbm.xml

<?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> <!--1、class和table的映射,name屬性是實體名,table屬性是表名(table可省略,則name即是映射的表名)--> <class name="com.bjpowernode.hibernate.User"><!-2、主鍵映射,name屬性是實體類的標識符屬性,對應table的主鍵,即用column表示(column同樣可省略)-->  <id name="id" access="field"><!--主鍵產生器,class屬性工作表示建置原則,根據不同的需求選擇-->   <generator class="uuid"/></id> <!--3、其他屬性的映射 property-->  <property name="name" length="40" unique="true" /><property name="password"/><property name="createTime"/><property name="exprieTime"/><filter name="testFiltere"  condition="id < :myid"></filter></class></hibernate-mapping>

3hibernate.cfg.xml設定檔

<!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/hibernast_test</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">hanhan</property><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><!--列印sql--><property name="hibernate.show_sql">true</property><!--在沒有表的時候,建立sessionfactroy 時,就會去建立表(update的方式,不刪除原有資料)--><property name="hibernate.hbm2ddl.auto">update</property><mapping  resource="com/bjpowernode/hibernate/User.hbm.xml"/></session-factory></hibernate-configuration>
Hibernate的主鍵建置原則種類

手動:Assigned

Hibernate主動:uuid

資料庫互動:

需要和資料庫互動以產生id的:guid、identity、sequence、native、foreign

說明:需要和資料庫互動產生,需要經過一次查詢才能產生

Guid,identity:MySQL,SQLserver的產生方式

sequence:Oracle,db2的產生方式,自增序列

native:identity+sequence,跨平台

foreign:只適用基於共用主鍵的一對一關聯映射的時候使用。即一個對象的主鍵是參照的另一張表的主鍵產生的。 

總結:

Hibernate的基本映射:重點是對主鍵建置原則的認識,根據不同資料庫選擇不同的方式,重要理解。

Hibernate基礎映射

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.