使用 hibernate 根據映射文件生成數據庫表


為了更好的顯示效果,可以在hibernate.cfg.xml配置文件的<session-factory>標簽里加入以下內容:

顯示sql語句和格式化顯示sql語句:

<property name="show_sql">true</property>
<property name="format_sql">true</property>

方法一:使用SchemaExport

映射文件Student.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="accp.hibernate">
    <class name="Student" table="Student">
        <id name="id" column="id">
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String" column="name" />
        <property name="birthDay" type="java.util.Date" column="birthday" />
    </class>
</hibernate-mapping>

對應的實體類,Student.java

package accp.hibernate;

import java.util.Date;

public class Student{
    private Integer id;
    private String name;
    private Date birthDay;
    //省略get,set方法......
}

關鍵代碼!使用SchemaExport生成表的代碼如下:

Test.java:

package accp.hibernate;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class Test {
    public static void main(String[] args) {
        Configuration conf = new Configuration().configure();   //讀取並解析配置文件
        SchemaExport export=new SchemaExport(conf);   //創建SchemaExport
        //執行生成表。參數1:是否顯示sql語句,參數2:是否到數據庫里執行
        export.create(true, true);  
    }
}

方法二:配置<property name="hbm2ddl.auto">屬性

在hibernate.cfg.xml配置文件的<session-factory>標簽里加入以下內容:

<property name="hbm2ddl.auto">create</property>

<property name="hbm2ddl.auto">的選項有:validate | update | create | create-drop

areate 如果數據庫里沒這張表,則自動創建。
update 配置文件里的表結構發生改變,重新創建該表。
create-drop

在顯式關閉SessionFactory 時,將刪除掉數據庫 schema。

validate 驗證,每次對數據庫表作操作時驗證是否和映射文件的結構對應上。

配置完成后。只要在程序里對該“表”進行操作,hibernate就會自動創建該表。如Test.java:

以上兩種方法都能根據映射文件往數據庫中創建表,並在控制台中打印出如下sql建表語句:

Hibernate: 
    drop sequence hibernate_sequence
Hibernate: 
    create table Student (
        id number(10,0) not null,
        name varchar2(255 char),
        birthday timestamp,
        primary key (id)
    )
Hibernate: 
    create sequence hibernate_sequence

========================================================================================

補充:在hibernate自動創建表時添加約束

需要在hibernate創建表時添加約束的話,可以在映射文件相應字段的property里添加<column>子元素。如name字段:

<property name="name" type="java.lang.String">
    <column name="name"></column>
</property>

在<column>標簽里添加相應的屬性即可,如設置name列的長度為10,只能以a開頭:

<property name="name" type="java.lang.String">
    <column name="name" length="10" check="name like 'a%'"></column>
</property>

這時,hibernate打印出來的sql語句為:

drop table Student cascade constraints

    drop sequence hibernate_sequence

    create table Student (
        id number(10,0) not null,
        name varchar2(10 char) check (name like 'a%'),
        birthday timestamp,
        primary key (id)
    )

    create sequence hibernate_sequence


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM