1

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

但是,当我说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> 

回答

2

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

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

相关问题