oracle 19c默认值与liquibase

iyr7buue  于 2021-07-07  发布在  Java
关注(0)|答案(1)|浏览(441)

我有一个简短的问题,为什么我的默认值没有被使用。使用oracle19c、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);

我觉得我错过了一些明显的东西。。。
(其他类型也一样,例如localdatetime,使用type=“timestamp”和defaultvaluecomputed=“current\u timestamp”)

cdmah0mi

cdmah0mi1#

我想我知道发生了什么。所有想知道。。。
这个项目使用hibernate(我没想过),似乎hibernate在没有手动设置的情况下会将所有值设置为“null”。我没有找到在liquibase中添加“defaultonnull”的解决方案。
这就是我使用hibernate钩子@prepersist的解决方案

class MyEntity {
  private Boolean attribute;

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

相关问题