0

我的实体类:

@Entity
@Table(name = "songs", schema = "dbo")
public class Song {
    @Id
    // Try 1
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "songs_song_id_seq")
    // Try 2 
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "songs_song_id_seq")
    // Try 3
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "songs_song_id_seq")
    // Just so you know, when using one try, the other 2 were commented out :D

    @Column(name = "song_id")
    private Integer songId;

    // MRE
}

我的数据库:(PostgreSQL 10)
我的序列生成器


我的邮递员 POST 请求:

{
    "songName": "Song1",
    "songDuration": "500"
}

表:
我的表

序列 DDL:(nextval('dbo.songs_song_id_seq'::regclass)这与数据类型 BigSerial 一起提供)

我不断收到的错误:The given id must not be null!; nested exception is java.lang.IllegalArgumentException: The given id must not be null!


我没有选择。请告诉我我做错了什么。

PS:使用 Spring Boot 和 PostgreSQL 的第一天。

4

1 回答 1

0

Maybe you have a little incorrect code anywhere. For viewing return values of nextval function, so execute this query

select * from nextval('public.table_id_seq'::regclass)

3-5 times, you will view returned values. Values are modified in the same execution time.

If you want to use this function on inserting process manually, then use this sample query:

INSERT INTO table1 (id, "date", amount) VALUES(nextval('public.table_id_seq'::regclass), '2021-02-06', 30);

I tested all theses queries, worked 100%

于 2022-02-23T23:13:47.103 回答