2
I’m having trouble in oneToMany relationship of two entities using Jpa Hibernate.
I have two tables, one for cancellation and another that has the orders that are part of the cancellation. The cancellation table has a simple primary key (idcancellation) The table of cancellation requests has a key composed of 4 fields (idcancellation, codigofilial, pedidovenda, tipopedido), I do not know the reason for this structure, as it is a very large base and it works for a long time already.
So I created an entity for the cancel table with the following relationship :
@Column
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER , targetEntity = Pedido.class)
@JoinColumn(name = "idcancelamento")
private List<Pedido> pedidos;
I created an entity for the order table with and the composite key using the Annotation @Idclass
@Id
@Column(name = "idcancelamento")
private Long idCancelamento;
@Id
@Column(name = "codigofilial")
private Long codigoFilial;
@Id
@Column(name = "pedidovenda")
private Long pedidoVenda;
@Id
@Column(name = "tipopedido")
private Integer tipoPedido;
Composite key class
public class PedidoKey implements Serializable {
private static final long serialVersionUID = 9057318800444577701L;
private Long idCancelamento;
private Long codigoFilial;
private Long pedidoVenda;
private Integer tipoPedido;
public PedidoKey(){
}
public PedidoKey(Long idCancelamento, Long codigoFilial, Long pedidoVenda, Integer tipoPedido) {
this.idCancelamento = idCancelamento;
this.codigoFilial = codigoFilial;
this.pedidoVenda = pedidoVenda;
this.tipoPedido = tipoPedido;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PedidoKey taskId1 = (PedidoKey) o;
if (!idCancelamento.equals(taskId1.idCancelamento)) return false;
if (!codigoFilial.equals(taskId1.pedidoVenda)) return false;
if (!pedidoVenda.equals(taskId1.pedidoVenda)) return false;
return tipoPedido.equals(taskId1.tipoPedido);
}
@Override
public int hashCode() {
return Objects.hash(idCancelamento, codigoFilial,pedidoVenda,tipoPedido );
}
}
When I make a query per id and try to access the entity’s request cancellation property the program launches the following exception :
"error ocurred org.springframework.orm.jpa.JpaSystemException: Could not read entity state from ResultSet : EntityKey[com.smn.tdc.information.model.Pedido#component[tipoPedido,pedidoVenda,idCancelamento,codigoFilial]{pedidoVenda=403042676, idCancelamento=7662233, tipoPedido=0, codigoFilial=35}]; nested exception is org.hibernate.exception.GenericJDBCException: Could not read entity state from ResultSet : EntityKey[com.smn.tdc.information.model.Pedido#component[tipoPedido,pedidoVenda,idCancelamento,codigoFilial]{pedidoVenda=403042676, idCancelamento=7662233, tipoPedido=0, codigoFilial=35}]"
I already switched the oneToMany fetch to LAZY, tried @Embeddedid and always have the same exception. The only way that it did not give the error was when I removed the 3 columns of the composite key and left only the idcancellation, but when access to property it returns me only 1 result, and q should return 14.
I don’t know where else to look, I tried a lot of different ways. I appreciate the attention and hope that someone can help.