首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> JSP 程式設計 >> 關於JSP

JBuilder X + SQL Server + Hibernate 2.1 開發環境搭建全記錄

     編輯:關於JSP

記錄在 JBuilder X (JDK 1.4) 下,使用 JSQL Connect JDBC 3.0 驅動連接 MS SQL Server 2000,整合 Hibernate 2.1.2 的完整配置步驟、映射檔編寫、Schema 自動生成與 CRUD 代碼範例,供同類技術棧開發者參考。

環境與準備

  • IDE: JBuilder X (內建 JDK 1.4,支援 JDBC 3.0)
  • 資料庫: MS SQL Server 2000
  • JDBC 驅動: JSQL Connect (JDBC 3.0),com.jnetdirect.jsql.JSQLDriver (官網註冊下載試用版)
  • Hibernate: 2.1.2 (SourceForge 下載)
  • JBuilder 庫配置:
    • hibernate_full:含 hibernate/lib/*.jar + hibernate2.jar
    • JSQL3:JSQLConnect.jar

專案建立與配置

  1. 新建專案 testhibernate,加入上述兩個庫。
  2. Project Properties → Build → Resource 勾選 .xml 複製到輸出目錄 (使映射檔隨編譯部署)。
  3. 建立 src 目錄,複製 hibernate.properties、log4j.properties 至其中。
  4. 修改 hibernate.properties 針對 SQL Server + JSQL Driver:
# 註釋掉預設 HSQLDB 區段
#hibernate.dialect net.sf.hibernate.dialect.HSQLDialect
#hibernate.connection.driver_class org.hsqldb.jdbcDriver
...

# 啟用 SQL Server + JSQL Driver
hibernate.dialect net.sf.hibernate.dialect.SQLServerDialect
hibernate.connection.username sa
hibernate.connection.password sa
hibernate.connection.driver_class com.jnetdirect.jsql.JSQLDriver
hibernate.connection.url jdbc:JSQLConnect://yuj/testhi  # yuj=伺服器機器名, testhi=資料庫名

領域模型與映射

POJO: testhibernate.Person


public class Person {
    private String id, name, address;
    // 標準 getter/setter
}

映射檔: 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">
    <id name="id" unsaved-value="null">
      <generator class="uuid.hex"/> <!-- UUID 主鍵 -->
    </id>
    <property name="name"/>
    <property name="address"/>
  </class>
</hibernate-mapping>

客戶端代碼:Schema 匯出與資料持久化

Client1: 僅執行一次,建立資料表


Configuration conf = new Configuration().addClass(Person.class);
SchemaExport dbExport = new SchemaExport(conf);
dbExport.setOutputFile("sql.txt"); // 輸出 DDL 供參考
dbExport.create(true, true); // drop + create

Client2: 正常業務持久化


Configuration conf = new Configuration().addClass(Person.class);
SessionFactory sf = conf.buildSessionFactory();
Session s = sf.openSession();
Transaction tx = s.beginTransaction();

Person p1 = new Person(); p1.setName("john"); p1.setAddress("上海");
Person p2 = new Person(); p2.setName("zhaoyh"); p2.setAddress("上海");

s.save(p1); s.save(p2); // 持久化
tx.commit();
s.close();

驗證與後續

執行 Client1 建表 (查看 sql.txt 確認 DDL),再執行 Client2 插入資料,SQL Server Management Studio 查詢 Person 表確認兩筆記錄。後續可深入探索:HQL 查詢、關聯映射、二級快取、事務邊界聲明、Session 生命週期管理等進階主題。

關鍵提醒

  • JSQL Driver 非 Microsoft 官方驅動,但 Hibernate 官方文檔標註 Microsoft Driver (not recommended!),JSQL 穩定性較好。
  • 映射檔 .hbm.xml 必須隨 Class 輸出 (JBuilder Resource 複製設定),否則 Configuration.addClass() 找不到映射。
  • SchemaExport.create(true, true) 會**刪除重建**表,**僅限初始化/測試**使用,生產環境嚴禁。
  • Hibernate 2.x 使用 net.sf.hibernate 包名,3.x+ 遷移為 org.hibernate,遷移時需注意。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved