使用 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
使用 hibernate 根据映射文件生成数据库表的更多相关文章
- hibernate笔记--通过SchemaExport生成数据库表
方法比较简单,项目中只需要两个java类(一个实体类,如User,一个工具类),两个配置文件(hibernate必须的两个配置文件hibernate.cfg.xml,与User.hbm.xml),即可 ...
- Mybatis映射文件中数据库表列名称和POJO成员域的联系
下面是Mybatis的SQL映射文件. <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ma ...
- [Nhibernate]SchemaExport工具的使用(一)——通过映射文件修改数据表
目录 写在前面 文档与系列文章 SchemaExport工具 SchemaUpdate工具 一个例子 总结 写在前面 上篇文章介绍了使用代码生成器的nhibernate模版来生成持久化类,映射文件等内 ...
- 使用oracle数据库和MySQL数据库时hibernate的映射文件.hbm.xml的不同
假设是使用oracle数据库.那么hibernate的映射文件.hbm.xml例如以下: <id name="xuehao" column="xuehao" ...
- Hibernate生成数据库表
首先创建实体类 import java.util.Date; public class ProductionEntity { public Integer getId() { return id; } ...
- Hibernate之深入Hibernate的映射文件
这周周末 要把hibernate的映射文件搞定 .. 1.映射文件的主结构 主要结构 :根元素为<hibernate-mapping ></hibernate-mapping> ...
- Mybatis总结之如何自动生成数据库表结构
一般情况下,用Mybatis的时候是先设计表结构再进行实体类以及映射文件编写的,特别是用代码生成器的时候. 但有时候不想用代码生成器,也不想定义表结构,那怎么办? 这个时候就会想到Hibernate, ...
- 用eclipes 添加jboss tools中的hibernate tool进行反向工程生成数据库对应的BOJO(Javabean)
用eclipes 添加jboss tools中的hibernate tool进行反向工程生成数据库对应的BOJO(Javabean) 安装: 在help中eclise marksplace中查询JBo ...
- sts使用mybatis插件直接生成数据库表的mapper类及配置文件
首先点击help------>Eclipse Marketplace----->在find中搜索mybatis下面图片的第一个 点击installed 还需要一个配置文件generator ...
随机推荐
- 【计算几何初步-线段相交+并查集】【HDU1558】Segment set
Segment set Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- 【二分答案】 【POJ3497】 【Northwestern Europe 2007】 Assemble 组装电脑
Assemble Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3171 Accepted: 1013 Descript ...
- android——拍照,相册图片剪切其实就这么简单
接触android这么久了.还没有真正的浩浩看看android拍照,相册图片剪切到底是怎么回事,每次都是从别人的代码一扣,就过来了.其实,谷歌提供的API已经很强大.只需要用的好,就那么几句就可以搞定 ...
- XML.ObjTree -- XML source code from/to JavaScript object like E4X
转载于:http://www.kawa.net/works/js/xml/objtree-try-e.html // ========================================= ...
- C#实现WinForm传值实例解析
C#实现WinForm传值的问题经常会做为公司面试的题目,那么作为学习C#以及WinForm传值,我们需要掌握哪些方法和思路呢?下面我们就向你介绍详细的思路和实现的具体步骤,希望对你有所帮助. C#实 ...
- iOS 事件处理机制与图像渲染过程
Peter在开发公众号功能时触发了一个bug,导致群发错误.对此我们深表歉意,并果断开除了Peter.以下交回给正文时间: iOS 事件处理机制与图像渲染过程 iOS RunLoop都干了什么 iOS ...
- W - stl 的 优先队列 Ⅲ
Description In a speech contest, when a contestant finishes his speech, the judges will then grade h ...
- SFTP信任公钥配置及JSCH库
1.SFTP信用公钥配置 1.1 客户端生成密钥对 以DSA举例: ssh-keygen –t dsa 执行该命令后,在home/用户名/.ssh目录下,会生成id_dsa和id_dsa.pub两个文 ...
- php isset — 检测变量是否设置 foreach循环运用
例子 $a = 336 ; $b = 33 ; function large($x,$y){ if((!isset($x))||(!isset($y))){ // echo "this fu ...
- mac osx 10.9 ftp server端口
开启 FTP Serversudo -s launchctl load -w /System/Library/LaunchDaemons/ftp.plist 关闭 FTP Serversudo -s ...