简体   繁体   English

序列发生器在 Oracle 18c 上不起作用

[英]Sequence Generator not working on Oracle 18c

I have a table with columns id and name which I am creating using this class.我有一个表,其中包含我正在使用此 class 创建的列 ID 和名称。

@Entity
@Table(name = "xyz")
public class XYZ implements Serializable{
   
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "some_gen")
    @SequenceGenerator(name = "some_gen", sequenceName = "some_seq", allocationSize = 2)
    @Column(name = "id")
    private Long id;

    @Column(name = "name")
    private String name;
    
    //Getter Setter NoArgConstructor AllArgsConstructor


}

Now when I run this, table as well as sequence is created.现在,当我运行它时,会创建表和序列。 Verified it from SQL Developer.从 SQL Developer 验证。 But when I run the following statement I am getting following error.但是当我运行以下语句时,我收到以下错误。

INSERT INTO xyz (name) VALUES ('John');

ORA-01400: cannot insert NULL into ("SOME_USER"."XYZ"."ID") ORA-01400: 无法将 NULL 插入 ("SOME_USER"."XYZ"."ID")

I am using JPA and Spring Boot.我正在使用 JPA 和 Spring 引导。

//This is MyBatis. It will be triggered by REST API
@Mapper
public interface XYZMapper {

    @Insert("INSERT INTO xyz (name) VALUES (#{name})")
    public void addNewName(XYZ xyz);
}

I don't know Java.我不知道 Java。

But, from Oracle's point of view, the fact that you generated the sequence doesn't mean that Oracle will automagically use it.但是,从 Oracle 的角度来看,您生成序列这一事实并不意味着 Oracle 会自动使用它。 You have to either use it directly, such as您必须直接使用它,例如

insert into xyz (id, name) values (some_seq.nextval, 'John');

or create a database trigger which will do it for you:或创建一个数据库触发器,它将为您完成:

create or replace trigger trg_bi_xyz
  before insert on xyz
  for each row
begin
  :new.id := some_seq.nextval;
end;
/

and now you can use insert you previously posted:现在您可以使用之前发布的insert

INSERT INTO xyz (name) VALUES ('John');

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

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