I have a system where all tables in the database have a column empresa
, which is part of PRIMARY KEY
.
In the client table, I have a column id
(autoincrement), which together with empresa
form a composite key, mapped as follows to the Java entity:
Customer Entity
@Entity
@Table(name = "CLIENTES")
public class ClienteEntity implements Serializable {
@EmbeddedId
private ClienteId id;
//getters e setters e demais campos omitidos
}
Client composite key
@Embeddable
public class ClienteId implements Serializable {
@Column(name = "EMPRESA")
private String empresa;
@Column(name = "ID_CLIENTE")
private String idCliente;
//getters e setters omitidos
}
The problem is in mapping the Client entity as foreign key in the Order.
Customer Order Entity
@Entity
@Table(name = "PEDIDOS")
public class PedidoEntity implements Serializable {
@EmbeddedId
private PedidoId id;
@Column(name = "STATUS")
private Integer status;
@Column(name = "DATA_ENTREGA")
private LocalDateTime dataEntrega;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumns(value = {
@JoinColumn(name = "ID_CLIENTE", referencedColumnName = "ID_CLIENTE", updatable = false, insertable = false),
@JoinColumn(name = "EMPRESA", referencedColumnName = "EMPRESA", updatable = false, insertable = false)})
private ClienteEntity cliente;
}
As it stands, I can read the requests and bring the client together through the call to pedidoRepository.findAll()
.
However, I can not write a new request by passing the existing client on the requested entity and making a simple one:
pedidoRepository.save(pedido);
Order table structure
EMPRESA | ID_PEDIDO | ID_CLIENTE | STATUS | DATA_ENTREGA
What is the correct way to map Customer (a compound foreign key) in the Order class?