博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hibernate自动生成表
阅读量:6870 次
发布时间:2019-06-26

本文共 2518 字,大约阅读时间需要 8 分钟。

当我们没有配置初始化加载hibernate的时候可以用以下代码自动生成数据库表:

package com.mr.test;import java.io.File;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.tool.hbm2ddl.SchemaExport;public class HelloHibernate {    static Session session;    static Configuration config = null;    static Transaction tx = null;    public static void main(String[] args) {        /** */        /**         * 根据映射文件创建数据库结构         */        try {            config = new Configuration().configure(new File(                    "src/hibernate.cfg.xml"));            System.out.println("Creating tables...");            SessionFactory sessionFactory = config.buildSessionFactory();            session = sessionFactory.openSession();            tx = session.beginTransaction();            SchemaExport schemaExport = new SchemaExport(config);            schemaExport.create(true, true);            System.out.println("Table created.");            tx.commit();        } catch (HibernateException e) {            e.printStackTrace();            try {                tx.rollback();            } catch (HibernateException e1) {                e1.printStackTrace();            }        } finally {        }    }}

注意配置文件里参数配置:

转自:http://blog.csdn.net/zwhfyy/article/details/4514966

只要在hibernate.cfg.xml添加这句话,就可以自动生成数据表

<property name="hibernate.hbm2ddl.auto">update</property>
update:表示自动根据model对象来更新表结构,启动hibernate时会自动检查数据库,如果缺少表,则自动建表;如果表里缺少列,则自动添加列。
还有其他的参数:
create:启动hibernate时,自动删除原来的表,新建所有的表,所以每次启动后的以前数据都会丢失。
create-drop:启动hibernate时,自动创建表,程序关闭时,自动把相应的表都删除。所以程序结束时,表和数据也不会再存在。
PS:数据库要预先建立好,因为hibernate只会建表,不会建库

 

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

 

表结构和数据总是在程序执行的时候无端的修改,折腾了好长时间,查了很长时间hibernate的数据库映射文件和接口程序,始终没有发现有什么错误,到最后才发现了它!

           <property name="hibernate.hbm2ddl.auto" value="update" />
解释如下:

hibernate.hbm2ddl.auto Automatically validate or export schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly. eg. validate | update | create | create-drop

其实这个参数的作用主要用于:自动创建|更新|验证数据库表结构。

如果没有此方面的需求建议set value="none".

其它几个参数的意思:

 

validate               加载hibernate时,验证创建数据库表结构create                  每次加载hibernate,重新创建数据库表结构create-drop        加载hibernate时创建,退出是删除表结构update                 加载hibernate自动更新数据库结构

 

如果发现数据库表丢失或新增,请检查hibernate.hbm2ddl.auto的配置 可设置 <property name="hibernate.hbm2ddl.auto" value="none"/>

你可能感兴趣的文章
老硬盘华丽变身高速硬盘
查看>>
IT公司规模对技术人员知识增长率的影响
查看>>
由日企衰败联想到的运维管理问题
查看>>
Outlook中轻松添加LDAP服务
查看>>
线上咨询交易火爆 新模式还是新机遇?
查看>>
比较使用sql*loader的直接加载方式和传统加载方式的性能差异
查看>>
HAProxy Nginx LVS Apache总结篇
查看>>
BlackBerry Localization sample (1)
查看>>
类模版和函数模版需要注意的
查看>>
用 Tornado 实现简单的在线代理
查看>>
函数指针和指针函数
查看>>
HTML 如何让图片全屏的问题
查看>>
silverlight 如何在浏览器的新页面里打开一个xaml
查看>>
SQL Tuning Advisor使用实例
查看>>
server-U上传中文文件乱码
查看>>
编程珠玑:用后缀数组寻找最长重复字符串
查看>>
Java写到.txt文件,如何实现换行
查看>>
通过http proxy访问git 服务
查看>>
JavaScript之isNaN()函数讲解
查看>>
MPlayer源代码分析
查看>>