1. 程式人生 > >Hibernate配置檔案

Hibernate配置檔案

通過配置檔案(hibernate.propertieshibernate.cfg.xml)和對映檔案(.hbm.xml)把java物件或持久化物件(Persistent Object,PO)對映到資料庫中的資料表,然後通過操作PO,對資料表中的資料進行增,刪,改,查等操作.Hibernate配置檔案主要用來配置資料庫連線引數,例如資料庫的驅動程式URL,使用者名稱,密碼等。    兩種格式:hibernate.propertieshibernate.cfg.xml    一般情況下,hibernate.cfg.xmlHibernate的預設配置檔案。

1 hibernate.properties

Hibernate-3.1etc目錄下有一個hibernate.properties模板。該配置模板檔案定義了連線各種資料庫所需要的引數。需要使用hibernate.properties時,修改該模板即可。該模板檔案中每一個配置項前面的“#”是註釋符號。2hibernate.cfg.xml除了要定義Hibernate的各項屬性,還要定義程式中用的對映檔案(xxx.hbm.xml)。一般情況下是hibernate的預設配置檔案。

hibernate.cfg.xml配置檔案

<?xmlversion='1.0'encoding='UTF-8'?>

<!DOCTYPEhibernate-configuration

PUBLIC

"-//Hibernate/HibernateConfiguration DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                  -->

<hibernate-configuration>

<session-factory>

<propertyname="connection.username">sa</property

>

<propertyname="connection.url">

jdbc:sqlserver://localhost:1433;databaseName=hbDB

</property>

<propertyname="dialect">

org.hibernate.dialect.SQLServerDialect

</property>

<propertyname="myeclipse.connection.profile">hbDB</property>

<propertyname="connection.password">sa</property>

<propertyname="connection.driver_class">

com.microsoft.sqlserver.jdbc.SQLServerDriver

</property>

<propertyname="show_sql">true</property>

<property key="hibernate.connection.autocommit">false</property>

<mappingresource="cn/Customers.hbm.xml"/>

</session-factory>

</hibernate-configuration>

hibernate.cfg.xml配置檔案屬性

屬性

說明

connection.username

指定連線資料庫的使用者名稱

connection.url

指定連線資料庫的URL

dialect

用於配置hibernate使用的不同的資料型別。如Oracle、DB2、MS SQL Server、MySQL等。

myeclipse.connection.profile

資料庫的配置檔案

connection.password

指定連線資料庫的密碼

connection.driver_class

指定資料庫的驅動程式

show_sql

若為true,表示程式在執行時,在控制檯輸出SQL語句。

mapping

資料庫表和實體的對映資訊要在另外的對映檔案中定義,但要在配置檔案中宣告。

實體類

Customers實體類要實現implemetns java.io.Serializable介面。

packagecn;

publicclassCustomersimplementsjava.io.Serializable {

privateIntegerid;

privateStringname;

privateIntegerage;

publicInteger getAge() {

returnage;

}

publicvoidsetAge(Integer age) {

this.age= age;

}

publicInteger getId() {

returnid;

}

publicvoidsetId(Integer id) {

 this.id= id;

}

publicString getName() {

returnname;

}

publicvoidsetName(String name) {

this.name= name;

}

}

對映檔案

另外,還要告訴Hibernate實體類Customers對映到資料庫的哪個表,以及哪個屬性對應資料庫中的哪個欄位。

Customers.hbm.xml

<?xmlversion="1.0"encoding="utf-8"?>

<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!--

Mappingfile autogenerated by MyEclipse Persistence Tools

-->

<hibernate-mapping>

<classname="cn.Customers"table="Customers"schema="dbo"catalog="hbDB">

<idname="id"type="java.lang.Integer">

<columnname="ID"/>

<generatorclass="identity"/>

</id>

<propertyname="name"type="java.lang.String">

<columnname="Name"length="20"not-null="true"/>

</property>

<propertyname="age"type="java.lang.Integer">

      <columnname="Age"not-null="true"/>

</property>

</class>

</hibernate-mapping>

1<classname="cn.Customers"table="Customers"schema="dbo"catalog="hbDB"通訊dynamic-update="false"dynamic-insert="false"mutable="true"></class>

每一個<class>節點配置一個實體類的對映資訊。

(1name屬性:對應指定持久化實體類:Customers

(2table屬性:對應資料庫表的名字。

(3schema屬性:

(4catalog屬性:

(5)dynamic-update:若為false,表示當儲存一個例項時,會動態生成update語句,只有該例項中的欄位取值變化,才會把它包含到insert語句中。預設值為true。

(6)dynamic-insert:若為false,表示當插入一個例項時,會動態生成inset語句,只有該例項中的欄位取值不為null時,才會把它包含到insert語句中。預設值為true。

(7)mutable:若為false,等價於所有的<property>元素的update屬性為false,表示整個例項不能被更新。預設為true。

2<idname="id"type="java.lang.Integer">

<columnname="ID"/>

<generatorclass="identity"/>

</id>

<id>節點用於定義實體的標識屬性(對應資料庫表的主鍵)

(1)name屬性:對應實體類的屬性。

(2)type屬性:指定hibernate對映的資料型別。對應Java資料型別。

(3)column屬性:通過name屬性指定其對應的資料庫表的主鍵。

(4)generator屬性:指定主鍵的生成策略。

3<propertyname="name"type="java.lang.String"update="true"insert="true">

<columnname="Name"length="20"not-null="true"/>

</property>

與<id>節點類似,但不能包括<generator>節點。

(1)name屬性:指定持久化類的屬性。

(2)type屬性:指定hibernate對映的資料型別。對應Java資料型別。

(3)column屬性:通過name屬性指定其對應的資料庫表的欄位名。

(4)length屬性:通過name屬性指定其對應的資料庫表的欄位名的長度。

(5)not-null屬性:通過name屬性指定其對應的資料庫表的欄位名是否為空。

(6)update:若為false則在更新資料時,不會更新該欄位。預設為true.即更新。

(7)insert:若為false則在插入資料時,不會插入該欄位。預設為true.即插入。

<column>元素的屬性

說 

Name

指定持久化類的屬性。

length

指定段長度

not-null

指明欄位是否為空。預設為false

unique

指明欄位是否具有唯一索引。預設為false

unique-key

為多個欄位設定惟一約束

foreign-key

指明一個外來鍵的名字,它是為關聯生成的。

sql-type

設定欄位的SQL型別。

check

設定SQL檢查約束。

在做唯一外來鍵關聯的時候都要用到property-ref="readTypeName"

many-to-one加個屬性,這個屬性預設是指向主鍵的,把它改為主表的關聯欄位