【问题标题】:Why Hibernate SchemaUpdate is creating new field instead of updating?为什么 Hibernate SchemaUpdate 是创建新字段而不是更新?
【发布时间】:2013-10-02 07:18:28
【问题描述】:

我正在尝试使用 SchemaUpdate 以编程方式更新现有架构。我更改了特定表中现有字段名称的名称,然后, 我正在创建休眠配置对象并将更改后的 hbm.xml 文件添加到配置对象中。

但是当我说SchemaUpdate.execute(true,true) 时,它是在表中创建新字段而不是更新。

这是我的代码:

Configuration hibConfiguration = new Configuration();
hibConfiguration.configure(configFileDoc);
hibConfiguration.addDocument(doc1);
 hibConfiguration.addDocument(doc2);

hibConfiguration.buildMappings();
SchemaUpdate schemaUpdate = new SchemaUpdate(hibConfiguration);
schemaUpdate.execute(true, true);

以下是我的 cfg.xml 文件:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<hibernate-configuration>
   <session-factory>
      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.password">passwrd</property>
      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.url">jdbc:mysql://localhot:3306/testSchema</property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
      <property name="javax.persistence.validation.mode">none</property>
      <property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
      <property name="hibernate.hbm2ddl.auto">update</property>
      <property name="hibernate.default_entity_mode">dynamic-map</property>
   </session-factory>
</hibernate-configuration>

这是我要更新的表的 hbm.xml 文件:

<?xml version="1.0" encoding="UTF-8"?><hibernate-mapping>
  <class entity-name="testTable2">
    <id column="id" name="id" type="java.lang.Long">
      <generator class="identity"/>
    </id>
    <property column="testTable1Id" length="20" name="testTable1Id" type="java.lang.Long"/>
    <property column="doubleColumnj" length="20" name="doubleColumnj" type="java.lang.Double"/>
  </class>
</hibernate-mapping>

【问题讨论】:

    标签: java hibernate configuration-files hibernate-mapping hbm2ddl


    【解决方案1】:

    Hibernate 不对表进行版本控制。它无法确定该字段是否已重命名。因此,当它将现有映射与数据库进行比较时,它所能确定的只是存在一个以前不存在的映射列。

    它也不会删除未映射的列,以便能够支持旧式数据库,或者可能是多个休眠实体提供同一个表的不同视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-30
      • 2018-05-09
      • 2013-05-22
      • 2011-08-05
      • 1970-01-01
      相关资源
      最近更新 更多