Alguém me ajuda com um erro de persistência???
Tenho um objeto e que salva-lo no banco e em uma tabela separa guardando a referencia de produto e estoque sendo
Produto N
Estoque 1
Tenho o seguinte código.
Caused by: java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails (`waveerp`.`tbl_erp_entrada_estoque_produto`, CONSTRAINT `FK_ERP_ENTRADA_ESTOQUE_PRODUTO` FOREIGN KEY (`ID_PRODUTO`) REFERENCES `tbl_produto` (`CODIGO`) ON DELETE NO ACTION ON UPDATE NO ACTION)
EntradaEstoque.java
@OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.REMOVE, mappedBy="entradaEstoque")
@Fetch(org.hibernate.annotations.FetchMode.SELECT)
private Collection<EntradaEstoqueProduto> produto = new ArrayList<EntradaEstoqueProduto>();
EntradaEstoqueProduto.java
public static EntradaEstoqueProduto newInstance(EntradaEstoque entradaEstoque, Produto produto, BigDecimal quantidade, BigDecimal valorUnitario, BigDecimal valorTotal, String identificacao){
EntradaEstoqueProduto entradaEstoqueProduto = new EntradaEstoqueProduto();
entradaEstoqueProduto.setId(new PK(entradaEstoque.getId(), produto.getCodigo()));
entradaEstoqueProduto.setEntradaEstoque(entradaEstoque);
entradaEstoqueProduto.setProduto(produto);
entradaEstoqueProduto.setQuantidade(quantidade);
entradaEstoqueProduto.setValorUnitario(valorUnitario);
entradaEstoqueProduto.setValorTotal(valorTotal);
entradaEstoqueProduto.setIdentificacao(identificacao);
return entradaEstoqueProduto;
}
@Embeddable
public static class PK implements Serializable{
@Column(name="id_entrada_estoque", nullable=false, updatable=false)
private Long entradaEstoqueId;
@Column(name="id_produto", nullable=false, updatable=false)
private Long produtoId;
public PK(){}
public PK(Long entradaEstoqueId, Long produtoId){
this.entradaEstoqueId = entradaEstoqueId;
this.produtoId = produtoId;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((entradaEstoqueId == null) ? 0 : entradaEstoqueId.hashCode());
result = prime * result
+ ((produtoId == null) ? 0 : produtoId.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PK other = (PK) obj;
if (entradaEstoqueId == null) {
if (other.entradaEstoqueId != null)
return false;
} else if (!entradaEstoqueId.equals(other.entradaEstoqueId))
return false;
if (produtoId == null) {
if (other.produtoId != null)
return false;
} else if (!produtoId.equals(other.produtoId))
return false;
return true;
}
}
@EmbeddedId
private PK id;
@ManyToOne
@JoinColumn(name="ID_ENTRADA_ESTOQUE", insertable=false, updatable=false, nullable=false)
private EntradaEstoque entradaEstoque;
@ManyToOne(fetch = FetchType.EAGER)
@Fetch(FetchMode.SELECT)
@JoinColumn(name="ID_PRODUTO", insertable=false, updatable=false, nullable=false)
private Produto produto;
abraços.