0

I'm trying using Spring Data Rest in a Spring Boot backend. I have an entity named Request with some fields and another entity named RequestData. Now, RequestData's mapped table has the foreign key referencing request_id (and nullable=false)

Then, I need to insert a request along with request data.

The problem here is when I try to create a new request along with its data: I can't set the request inside request data because I still have no request Id, but the request_id is not nullable so i need to insert it.

a body like this:

{
   type: "localhost:8080/..../1",
   state: "localhost:8080/.../1",
   ...
   data: [
      {
         field: "foo",
         value: "bar",
      }
   ]
}

throws an exception because it needs the request inside the data record.

This is the Request Entity

@Entity
@Table(name="requests")
public class Request {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private long id;
    @ManyToOne(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
    @JoinColumn(name="request_type_id")
    private RequestType type;
    @ManyToOne(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
    @JoinColumn(name="request_state_id")
    private RequestState state;
    @ManyToOne(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
    @JoinColumn(name="citizen_id")
    private User citizen;
    @ManyToOne(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
    @JoinColumn(name="request_by")
    private User requestedBy;
    @ManyToOne(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
    @JoinColumn(name="operator_id")
    private User operator;
    @Column(name="creation_date")
    private Instant creationDate;
    @Column(name="last_change_date")
    private Instant lastChangeDate;
    @Column(name="closing_date")
    private Instant closingDate;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name="request_id")
    private List<RequestData> data;
    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name="request_id")
    private List<RequestPost> posts;
    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name="request_id")
    private List<RequestAttachment> attachments;
    // constructors, getters, setters, ...

This is the RequestData Entity

@Entity
@Table(name="request_data",
uniqueConstraints = {@UniqueConstraint(columnNames = {"request_id", "field"})})
public class RequestData {
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Column(name="id")
    private long id;
    @Column(name="field", length=25, nullable = false)
    private String field;
    @Column(name="value")
    private String Value;
    @ManyToOne(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
    @JoinColumn(name="request_id", nullable = false)
    private Request request;

    // constructors, getters and setters... 

I created a RequestRepository extending JPARepository but not a RequestDataRepository because request data should always go along with the request.

Removing the not nullable constraint on request_data.request_id would let me to do the post call without problems and checking the request data record over the table I find the request_id correctly set. But I don't want to remove that constraint. Is there any method to specify that request Id will be automatically populated with the new request's id?

Thanks in advance

0 Answers0