使用 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值