【发布时间】:2013-06-04 15:43:49
【问题描述】:
我正在使用 java 2 ee 开发一个 Web 应用程序。我也使用休眠和mysql。 为了恢复备份文件,在我的应用程序的某个时刻,我需要删除当前数据库并重新创建它,我这样做如下:
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;");
删除并重新创建后,我需要使用默认用户(弹簧安全用户)初始化数据库
User admin = new User();
UserAuthority ROLE_USER = new UserAuthority("ROLE_USER");
ROLE_USER.save();
admin.addUserAuthority(ROLE_USER);
admin.setEnabled(true);
admin.save();
但在最后一行应用程序抛出了这个异常
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
我知道 hibernate 在启动时会创建表,但在这种情况下,它无法在 drop/recreate 之后重新创建表,那么如何强制 hibernate 再次创建表?
或者假设有类似Hibernate.createTable(Class) 的东西?
【问题讨论】:
标签: java mysql spring hibernate jakarta-ee