如何使用“ddl-auto=update”和“nullable=false”添加新列

Tar*_*ion 4 java postgresql hibernate

如果我将这样的列添加到现有实体,hibernate 将无法添加该列,因为存在没有值的现有行:

\n\n
@Column(name = "width", nullable = false)\nprivate int width;\n
Run Code Online (Sandbox Code Playgroud)\n\n

失败并出现此错误:

\n\n
2015-11-14 20:33:41.163 ERROR 9068 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000388: Unsuccessful: alter table texture add column width int4 not null\n2015-11-14 20:33:41.163 ERROR 9068 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaUpdate  : FEHLER: Spalte \xe2\x80\x9ewidth\xe2\x80\x9c enth\xc3\xa4lt NULL-Werte\n
Run Code Online (Sandbox Code Playgroud)\n\n

我不想在我的数据库列定义中使用默认值。

\n

mal*_*una 5

您必须使用现有数据更改表,因此如果列不可为空且未指定默认值,则无法将列添加到现有行。

所以我认为你需要允许可为空或指定默认值,你没有其他选择......