Oracle 19c 默认值与 Liquibase

Oracle 19c Default Values with Liquibase

我有一个简短的问题,为什么我的默认值没有被使用。 使用 oracle 19c、liquibase 和 spring-boot

<column name="ACTIVE" type="BOOLEAN" defaultValueBoolean="false">
  <constraints nullable="false"/>
</column>

成功添加到数据库并在 IntelliJ 中显示

ACTIVE NUMBER(1) = 0

每当我想添加一个新条目并且不设置我得到的活动属性时

ORA-01400: cannot insert NULL into

添加:

MyEntity me = new MyEntity();
me.setSomething("Hi");
meRepository.save(me); // Error because I didn't use me.setActive(false);

我觉得我遗漏了一些明显的东西......

(其他类型也是如此,例如使用 type="TIMESTAMP" 和 defaultValueComputed="CURRENT_TIMESTAMP" 的 LocalDatetime)

我想我知道发生了什么。对于每个想知道...

这个项目使用了 Hibernate(我没想到),似乎 Hibernate 在没有手动设置时将所有值设置为“null”。而且我没有找到在 Liquibase 中添加“default on null”的解决方案。

这就是我寻求使用 Hibernate Hook @PrePersist 的解决方案

class MyEntity {
  private Boolean attribute;

  @PrePersist
  public void onCreate() {
    if(attribute == null) attribute = DEFAULT_VALUE;
  }
}