2
I have a class Cliente
which 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 composite 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 annotation @MapsId("loja")
to indicate that the field loja
primary key should be used with the client connection field FK itself.
It happens that when performing the Insert in ClienteHistorico
the field cliente
is not being taken into consideration 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]
I would try the following: 1) Uncomment the
clientePK
and its annotation; 2) Note the classClientePK
with @Embeddable and put the field annotations in it, then deleting those class fieldsCliente
(you just want theclientePK
uncommented there); 3) in the classClienteHistorico
, delete the annotations of Join and@Resolvable
, and modify the@mapsId
for "clientePK". See if it works.– StatelessDev
@Statelessdev thus generates two new fields. CLIENTE_ID_CLIENTE AND CLIENTE_LOJA when in fact I need him to take advantage of the Customer Class Store field
– Rafael R...