Java 如果不存在Hibernate4和spring4 for mysql,则不会自动创建模式
若不存在,则不会自动创建架构。若数据库名称存在,则表示会自动创建表,但架构不存在,则表示不会在运行时创建架构。如何解决此问题 休眠属性Java 如果不存在Hibernate4和spring4 for mysql,则不会自动创建模式,java,mysql,spring,hibernate,Java,Mysql,Spring,Hibernate,若不存在,则不会自动创建架构。若数据库名称存在,则表示会自动创建表,但架构不存在,则表示不会在运行时创建架构。如何解决此问题 休眠属性 hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.show_sql=true hibernate.hbm2ddl.auto=create-update ** i use these keyword seperately also //create or//update** **xm
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create-update ** i use these keyword seperately also //create or//update**
**xml configuration**
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.testing.domain" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</prop>
</props>
</property>
</bean>
hibernate.dialogue=org.hibernate.dialogue.mysqldialogue
hibernate.show_sql=true
hibernate.hbm2ddl.auto=创建更新**我也单独使用这些关键字//创建或//更新**
**xml配置**
${hibernate.dial}
${hibernate.show_sql}
${hibernate.hbm2ddl.auto}
属性hibernate.hbm2ddl.auto
没有create update
这样的值。所有可能的值都是
- 证实
- 更新
- 创造
- 创建下降
请仔细阅读。您可以使用
create
创建模式,如果不存在模式请在代码中使用此模式,它会删除当前模式并创建一个新模式
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto.create-drop}</prop>
${hibernate.hbm2ddl.auto.create drop}
您可以使用import.sql
在资源中添加import.sql
文件,如下所示:
/*create database at first time*/
CREATE SCHEMA your-database-name;
<hibernate-configuration>
<session-factory>
...
...
<property name="hbm2ddl.import_files">import.sql</property>
...
...
</session-factory>
</hibernate-configuration>
并在hibernate.cfg.xml
中添加一行,如下所示:
/*create database at first time*/
CREATE SCHEMA your-database-name;
<hibernate-configuration>
<session-factory>
...
...
<property name="hbm2ddl.import_files">import.sql</property>
...
...
</session-factory>
</hibernate-configuration>
值create将在sessionFactory
creation中创建表,并使其保持不变
值create drop将创建表,然后在关闭会话工厂时删除它们
验证:验证架构,不更改数据库
更新:更新架构。也可以对hibernate.hbm2ddl.auto使用select值,而不是create。在我们的应用程序中,我们使用如下所示,它工作得非常好。我想这是关于hibernate.hbm2ddl.auto选择值的StackOverflow的第一个信息,我在谷歌上搜索了一下,但是没有找到任何关于“hibernate.hbm2ddl.auto”属性选择值的链接
<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="databaseDataSource" />
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.hbm2ddl.auto">select</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
选择
如果任何人对选择选项有进一步的输入。请共享。然后使用${hibernate.hbm2ddl.auto.create}这些仅在运行时顺序创建新模式,不用于更新,无法检索旧记录,我需要一次配置来创建模式(如果不存在)并继续更新记录检查此[链接]创建模式您的数据库名称;我必须去哪里configure@user3928299在import.sql
内部,这只是为了创建架构。如果不存在架构,hibernate不会创建自己的架构实例,它只是创建/更新表/列找到的解决方案数据库。url=jdbc:mysql://localhost:3306/hicasting?createDatabaseIfNotExist=true&characterEncoding=UTF-8;像hibernate.hbm2ddl.auto的select选项这样的数据库只用于查询数据库。这意味着应用程序中不再有DDL更改。如果错误,请更正。database.url=jdbc:mysql://localhost:3306/hicasting?createDatabaseIfNotExist=true&characterEncoding=UTF-8;