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

JBuilder X + SQL Server + Hibernate 2.1 開發實戰入門

     編輯:Java 程式設計入門

環境與依賴基線

組件版本/說明
IDEJBuilder 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 滿足條件。

JBuilder 專案庫配置

  1. 建立庫 `hibernate_full`:將 hibernate/lib/*.jar 及 hibernate/hibernate2.jar 全部加入
  2. 建立庫 `JSQL3`:加入 JNetDirect/JSQLConnect/JDBC_3.0_Driver/JSQLConnect.jar
  3. 專案屬性 → Required Libraries:勾選上述兩個庫
  4. 專案屬性 → Build → Resource:選中 XML 文件 → 「Copy」,確保編譯時 .hbm.xml 複製到 classes 目錄

配置文件準備與修改

1. 複製模板文件

在專案 src 目錄下放入:

  • hibernate.properties (來自 Hibernate 源碼 src/ 目錄)
  • log4j.properties

2. 修改 hibernate.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。

實體類與映射文件

1. POJO 實體類 testhibernate.Person

package 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; }
}

2. 映射文件 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>

客戶端程式:建表與數據操作

1. Client1.java —— 僅建表 (僅執行一次)

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
        // 警告:後續運行請勿再執行此類,否則會刪除已有表數據
    }
}

2. Client2.java —— 數據持久化操作

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 5/6 + Spring Boot)

項目Hibernate 2.1 (本文)現代版本 (5.x/6.x)
配置方式hibernate.properties + .hbm.xmlapplication.yml + JPA 註解 (@Entity)
Session APInet.sf.hibernate.Sessionorg.hibernate.Session / EntityManager
主鍵生成uuid.hex@GeneratedValue(generator = "uuid2") + @GenericGenerator
事務管理手動 Transaction API@Transactional 聲明式
Schema 管理SchemaExport 手動spring.jpa.hibernate.ddl-auto=update
查詢 APIHQL + Session.find()JPQL/CriteriaBuilder + Repository

總結

本文展示了 Hibernate 2.x 時代的經典整合流程:JBuilder 專案庫管理、JDBC 驅動選型、屬性配置、POJO/映射文件編寫、SchemaExport 建表、Session/Transaction 手動持久化。雖 API 已迭代數代,但核心概念(映射元數據、Session 生命週期、事務邊界、方言抽象)始終貫穿至今。理解歷史版本有助於深層掌握現代 JPA/Hibernate 設計哲學。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved