#NULL master reference in detail implementing master-detail relationship

1 messages · Page 1 of 1 (latest)

north hornet
#

I'm implementing a master-detail relationship and I'm having troubles when persisting data. I defined the following classes to represent the relationship:
`// MASTER
@Entity
@Table(name = "factura_cabecera")
public class FacturaEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private Long numeroFactura;

private LocalDateTime fechaEmision;
private String nombreRazonSocial;
private String codigoCliente;
private BigDecimal montoTotal;
private String usuario;

@OneToMany(
        mappedBy = "factura",
        cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE },
        fetch = FetchType.LAZY,
        orphanRemoval = true
)
private List<FacturaDetalleEntity> detalleList;

}

// DETAIL
@Entity
@Table(name = "factura_detalle")
public class FacturaDetalleEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(length = 500)
private String descripcion;
private BigDecimal cantidad;
private Integer unidadMedida;
private BigDecimal precioUnitario;
private BigDecimal subTotal;

@JsonIgnore
@ManyToOne
@JoinColumn(name = "id_factura", referencedColumnName = "id")
private FacturaEntity factura;

}`

I've also defined some classes to represent the request to the API and I'm using them this way to fill factura (master) and detalle (detail):
`FacturaEntity factura = new FacturaEntity();
// filling factura properties
factura.setNumeroFactura(facturaElectronicaCompraVenta.getCabecera().getNumeroFactura());
...
// filling detalle properties
List<DetalleCompraVenta> detalleList = facturaElectronicaCompraVenta.getDetalle();
List<FacturaDetalleEntity> facturaDetalleList = new ArrayList<>();

for (DetalleCompraVenta detalle :detalleList) {
facturaDetalleList.add(detalle);
}

factura.setDetalleList(facturaDetalleList);
// persist data
facturaRepository.save(factura);`

earnest vineBOT
#

<@&987246584574140416> please have a look, thanks.

earnest vineBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

north hornet
#

But when querying database I get this result.
So, why is id_factura column filling up with NULL values? Thank you in advance for your colaboration.