create(@RequestBody NotificationEntity e) { not" /> create(@RequestBody NotificationEntity e) { not" /> create(@RequestBody NotificationEntity e) { not"/>

POST with autogenerated id in r2dbc

33 Views Asked by At

I have spring boot/r2dbc application

My controller:

@PostMapping("/add")
public ResponseEntity<String> create(@RequestBody NotificationEntity e) {
    notificationDAO.save(e).subscribe();
    return new ResponseEntity<>(HttpStatus.OK);
}

repository:

@Repository
public interface NotificationDAO extends ReactiveCrudRepository<NotificationEntity, Long> {

}

entity:

@Data
@ToString
@Table(name = "notification")
public class NotificationEntity {

@Column
@Id
private Long id;

@Column(value = "content_pl")
private String contentPL;

@Column(value = "content_en")
private String contentEN;

@Column(value = "event_date")
private LocalDateTime eventDate;

@Column
private LocalDateTime seen;

@Column(value = "application_id")
private Integer applicationId;

@Column(value = "recipient_id")
private Integer recipientId;

@Column(value = "type_id")
private Integer typeId;

@Column(value = "document_id")
private Integer documentId;

@Column(value = "kom_id")
private Integer komId;

@Column(value = "koz_id")
private Integer kozId;

@Column(value = "kof_id")
private Integer kofId;

@Column(value = "control_id")
private Integer controlId;

}

and my table declaration in postgres:

CREATE TABLE public.notification (
    id int8 NOT NULL,
    content_en text NULL,
    content_pl text NULL,
    event_date timestamp NULL,
    seen timestamp NULL,
    application_id int8 NULL,
    recipient_id int8 NULL,
    type_id int8 NULL,
    document_id int8 NULL,
    kom_id int8 NULL,
    koz_id int8 NULL,
    kof_id int8 NULL,
    control_id int8 NULL,
    CONSTRAINT notification_pkey PRIMARY KEY (id),
    CONSTRAINT fk3gh0fu4xhghdgd62lm6mljcaf FOREIGN KEY (kom_id) REFERENCES public.merit_evaluation_card(id),
    CONSTRAINT fk_control_id FOREIGN KEY (control_id) REFERENCES public.application_control(id),
    CONSTRAINT fk_kof_id FOREIGN KEY (kof_id) REFERENCES public.formal_evaluation_card(id),
    CONSTRAINT fk_kom_id FOREIGN KEY (kom_id) REFERENCES public.merit_evaluation_card(id),
    CONSTRAINT fk_koz_id FOREIGN KEY (koz_id) REFERENCES public.merit_team_evaluation_card(id),
    CONSTRAINT fkdolhc4dbxvvovwo0924ifbrxn FOREIGN KEY (koz_id) REFERENCES public.merit_team_evaluation_card(id),
    CONSTRAINT fkigjeg33g1pae9aqliuepdxnpy FOREIGN KEY (document_id) REFERENCES public."document"(id),
    CONSTRAINT fkk8h5styb6x923o4kkokn5k1a7 FOREIGN KEY (application_id) REFERENCES public.application(id),
    CONSTRAINT fkl7oja3n6j1vkasuqmux4isnk8 FOREIGN KEY (type_id) REFERENCES public.metrics_type(id),
    CONSTRAINT fkmlumq0ssiy6k7cklmg337k03a FOREIGN KEY (kof_id) REFERENCES public.formal_evaluation_card(id),
    CONSTRAINT fkmmav4veuakqbfiuctechf7qx4 FOREIGN KEY (recipient_id) REFERENCES public.app_user(id)
);
CREATE INDEX notification_application_id_idx ON public.notification USING btree (application_id);
CREATE INDEX notification_control_id_idx ON public.notification USING btree (control_id);
CREATE INDEX notification_document_id_idx ON public.notification USING btree (document_id);
CREATE INDEX notification_kof_id_idx ON public.notification USING btree (kof_id);
CREATE INDEX notification_kom_id_idx ON public.notification USING btree (kom_id);
CREATE INDEX notification_koz_id_idx ON public.notification USING btree (koz_id);
CREATE INDEX notification_recipient_id_idx ON public.notification USING btree (recipient_id);
CREATE INDEX notification_type_id_idx ON public.notification USING btree (type_id);

Problem is: if I Post to my /api/add controler following json:

{
    "contentPL": "Wnifgh",
    "contentEN": "Apfgh",
    "eventDate": "2018-04-27T17:39:18.622",
    "seen": "2018-05-02T08:46:51.892",
    "applicationId": 7388,
    "recipientId": 1,
    "typeId": 2,
    "documentId": null,
    "komId": null,
    "kozId": null,
    "kofId": null,
    "controlId": null
}

I receive error :

Caused by: io.r2dbc.postgresql.ExceptionFactory$PostgresqlDataIntegrityViolationException: null value in column "id" of relation "notification" violates not-null constraint

In second application I have jpa instead of r2dbc and if I use @GeneratedValue annotation I can send via post and save entity without id (jpa generates id for me) How can I achieve something like that in r2dbc? I cannot send id in POST json, it should be added by r2dbc. Thanks for any advise

1

There are 1 best solutions below

0
mtmx On

serial/default next val on id column in postgres - that's the answer