I am having a Spring Data rest API where I am trying to save a new record in Entity named comment. This entity has a many to one relationship with another entity named department.
When I call the post api for this entity , I am getting below null pointer error but I am passing the department foreign key as part of the postman request. I am not sure why it is not getting received in the server side. Can someone please throw some light on it.
Error
"cause": {
"cause": {
"cause": null,
"message": "ERROR: null value in column \"department_id\" of relation \"comments\" violates not-null constraint\n Detail: Failing row contains (32, null, null, null, null, null)."
},
"message": "could not execute statement"
}
Comment Entity (Removed getters and setters for saving lines
@Table(name="comments")
public class Comment implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name="SEQUENCE_FOR_COMMENTS", sequenceName="SEQUENCE_FOR_COMMENTS",allocationSize = 1 )
@GeneratedValue(strategy=GenerationType.AUTO, generator="SEQUENCE_FOR_COMMENTS")
@Column(name="comment_id")
private Integer commentId;
private String comment;
@ManyToOne
@JoinColumn(name="department_id")
private Department department;
}
Department Entity (Removed getters and setters for saving lines
public class Department implements Serializable { private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO, generator="SEQUENCE_FOR_Department" )
@Column(name="department_id")
private Long departmentId;
}
Postman Request
{
"departmentv1":{
"departmentId":163
},
"comment":"Apr292023"
}