简体   繁体   English

Oracle 19c 默认值与 Liquibase

[英]Oracle 19c Default Values with Liquibase

I have a quick question on why my default values are not being used.我有一个关于为什么我的默认值没有被使用的快速问题。 Using oracle 19c, liquibase and spring-boot使用 oracle 19c、liquibase 和 spring-boot

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

With this being added successfully to the database and even showing in IntelliJ将其成功添加到数据库中,甚至显示在 IntelliJ 中

ACTIVE NUMBER(1) = 0

Whenever I want to add a new entry to and do NOT set the active attribute I get每当我想添加一个新条目并且不设置我得到的活动属性时

ORA-01400: cannot insert NULL into

Adding:添加:

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

I feel I am missing something obvious...我觉得我错过了一些明显的东西......

(Same goes for other types such as LocalDatetime using type="TIMESTAMP" and defaultValueComputed="CURRENT_TIMESTAMP") (同样适用于其他类型,例如使用 type="TIMESTAMP" 和 defaultValueComputed="CURRENT_TIMESTAMP" 的 LocalDatetime)

I think I found out what's happening.我想我知道发生了什么事。 For everyone wondering...对于每个想知道...

This project uses Hibernate (I didn't think about it) and it seems Hibernate sets all values to "null" when they are not manually set.该项目使用 Hibernate(我没有考虑过),似乎 Hibernate 在未手动设置时将所有值设置为“null”。 And I didn't find a solution to add "default on null" in Liquibase.而且我没有找到在 Liquibase 中添加“default on null”的解决方案。

That's I went for the solution of using the Hibernate Hook @PrePersist那就是我寻求使用 Hibernate Hook @PrePersist 的解决方案

class MyEntity {
  private Boolean attribute;

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

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

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