java 如果不存在 mysql,休眠创建模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26273097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
hibernate create schema if not exists mysql
提问by Nessaj Nguyen
I'm using Spring 3 and hibernate 4.
我正在使用 Spring 3 和 hibernate 4。
Here is my root-context.xml
这是我的 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>
And I've got this :
我有这个:
WARN : org.hibernate.engine.jdbc.internal.JdbcServicesImpl - HHH000342: Could not obtain connection to query metadata : Unknown database 'musicstore'
When I deploy my project in tomcat, I want hibernate will create the schema if it's not exist. I have tried hibernate.hbm2ddl.auto= create but it's not working
当我在 tomcat 中部署我的项目时,如果它不存在,我希望 hibernate 将创建模式。我试过 hibernate.hbm2ddl.auto= create 但它不起作用
Are there any ways to do create the schema automatically at run time ? Any suggestions would be helpful :D
有没有办法在运行时自动创建模式?任何建议都会有所帮助:D
Thanks in advance.
提前致谢。
回答by Brendon Dugan
I don't know how to solve your problem in a hibernate specific way, but a cool thing about MySQL is that you can (at the very least under certain conditions) specify for the Database itself to be created if it doesn't already exist via the connection string by appending "?createDatabaseIfNotExist=true" to the end.
我不知道如何以特定于休眠的方式解决您的问题,但是关于 MySQL 的一个很酷的事情是您可以(至少在某些条件下)指定要创建的数据库本身,如果它不存在通过将“?createDatabaseIfNotExist=true”附加到连接字符串的末尾。
So, by changing your Spring config to the following you should get the results you need.
因此,通过将您的 Spring 配置更改为以下内容,您应该会获得所需的结果。
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/musicstore?createDatabaseIfNotExist=true"></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>
It's worth noting that I don't know anything more about this than that it works, so it may very well have limitations of which I am unaware.
值得注意的是,除了它的工作原理之外,我对此一无所知,因此它很可能具有我不知道的局限性。
回答by Alan Hay
Hibernate requires the database to exist: it cannot help you create the database. If you need to create the database the you will need to implement another solution that executes before Hibernate is initialized.
Hibernate 要求数据库存在:它无法帮助您创建数据库。如果您需要创建数据库,您将需要实现另一个在 Hibernate 初始化之前执行的解决方案。
As you are using Spring the following maybe useful for executing the initial 'CREATE DATABASE X' statement.
当您使用 Spring 时,以下内容对于执行初始的“CREATE DATABASE X”语句可能很有用。
http://docs.spring.io/spring-framework/docs/3.0.0.RC3/reference/html/ch12s09.html
http://docs.spring.io/spring-framework/docs/3.0.0.RC3/reference/html/ch12s09.html
You obviously need to ensure the initailization executes before the sessionFactory bean is initialized.
您显然需要确保在 sessionFactory bean 初始化之前执行初始化。
You would also likelydefine 2 datasources, one configured as outlined at the link provided by mretierer (Create MySQL database from Java) i.e. with no database defined and which is used by the Spring db initializer bean and one for use by Hibernate which references the database created during initialization.
您还可能定义 2 个数据源,一个按照 mretierer(从 Java 创建 MySQL 数据库)提供的链接中的概述进行配置,即没有定义数据库并且由 Spring db 初始值设定项 bean 使用,一个由引用数据库的 Hibernate 使用在初始化期间创建。
No idea if any of this will work but looks feasible...
不知道这是否可行,但看起来可行......
回答by mreiterer
First the answer to your question: The property you already supplied should do what you were asking for.
首先回答您的问题:您已经提供的财产应该满足您的要求。
<prop key="hibernate.hbm2ddl.auto">update</prop>
The exception your are getting refers to a different problem. Hibernate can't connect to the database you specified.
您得到的异常是指不同的问题。Hibernate 无法连接到您指定的数据库。
Pls check your connection string
请检查您的连接字符串
<property name="url" value="jdbc:mysql://localhost:3306/musicstore"></property>