Hibernate使用hbm2ddl创建或更新数据库表
来源:互联网 发布:网络管理规范 编辑:程序博客网 时间:2024/06/05 06:56
使用hibernate自带的hbm2ddl可以实现自动创建和更新数据库表。使用起来也很简单。
只需要在hibernate-config.xml配置文件中设置:
<property name="hbm2ddl.auto">create</property>
文档中是如下注解:
在SessionFactory
创建时,自动检查数据库结构,或者将数据库schema的DDL导出到数据库. 使用 create-drop
时,在显式关闭SessionFactory
时,将drop掉数据库schema.
取值 validate
| update
| create
| create-drop
程序就会自动创建或更新表。
也可以手动调用create方法,将sql语句导出,让其自动在数据中执行。
hbm2ddl包的位置:org.hibernate.tool.hbm2ddl;
其中有一个重要的类:SchemaExport
create方法的参数如下。
/**
* Run the schema creation script.
*
* @param script print the DDL to the console
* @param export export the script to the database
*/
public void create(boolean script, boolean export) {
execute( script, export, false, false );
}
PS:如果有many to one,默认生成的字段名是 关联表的表名_主键(假设为主键关联)
如果想自己设置关联的字段名,可以加 @JoinColumn(name="roleid")-----Annotation的方法。xml的方式省略。
- Hibernate使用hbm2ddl创建或更新数据库表
- hibernate.hbm2ddl.auto(自动创建表结构)配置详解
- hibernate.hbm2ddl.auto=update 更改实体类后,数据库不更新
- hibernate.hbm2ddl
- Hibernate hbm2ddl
- 使用Hibernate-tools中的hbm2java和hbm2ddl根据hbm文件自动生成pojo和数据库脚本
- 使用Hibernate-tools中的hbm2java和hbm2ddl根据hbm文件自动生成pojo和数据库脚本
- 使用Hibernate-tools中的hbm2java和hbm2ddl根据hbm文件自动生成pojo和数据库脚本
- hibernate.hbm2ddl.auto的update属性不能创建表的问题
- hibernate.hbm2ddl.auto设置为update时数据库没有自动生成表结构
- 利用DBLink创建数据库数据定时更新或同步
- Hibernate参数配置说明表hibernate.hbm2ddl.auto
- hibernate.hbm2ddl.auto—Hibernate自动建表的配置
- Hibernate参数配置说明表hibernate.hbm2ddl.auto
- hibernate.hbm2ddl.auto—Hibernate自动建表的配置
- hibernate配置属性---自动建表 hibernate.hbm2ddl.auto
- hibernate笔记-002-hibernate5.2使用hbm2ddl.auto错误
- 为什么Hibernate中使用serializable来加载或更新实体
- 谢磊:关于沟通
- 重装Win7修复Ubuntu10.04引导
- Hadoop知识分享文稿 ( by quqi99 )
- 解决ECLIPSE 卡死的方法
- 有用网址
- Hibernate使用hbm2ddl创建或更新数据库表
- VC工程中后缀为.cpp,.h,.rc,.dsp,.dsw
- Do not delete and re-create if the object already exist
- xml中会报错的符号及处理办法
- 编程技巧之:VS2008中不能添加基类为CButton等类的问题
- C++常数据(函数)成员
- AS 不接受拖放unaccept 以及自定义拖放指示符dropIndicatorSkin
- 创建一个可以写本地文件的触发器
- POCO的Application分析