【发布时间】:2016-05-27 08:23:26
【问题描述】:
我正在使用带有 MySQL 数据库的 Java 上的 Hibernate。如果我设置hbm2ddl.auto to create`,所有当前表都将被删除并正确重新创建,但如果我将其设置为“更新”,尽管向控制台报告了以下行,但它对数据库没有任何作用:
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>
显然,由于 create 正在工作,我认为没有必要显示我的注释类。
编辑:
我期望发生的更改基本上是将数据库列类型更正为正确的类型。在运行更新之前,我将 MySql 中的列类型更改为与带注释的模式不同的内容,但在更新时它并没有像预期的那样更正它。相比之下,当我完全删除表时,更新确实有效,似乎更新没有预期的那么全面?
【问题讨论】:
-
您期望的结果是什么?如果所有表都存在且未更改,更新将不执行任何操作,尝试删除所有表并使用
update运行应用程序我认为它会创建表。 -
我正在检查更新是否将列类型更改为正确的类型。因此,在运行更新之前,我将 mysql 中的列类型更改为与注释模式不同的内容,但在更新时它并没有像预期的那样更正它。
-
只是为了验证我是否按照您所说的完全删除了表格并且它起作用了,所以似乎更新没有像预期的那样功能齐全?
-
我对这个问题进行了搜索,看起来休眠只会将新列添加到数据库中,当它们添加到您的实体类时,请检查此问题(查看第一个答案)stackoverflow.com/questions/15978368/…
-
谢谢,这似乎解释了它。
标签: java database hibernate jdbc