I have a class Cliente
that has composite key:
@Entity
@Table(name = "Clientes")
@IdClass(ClientePK.class)
public class Cliente implements Serializable {
//@EmbeddedId
//private ClientePK clientePK;
@Id
@Column(name = "Id_Cliente", nullable = false, length = 15)
private long idCliente;
@Id
@Fetch(FetchMode.SELECT)
@ManyToOne
@JoinColumn(name = "Loja", nullable = false)
private Loja loja;
}
public class ClientePK implements Serializable {
private long idCliente;
private Loja loja;
}
Next I have a class ClienteHistorico
which also contains compound key.
@Entity
@Table(name = “Clientes_Historicos”)
@IdClass(ClienteHistoricoPK.class)
public class ClienteHistorico implements Serializable {
@Id
@Resolvable(colName = "Id. Cliente Histórico")
@Column(name = "Id_Cliente_Historico", nullable = false)
private Long idClienteHistorico;
@Id
@Fetch(FetchMode.SELECT)
@ManyToOne
@JoinColumn(name = "Loja", nullable = false)
private Loja loja;
@Resolvable(colName = "Cliente")
@Fetch(FetchMode.SELECT)
@JoinColumns({
@JoinColumn(name = "id_cliente", referencedColumnName = "Id_Cliente", nullable = false, insertable = true)
,
@JoinColumn(name = "loja", referencedColumnName = "loja", nullable = false, insertable = false, updatable = false)
})
@MapsId("loja")
@ManyToOne(optional = false)
private Cliente cliente;
}
I used the @MapsId("loja")
annotation to indicate that the loja
field of the primary key should be used with the FK field of the client connection itself.
It happens that when inserting in ClienteHistorico
the cliente
field is not being considered for insertion.
Informações: Hibernate: insert into Clientes_Historicos (Data, Tipo, Usuario, Id_Cliente_Historico, Loja) values (?, ?, ?, ?, ?) WARN: SQL Error: 1400, SQLState: 23000 ERROR: ORA-01400: não é possível inserir NULL em ("MARTINELLO"."CLIENTES_HISTORICOS"."ID_CLIENTE") Informações: HHH000010: On release of batch it still contained JDBC statements ERROR: HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]