用Hibernate Tools生成Hibernate Mapping映射文件

Eclipse中要集成安裝Hibernate Tools組件html

若是沒有,請查看:Eclipse juno 中安裝 JBoss Tools,集成Hibernatejava

一.肯定環境:

1.Maven3.0.5數據庫

2.Eclipse Juno,集成Jboss/Hibernate Tools
session

3.Oracle10goracle

4.Hibernate3.6.5.Finalapp

5.JDK1.7.0_11maven

二.打開Hibernate Perspective視圖層

Eclipse中打開「Hibernate Perspective「。post

  選擇「Windows」 >> 「Open Perspective」 >> 「Others…」 , 選擇 「Hibernate「。測試

三.新建Hibernate Configuration 配置

1.在Hibernate Perspective中, 右鍵,選 「Add Configuration…」,彈出對話框url

2.在 「Project」區域, 單擊 「Browse..」 選擇你本身的項目。
3.在 「Database Connection」 區域, 單擊 「New..」建立你本身的數據庫設定。


4.彈出Connection Profile中選Oracle ,並填寫「Name」後,Next

5.在彈出對話框中,選Driver,沒有的話要新加,此處新加Driver。

6.彈出對話框中選一個driver,若是出現「Unable to locate JAR/zip in file system as specified by the driver definition: ojdbc14.jar.」

就要在「JAR List」選項卡中刪除原有的驅動文件,從新加一下就行了。

刪除驅動文件從新添加,此處添加的是Maven倉庫中的jar包

完成後點擊OK

填寫完數據庫的相關鏈接後,能夠點Test Connection測試是否能夠鏈接上。

鏈接上如圖

7.Configuration file配置

店家「Setup」彈出「Setup configuration file」對話框,若是不存在.dfg.xml文件,能夠新建

上圖點擊「Create New...」後,彈出新建cfg.xml文件對話框

建立在項目目錄下的main/java/resources下,

個人是在「maven-hibernate-demo/src/main/resources」

能夠點擊next配置更具體的設置,此處咱們直接Finsh

返回前一界面後,OK。記住,此處的「Hibernate Version」選項必定要選對版本,不然反向工程不會成功。

查看Hibernate Configurations視圖

四.查看src/main/resources目錄下的剛建好的hibernate.cfg.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@yourdatabaseip:gzip</property>
        <property name="hibernate.connection.username">username</property>
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
    </session-factory>
</hibernate-configuration>

五.生成Hibernate代碼

在「Hibernate Perspective」視圖中, 點擊 「Hibernate Code Generation Configuration」

在生成代碼框中,新建configuration,並填寫Main選項卡和Exporters選項卡,

Main中須要填寫輸出路徑等,填完的以下

console configuration選擇前邊剛配置完的Hibernate Configuration,前邊的名字是「hibernate」

Output directory選擇輸出路徑

Reverse enginner from JDBC Connection勾選

Package填寫包名

reveng.xml填寫建立hibernate.reveng.xml的位置,沒有則新建。

在Exporters選擇相應要生成的代碼

能夠生成Model , mapping file (hbm) , DAO, annotation code 等等。

點Run生成代碼

生成的代碼結構以下

相關文章
相關標籤/搜索