hibernate自动创建表

有时候,当重装系统时.数据库丢失程序无法运行,安装好数据库后,需重新建表.可以利用hibernate配置文件属性,或程序实现 表的创建.

1.配置文件属性:
   <property name="hibernate.hbm2ddl.auto" value="create" />
   value 的值为:

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

2.程序实现:
class UserTest{
     public static void main(String[] args) throws Exception{           
        //配置环境,分析xml映射文件
        Configuration conf= new Configuration().....       
        //生成并输出sql到文件(当前目录)和数据库
        SchemaExport dbExport=new SchemaExport(conf);
       dbExport.create(true, true);
}

猜你喜欢

转载自cesymm.iteye.com/blog/1025617