spring-data-jpa 列不能为空值“一对多”和“多对一”的Spring启动

wj8zmpe1  于 2022-11-10  发布在  Spring
关注(0)|答案(1)|浏览(219)

My "invoice" table is related to "invoice_detail", when I want to save the complete invoice object, with its list of details, it throws me the error: "invoice_id" cannot be null (This in the invoice_detail table).
My tables: (MySQL)

create table factura
(
    id             int auto_increment
        primary key,
    cantidad_items int          null,
    codigo         varchar(255) null,
    costo_total    int          null,
    fecha          datetime     null
);

create table factura_detalle
(
    id              int auto_increment
        primary key,
    cantidad        int          null,
    id_factura      int          not null,
    item            varchar(255) null,
    precio_unitario int          null,
    foreign key (id_factura) references factura(id)
)

My Entity:

@Entity @Getter @Setter
@Table(name = "factura")
public class Factura {

    @JsonIgnore
    @OneToMany(targetEntity = FacturaDetalle.class, mappedBy = "factura", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
    private List<FacturaDetalle> facturaDetalles;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

    @Column(name = "codigo")
    private String codigo;

    @Column(name = "cantidad_items")
    private Integer cantidadItems;

    @Column(name = "costo_total")
    private Integer costoTotal;

    @Column(name = "fecha")
    private Date fecha;

}

@Entity @Getter @Setter
@Table(name = "factura_detalle")
public class FacturaDetalle {
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "id_factura", insertable = false, updatable = false)
    private Factura factura;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

    @Column(name = "cantidad")
    private Integer cantidad;

    @Column(name = "precio_unitario")
    private Integer precioUnitario;

    @Column(name = "id_factura")
    private Integer idFactura;

    @Column(name = "item")
    private String item;

}

My Object before to save: View
Finally, my error is the following:
java.sql.SQLIntegrityConstraintViolationException:Column 'id_factura' cannot be nullat com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:117) ~[mysql-connector-java-8.0.29.jar:8.0.29] at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.29.jar:8.0.29] at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:916) ~[mysql-connector-java-8.0.29.jar:8.0.29] at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1061) ~[mysql-connector-java-8.0.29.jar:8.0.29] at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1009) ~[mysql-connector-java-8.0.29.jar:8.0.29]

i2byvkas

i2byvkas1#

我认为您的实体关系不正确,请检查实体关系。
你不需要在下面添加额外的列,因为双向将在子端创建外键。这里FacturaDetail是Factura的子级。

@Column(name = "id_factura")
    private Integer idFactura;

试试看...应该可以的。
第一个

相关问题