| 組件 | 版本/說明 |
|---|---|
| IDE | JBuilder X (JDK 1.4 默認) |
| 資料庫 | MS SQL Server 2000 |
| JDBC 驅動 | JNetDirect JSQL Connect 3.0 (JDBC 3.0,需註冊下載試用版) |
| ORM 框架 | Hibernate 2.1.2 (當時最新) |
關鍵提示: Hibernate 官方配置文件明確標註 Microsoft JDBC 驅動「不推薦」,故選用 JSQL Driver。JDBC 3.0 需 JDK 1.4+,JBuilder X 滿足條件。
hibernate/lib/*.jar 及 hibernate/hibernate2.jar 全部加入JNetDirect/JSQLConnect/JDBC_3.0_Driver/JSQLConnect.jar.hbm.xml 複製到 classes 目錄在專案 src 目錄下放入:
hibernate.properties (來自 Hibernate 源碼 src/ 目錄)log4j.propertieshibernate.properties 針對 SQL Server + JSQL Driver# 註釋掉默認 HypersonicSQL 配置
# hibernate.dialect net.sf.hibernate.dialect.HSQLDialect
# hibernate.connection.driver_class org.hsqldb.jdbcDriver
# hibernate.connection.username sa
# hibernate.connection.password
# hibernate.connection.url jdbc:hsqldb:hsql://localhost
# 啟用 SQL Server 配置
hibernate.dialect net.sf.hibernate.dialect.SQLServerDialect
hibernate.connection.username sa
hibernate.connection.password sa
# JSQL Driver 配置
hibernate.connection.driver_class com.jnetdirect.jsql.JSQLDriver
hibernate.connection.url jdbc:JSQLConnect://yuj/testhi
參數說明: yuj 為資料庫伺服器機器名,testhi 為資料庫名,帳號/密碼均為 sa。
testhibernate.Personpackage testhibernate;
public class Person {
private String id;
private String name;
private String address;
// Getter/Setter 省略
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 getAddress() { return address; }
public void setAddress(String address) { this.address = address; }
}
Person.hbm.xml (同目錄)<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE hibernate-mapping SYSTEM
"https://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="testhibernate.Person">
<!-- UUID 十六進制主鍵生成策略 -->
<id name="id" unsaved-value="null">
<generator class="uuid.hex"/>
</id>
<!-- 屬性默認映射同名列 -->
<property name="name"/>
<property name="address"/>
</class>
</hibernate-mapping>
package testhibernate;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
public class Client1 {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration().addClass(Person.class);
SchemaExport dbExport = new SchemaExport(conf);
dbExport.setOutputFile("sql.txt"); // 導出 DDL 到文件
dbExport.create(true, true); // drop + create
// 警告:後續運行請勿再執行此類,否則會刪除已有表數據
}
}
package testhibernate;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;
public class Client2 {
private static SessionFactory sessionFactory;
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration().addClass(Person.class);
sessionFactory = conf.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Person p1 = new Person();
p1.setName("john");
p1.setAddress("上海");
Person p2 = new Person();
p2.setName("zhaoyh");
p2.setAddress("上海");
session.save(p1); // 持久化:此時已可在資料庫查到
session.save(p2);
tx.commit();
session.close();
}
}
執行 Client1 → 執行 Client2 → SQL Server 企業管理器查看 Person 表,新增 2 條記錄,主鍵為 UUID 十六進制字串。初步整合成功。
| 項目 | Hibernate 2.1 (本文) | 現代版本 (5.x/6.x) |
|---|---|---|
| 配置方式 | hibernate.properties + .hbm.xml | application.yml + JPA 註解 (@Entity) |
| Session API | net.sf.hibernate.Session | org.hibernate.Session / EntityManager |
| 主鍵生成 | uuid.hex | @GeneratedValue(generator = "uuid2") + @GenericGenerator |
| 事務管理 | 手動 Transaction API | @Transactional 聲明式 |
| Schema 管理 | SchemaExport 手動 | spring.jpa.hibernate.ddl-auto=update |
| 查詢 API | HQL + Session.find() | JPQL/CriteriaBuilder + Repository |
本文展示了 Hibernate 2.x 時代的經典整合流程:JBuilder 專案庫管理、JDBC 驅動選型、屬性配置、POJO/映射文件編寫、SchemaExport 建表、Session/Transaction 手動持久化。雖 API 已迭代數代,但核心概念(映射元數據、Session 生命週期、事務邊界、方言抽象)始終貫穿至今。理解歷史版本有助於深層掌握現代 JPA/Hibernate 設計哲學。