简体   繁体   English

Java Spring JPA Hibernate 不更新列

[英]Java Spring JPA Hibernate not updating column

I'm using Spring 4.1.0 with hibernate-entitymanager 4.1.9我正在使用 Spring 4.1.0 和 hibernate-entitymanager 4.1.9

Also, I'm using Postgres 9.6另外,我正在使用 Postgres 9.6

I have following entity:我有以下实体:

@Entity
@Table(name = "audit", schema = "public")
@SequenceGenerator(name = "AUDIT_SEQUENCE", sequenceName = "AUDIT_SEQUENCE", allocationSize = 1, initialValue = 1)
public class AuditTrail {

        @Id
        @Column(name = "auditid", unique = true)
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "AUDIT_SEQUENCE")
        private long auditId;

        @Column(name = "auditvalue")
        private String auditValue;


        ... getter setters ...
}

This gives the column structure as character varying(255)这给出了列结构作为字符 varying(255)

I want to update the auditValue column to have length 10000. So I update the entity as:我想将 auditValue 列更新为长度 10000。所以我将实体更新为:

@Column(name = "auditvalue", length = 10000)
private String auditValue;

But the column structure does not update.但是列结构不会更新。

My applicationContext.xml contains:我的 applicationContext.xml 包含:

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.avaya.mediamanager.entity" />
    <property name="jpaVendorAdapter" ref="hbAdapterBean_pgsql"/>   
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.id.new_generator_mappings">true</prop>
        </props>
    </property>     
</bean>
<bean id="hbAdapterBean_pgsql" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">  
    <property name="showSql" value="false"></property>  
    <property name="generateDdl" value="true"></property>  
    <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect"></property>  
</bean>  

How can I get the column updated from the application?如何从应用程序更新列?

From what i'm understanding, hibernate.hbm2ddl.auto set to update doesn't actually update already created tables, but will add new one without dropping your data. 根据我的理解,设置为update hibernate.hbm2ddl.auto实际上并不更新已创建的表,但会添加新表而不丢弃数据。

You could change hibernate.hbm2ddl.auto to create-drop , this will cause your schema to be recreated each time you restart your service, but I not sure if it's exactly what you want since it will also drop all your data. 您可以将hibernate.hbm2ddl.auto更改为create-drop ,这将导致每次重新启动服务时都重新创建架构,但我不确定它是否正是您想要的,因为它也会丢弃您的所有数据。

If you want to keep your data, you might need to think about using something like flyway to populate it. 如果您想保留数据,可能需要考虑使用flyway这样的东西来填充它。

I hope this help! 我希望这有帮助! Have fun! 玩得开心!

I agree with @Schisme From what I know, it's not possible to update type or delete columns.我同意@Schisme 据我所知,更新类型或删除列是不可能的。 You will need to use flyway, and create a migration.您将需要使用 flyway,并创建一个迁移。

In order to make things easier you can create a mysql procedure inside migration like this:为了使事情变得更容易,您可以像这样在迁移中创建一个 mysql 过程:

DELIMITER $$
CREATE PROCEDURE change_column_type_if_exists(`@TABLE` VARCHAR(100), `@COLUMN` VARCHAR(100), `@NEW_TYPE` varchar(100) )
`DropColumnIfExists`: BEGIN
    DECLARE `@EXISTS` INT UNSIGNED DEFAULT 0;

    SELECT COUNT(*) INTO `@EXISTS`
    FROM `information_schema`.`columns`
    WHERE (
                      `TABLE_SCHEMA` = DATABASE()
                  AND `TABLE_NAME` = `@TABLE`
                  AND `COLUMN_NAME` = `@COLUMN`
              );

    IF (`@EXISTS` > 0) THEN
        SET @SQL = CONCAT('ALTER TABLE ', `@TABLE`, ' modify ', `@COLUMN`, ' ',`@NEW_TYPE`,' ;');
        PREPARE query FROM @SQL;
        EXECUTE query;
    END IF;
END $$
DELIMITER ;

And then in case you have a column you need to modify type for, to run something like this:然后如果你有一个列你需要修改类型,运行这样的东西:

CALL change_column_type_if_exists('audit', 'auditvalue', 'varchar(3000)');

or或者

CALL change_column_type_if_exists('audit', 'auditvalue', 'TEXT');

If you found any better alternatives, please let us know too.如果您发现任何更好的替代品,也请告诉我们。 We would appreciate it.我们将不胜感激。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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