How to map entities with composite keys in JPA?

1

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?

    
asked by anonymous 09.04.2017 / 23:57

1 answer

1

In your example, you noted the relationship as follows:

@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;

Note that in the @JoinColumn annotation you filled in the updatable and insertable properties as false .

In this case, when saving the Request entity, it can not insert these fields into the table because you specified that the entity manager should not write to them.

Remove this setting and leave it as follows:

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumns(value = {
    @JoinColumn(name = "ID_CLIENTE", referencedColumnName = "ID_CLIENTE"),
    @JoinColumn(name = "EMPRESA", referencedColumnName = "EMPRESA")
})
private ClienteEntity cliente;
    
23.04.2017 / 16:56