SmlCodes
SmlCodes

Reputation: 25

JPA null value in column "header_id" of relation "invoice" violates not-null constraint with PostgresSQL

Getting below Error while saving data

Caused by: org.postgresql.util.PSQLException: ERROR: null value in column "header_id" of relation "invoice" violates not-null constraint Detail: Failing row contains (...null).

Header

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "header")
public class Header implements Serializable {

    private static final long serialVersionUID = 3363186434410305269L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "header_id")
    private Long headerId;

    @Column(name = "submitted_by", length = 17)
    private String submittedBy;

    @OneToMany(mappedBy = "header", cascade = CascadeType.ALL)
    @Builder.Default
    private List<Invoice> invoices = new ArrayList<>();
}

Invoice

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "invoice")
public class Invoice implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "invoice_id")
    private Long invoiceId;

    @Column(name = "serial_no")
    private Integer serialNo;

    @JsonIgnore
    @ManyToOne
    @JoinColumn(name = "header_id", nullable = false)
    private Header header;
}

Please help me to solve the error.

Upvotes: 1

Views: 649

Answers (1)

Bastien
Bastien

Reputation: 63

I think this is because your headerId can be null because you use "Long" as type, wich is a good practice, but you should remove nullable = false inside Invoice.java. Put nullable = false inside your database rules If this problem does not come from there, check that the headerId of your Header class is not null when you create the object that you want to save in the database

Upvotes: 0

Related Questions