简体   繁体   English

在Java中从JPA批注更改表

[英]Alter table from jpa annotations in Java

I have a definition for a column like that: 我有一个像这样的列的定义:

@Column
private String my_column;

And by default in Postgres database type for this field is character varying(255) . 缺省情况下,在Postgres数据库类型中,此字段的character varying(255)类型为character varying(255)

Now, I want to change the data type for this column. 现在,我想更改此列的数据类型。

How I can do this without entry in database and alter table? 如何在不输入数据库和更改表的情况下执行此操作?

I tried this: 我尝试了这个:

@Lob
@Column
private String my_column;

And

@Column(columnDefinition = "TEXT")
private String my_column;

But, without results. 但是,没有结果。

The thing is, that JPA does not handle Schema changes. 事实是,JPA不处理架构更改。 JPA maps your existing DB to Java Classes, it does not manage the database it self. JPA将您现有的数据库映射到Java类,它不会自行管理数据库。

As for schema changes managment. 至于架构变更管理。 A common practice is to have a schema migration tool to handle that, for example Flyway and Liquibase are a popular solutions. 通常的做法是有一个架构迁移工具来处理,例如迁飞Liquibase是一种流行的解决方案。

There you can write a SQL script, to change the DB column type to "text" and it will apply those changes when you run the DB migration process. 您可以在其中编写SQL脚本,以将数据库列类型更改为“文本”,并且在运行数据库迁移过程时,它将应用这些更改。

Or you can always just access your DB and modify it manually. 或者,您始终可以只访问数据库并手动对其进行修改。

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

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