一、下載下傳
官網位址:http://hibernate.org/
Hibernate下有ORM(關系型資料庫)、OGM(NoSQL資料庫)、Search(對象全文檢索)、Validator的工具。
ORM 5.2下載下傳位址為:http://hibernate.org/orm/releases/5.2/
使用時,将Hibernate Lib下的required Jar包引入即可,其他進階功能可繼續引用相關包。
二、工具
Hibernate通過XML将對象映射到資料庫表,可以通過Hibernate Tools自動生成XML、POJO等。
除Hibernate Tools外,也可使用xdoclet來生成XML,xdoclet位址:http://xdoclet.sourceforge.net/index.html
Hibernate Tools下載下傳位址:http://tools.jboss.org/downloads/
三、關鍵檔案
- Hibernate Configuration File(cfg.xml)
配置資料庫連接配接資訊(JDBC)以及注冊映射:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--資料庫連接配接配置-->
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:sqlserver://ipaddress:port;DatabaseName=dbname;SelectMethod=cursor</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.default_catalog">dbname</property>
<property name="hibernate.default_schema">dbo</property>
<!--資料庫方言-->
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<!--加載所有映射-->
<mapping resource="lims/hibernate/LIMS_TESTS.hbm.xml"/>
</session-factory>
</hibernate-configuration>
- Hibernate Console Configuration
當使用File->New後,竟然發現找不到這個檔案,正确的打開方式為:Run->Run Configurations。

實質上,Hibernate Configuration File(cfg.xml)是與項目相關的,例如包括了該項目使用的資料庫連接配接資訊,而Hibernate Console Configuration則是在項目開發過程中工具使用相關的,即可通過它來生成代碼。
配置時,選擇好項目及其配置檔案路徑,在Classpath中引入該項目所用的資料庫的JDBC驅動。
- Hibernate Reverse Engineering File(Revenge.xml)
該檔案為資料庫逆向工程配置檔案,如果先建立資料庫表,則可通過已有的資料庫表來建立POJO和ORM XML等檔案。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >
<hibernate-reverse-engineering>
<table-filter match-catalog="dbname" match-schema="dbo" match-name="tablename"/>
</hibernate-reverse-engineering>
根據該配置,使用Hibernate Tools可生成POJO和ORM XML等代碼:
使用Hibernate Tools生成代碼時,指定Hibernate Reverse Engineering File:
- Hibernate XML Mapping File(hbm.xml)
Hibernate XML Mapping File是關鍵,各種映射都通過其實作。
<?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="package name">
<class name="class name" table="table name">
<!-- 主鍵 ,id,映射-->
<id name="property name" column="column name">
<!-- <generator class="native"/> -->
</id>
<!-- 非主鍵,property,映射 -->
<property name="property name" column="column name" ></property>
</class>
</hibernate-mapping>
一個簡單的示例如上所示,配置包名、類名、類屬性和表字段映射即可。
複雜的應用,可以進一步配置字段的資料類型、長度、唯一限制等。
ID可配置自動生成方式,自增序列值、GUID值等,或可不配置,此時在業務代碼中根據業務規則生成并指派到Java對象中。
聯合主鍵時,除了實體類外,需建立ID類,id替換為composite-id定義。
外鍵通過many-to-one配置。
四、示例代碼
// 對象
tests test = new tests();
test.setTestcode(1);
test.setTestname("test");
// 擷取加載配置檔案的管理類對象
Configuration config = new Configuration();
// 預設加載src/hibenrate.cfg.xml檔案
config.configure();
// 建立session的工廠對象
SessionFactory sf = config.buildSessionFactory();
// 建立session (代表一個會話,與資料庫連接配接的會話)
Session session = sf.openSession();
// 開啟事務
Transaction tx = session.beginTransaction();
//儲存-資料庫
session.save(test);
// 送出事務
tx.commit();
// 關閉
session.close();
sf.close();
作者:馬洪彪
出處:http://www.cnblogs.com/mahongbiao/
本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,否則保留追究法律責任的權利。