【问题标题】:Hibernate Envers + Liquibase: NULL not allowed for column "REV"Hibernate Envers + Liquibase:列“REV”不允许为 NULL
【发布时间】:2020-05-28 09:06:21
【问题描述】:

我想将 Liquibase 添加到已经使用 Hibernate Envers 但自动创建数据库表的应用程序中。因此,我需要编写包含实体表和 Envers 所需表的 Liquibase 变更集。

这里是变更集:

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
     http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">

<property name="long_type" value="bigint" dbms="postgresql"/>
<property name="long_type" value="long" dbms="h2"/>

<changeSet id="CreateBasicSchema" author="Steven Schwenke">

    <createSequence sequenceName="hibernate_sequence" startValue="0" incrementBy="1"/>

    <createTable tableName="revinfo">
        <column name="rev" type="integer">
            <constraints primaryKey="true"/>
        </column>
        <column name="revtstmp" type="bigint"/>
    </createTable>

    <createTable tableName="stuff">
        <column name="id" type="${long_type}">
            <constraints nullable="false" unique="true" primaryKey="true"/>
        </column>
        <column name="text" type="varchar(36)">
            <constraints nullable="false"/>
        </column>
    </createTable>

    <createTable tableName="stuff_aud">
        <column name="id" type="${long_type}">
            <constraints nullable="false" unique="true" primaryKey="true"/>
        </column>
        <column name="rev" type="integer">
            <constraints referencedTableName="revinfo"
                         foreignKeyName="fk_brands_stuff_revinfo"
                         referencedColumnNames="rev"
                         nullable="false"/>
        </column>
        <column name="revtype" type="integer">
            <constraints nullable="false"/>
        </column>
        <column name="stuff" type="varchar(36)">
            <constraints nullable="true"/>
        </column>
    </createTable>
</changeSet>

这会导致以下异常:

o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 23502, SQLState: 23502
o.h.engine.jdbc.spi.SqlExceptionHelper   : NULL nicht zulässig für Feld "REV"
NULL not allowed for column "REV"; SQL statement:
/* insert org.hibernate.envers.DefaultRevisionEntity */ insert into revinfo (rev, revtstmp) values (null, ?) [23502-200]
org.springframework.dao.DataIntegrityViolationException: could not execute statement ...

经过一番研究,我终于找到了我的错误,并将其作为这个问题的答案发布。希望这可以帮助其他遇到相同异常的开发人员。

【问题讨论】:

    标签: liquibase hibernate-envers


    【解决方案1】:

    缺少的部分是一个简单的 autoIncrement 用于 revinfo 表的 ID:

    <createTable tableName="revinfo">
        <column name="rev" type="integer" autoIncrement="true">
            <constraints primaryKey="true"/>
        </column>
        <column name="revtstmp" type="bigint"/>
    </createTable>
    

    如果没有这个,revinfo 表中的新条目将有一个空 ID,这正是异常所说的。

    上面的 liquibase 代码中的另一个错误是 aud-table 的 id 在 id 上有一个主键。这将导致更新实体时出现异常。根据 Vlad Mihalcea 的说法,aud 表应该有一个组合键:

    <createTable tableName="stuff_aud">
        <column name="id" type="${long_type}">
            <constraints nullable="false" />
        </column>
        <column name="rev" type="integer">
            <constraints referencedTableName="revinfo"
                         foreignKeyName="fk_brands_stuff_revinfo"
                         referencedColumnNames="rev"
                         nullable="false"/>
        </column>
        <column name="revtype" type="integer">
            <constraints nullable="false"/>
        </column>
        <column name="stuff" type="varchar(36)">
            <constraints nullable="false"/>
        </column>
    </createTable>
    <addPrimaryKey tableName="stuff_aud" columnNames="id, rev" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-25
      • 2017-10-17
      • 2015-03-19
      • 1970-01-01
      • 2011-10-25
      • 1970-01-01
      • 2020-11-03
      相关资源
      最近更新 更多