我正在使用java 2 ee开发一个Web应用程序.我也使用hibernate和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;");
Run Code Online (Sandbox Code Playgroud)
删除并重新创建后我需要使用默认用户初始化数据库(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();
Run Code Online (Sandbox Code Playgroud)
但在最后一行应用程序抛出此异常
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
Run Code Online (Sandbox Code Playgroud)
我知道hibernate在启动时会创建表,但在这种情况下,它无法在删除/重新创建后重新创建表,那么我如何强制hibernate再次创建表?或假设有类似的东西Hibernate.createTable(Class)?
我有一个maven项目连接到已经存在的数据库.但我想改变它,以便如果数据库尚不存在.它被创造了.所以我在这个额外的代码中?createDatabaseIfNotExist=true加入了这一行.
dataSource.url=jdbc:mysql://localhost/rays-database?createDatabaseIfNotExist=true
而且我遇到了很多错误,所以我的问题是; 是否有任何额外的配置,以便使用?createDatabaseIfNotExist = true
我也尝试添加端口号,这没有任何区别,我没想到它.
dataSource.url=jdbc:mysql://localhost:3306/rays-database?createDatabaseIfNotExist=true
Run Code Online (Sandbox Code Playgroud)
这是我的.properties文件:
dataSource.driverClassName=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql://localhost/rays-database?createDatabaseIfNotExist=true
dataSource.username=root
dataSource.password=
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create
Run Code Online (Sandbox Code Playgroud) 我正在使用Spring 3和hibernate 4.
这是我的root-context.xml
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/musicstore"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<bean id="sessionFactory" name="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="packagesToScan" value="domain" /><!--
entity -->
</bean>
Run Code Online (Sandbox Code Playgroud)
我有这个:
WARN : org.hibernate.engine.jdbc.internal.JdbcServicesImpl - HHH000342: Could not obtain connection to query metadata : Unknown database 'musicstore'
Run Code Online (Sandbox Code Playgroud)
当我在tomcat中部署我的项目时,我希望hibernate会在不存在的情况下创建模式.我试过hibernate.hbm2ddl.auto = create但它没有用
有没有办法在运行时自动创建架构?任何建议都会有所帮助:D
提前致谢.