【问题标题】:ORA-02201: sequence not allowed hereORA-02201: 此处不允许序列
【发布时间】:2021-10-13 16:59:44
【问题描述】:

我有一个实体类 Content,我正在使用序列生成器自动生成 id。

@Entity
@Table(name = "CONTENT")
public class Content {
    @Id
    @Column(name = "ID", unique = true)
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "SEQ_C")
    @SequenceGenerator(name = "SEQ_C", sequenceName = "SEQ_INCREMENTER_CONTENT", allocationSize = 1)
    private Long id;
}

我正在使用 Spring Boot JPA 插入带有 saveAll() 的数据。

contentRepository.saveAll(contents);

我正在使用liquibase 生成表格。

<createSequence sequenceName="SEQ_INCREMENTER_CONTENT" cycle="false" minValue="1" startValue="1" incrementBy="1"/>
<createTable tableName="content">
        <column defaultValueSequenceNext="SEQ_INCREMENTER_CONTENT" name="id" type="BIGINT">
            <constraints nullable="false" primaryKey="true" primaryKeyName="pk_content"/>
    </column>
    <column name="content_type" type="VARCHAR(255)"/>
    <column name="content_type_id" type="INT"/>
    <column name="data" type="VARCHAR2(255)"/>
    <column name="placement" type="INT"/>
    <column name="placement_id" type="INT"/>
    <column name="segment_id" type="BIGINT"/>
</createTable>
<addForeignKeyConstraint baseColumnNames="segment_id" baseTableName="content" constraintName="FK_CONTENT_ON_SEGMENT" referencedColumnNames="id" referencedTableName="segment"/>

当我插入数据时,spring boot JPA 给了我以下错误。

Caused by: oracle.jdbc.OracleDatabaseException: ORA-02201: sequence not allowed here

另外,我尝试了GenerationType.IDENTITYGenerationType.AUTOGenerationType.SEQUENCE 等其他生成策略。不过,我有这个问题。我能做什么?

【问题讨论】:

    标签: oracle spring-boot jpa liquibase


    【解决方案1】:

    如果您使用的是 11 或更低版本,请查看this answer

    如果 DB-version 为 12 或更高版本,您可以将标识列功能用作described here

    这是您可以检查您正在使用的数据库版本的方法

    select version
      from v$instance;
    

    select *
      from v$version;
    

    【讨论】:

    • "在 oracle 的默认列设置中不允许使用序列" - 是的,从 12.2 开始(对于所有受支持的版本)
    • @a_horse_with_no_name 从来不知道这一点。谢谢!很快就会更新我的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-31
    • 2018-11-16
    • 2013-06-07
    • 2018-09-28
    • 1970-01-01
    相关资源
    最近更新 更多