强制休眠在删除和重新创建数据库之后创建表 [英] Force Hibernate To Create Tables After Dropping and Recreating database

查看:70
本文介绍了强制休眠在删除和重新创建数据库之后创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用java 2 ee开发一个web应用程序。我也使用休眠和MySQL。
为了恢复备份文件,在我的应用程序中的某一点,我需要删除当前数据库并重新创建它,我这样做:



<$ p (连接)DriverManager.getConnection(jdbc:mysql:// localhost /?user = user& password = pass); $ p> Connection conn =
Statement statement =(Statement)conn.createStatement();
statement.executeUpdate(DROP DATABASE tcs;);
statement.executeUpdate(CREATE DATABASE tcs charset = utf8 collat​​e = utf8_persian_ci;);

删除并重新创建后,我需要使用默认用户(spring security user)初始化数据库

  User admin = new User(); 
UserAuthority ROLE_USER = new UserAuthority(ROLE_USER);
ROLE_USER.save();
admin.addUserAuthority(ROLE_USER);
admin.setEnabled(true);

admin.save();

但在最后一行应用程序抛出这个异常

  Hibernate:插入角色(权限)值(?)
[DEBUG] [16:19:17,734] [http-bio-8080-exec-10] NewPooledConnection :367 com.mchange.v2.c3p0.impl.NewPooledConnection@bcbe33a处理一个throwable。
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表'tcs.roles'不存在

我知道hibernate在启动时创建表,但在这种情况下,它无法在删除/重新创建后重新创建表,所以我如何强制hibernate再次创建表?
或假设是否有类似于 Hibernate.createTable(Class)

解决方案对于未来的googlers:
它非常简单,毕竟,我只需要建立会话工厂,所以我添加了这一行,它的工作方式就像是魅力:



()。buildSessionFactory();

注意 buildSessionFactory()是在hibernate 4弃用,但我想你可以使用代码这里来做相同。


I am developing a web application with java 2 ee. I also use hibernate and mysql. in order to restore the backup file, in some point in my application i need to drop the current database and recreate it, i do it as follow :

    Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/?user=user&password=pass");
    Statement statement = (Statement) conn.createStatement();
    statement.executeUpdate("DROP DATABASE tcs;");
    statement.executeUpdate("CREATE DATABASE tcs charset=utf8 collate=utf8_persian_ci;");

after dropping and recreating i need to initialize database with default user (spring security user)

    User admin = new User();
    UserAuthority ROLE_USER = new UserAuthority("ROLE_USER");
    ROLE_USER.save();
    admin.addUserAuthority(ROLE_USER);
    admin.setEnabled(true);

    admin.save();

but at the last line application throws this exception

Hibernate: insert into roles (authority) values (?)
[DEBUG][16:19:17,734] [http-bio-8080-exec-10] NewPooledConnection:367  com.mchange.v2.c3p0.impl.NewPooledConnection@bcbe33a handling a throwable.
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'tcs.roles' doesn't exist

i know hibernate creates tables at startup but in this case it fails to recreate the tables after a drop/recreate , so how can i force hibernate to create tables again? or hypothetically is there something like Hibernate.createTable(Class) ?

解决方案

for future googlers : it was so simple after all , i just needed to build session factory so i added this line and it worked like charm :

new Configuration().configure().buildSessionFactory();

note that buildSessionFactory() is deprecated in hibernate 4 but i guess you can use the code here to do the same.

这篇关于强制休眠在删除和重新创建数据库之后创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆