[英]Java Hibernate Schema Update does nothing
我正在使用MySQL数据库在Java上使用Hibernate。 如果将hbm2ddl.auto设置为“ create”,则会删除所有当前表并正确地重新创建它们,但是如果将其设置为“ update”,则尽管对控制台报告了以下行,但它对数据库没有任何作用:
INFO: HHH000228: Running hbm2ddl schema update
作为参考,我用来建立数据库的代码是:
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure()
.build();
虽然我的cfg.xml文件看起来像:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/testdatabase</property>
<property name="connection.username">xxxxxxxx</property>
<property name="connection.password">xxxxxxxx</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<!-- Names the annotated entity class -->
<mapping class="com.test.case.Example"/>
</session-factory>
</hibernate-configuration>
显然,由于“创建”正在工作,因此我无需显示带注释的类。
编辑:我期望发生的更改基本上是将数据库列类型更正为正确的类型。 在运行更新之前,我将mysql中的列类型更改为与带注释的架构不同的类型,但是在更新时,它并没有按预期进行纠正。 相反,当我完全放弃该表时,更新工作了,看来更新的功能不如预期的那么充分?
服务注册表需要配置中的某些属性。 并且因为您使用的是休眠配置文件的非标准名称/位置
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure("cfg.xml")
.build();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.