简体   繁体   中英

Why Hibernate SchemaUpdate is creating new field instead of updating?

I am trying to update the existing schema programmatically using SchemaUpdate. I changed the name of the existing field name in specific table and then, I am creating the hibernate configuration object and adding the changed hbm.xml files into the configuration object.

But when I said SchemaUpdate.execute(true,true) it is creating the new field in the table instead of updating.

Here is my code:

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

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

following is my cfg.xml file:

<?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>

and this is my hbm.xml file for table to update:

<?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>

Hibernate does not do version control on tables. It has no way of determining that the field was renamed. So when it compares the existing mapping to the database, all it is able to determine is there is a mapped column which did not exist before.

It also does not drop unmapped columns in order to be able to support legacy databases, or perhaps multiple hibernate entities providing different views of the same table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM