我正在使用带有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中的列类型更改为与注释模式不同的内容,但在更新时,它不会按预期更正它。相比之下,当我完全放弃这个表时,更新确实有效,看起来更新并不像预期的那样功能齐全吗?
答案 0 :(得分:0)
服务注册表需要配置中的一些属性。并且因为您使用的是hibernate配置文件的非标准名称/位置
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure("cfg.xml")
.build();