I'm having problems in the oneToMany relationship of two entities using Jpa Hibernate.
I have two tables, one cancellation and another that has the orders that are part of the cancellation. The cancellation table has a simple primary key (idcount) The cancellation request table has a key composed by 4 fields (idcancelamento, codigofilial, sales order, typopedido), I do not know the reason for this structure, because it is a very large base and has worked for quite some time already.
Then I created an entity for the cancellation 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 compound 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 by id and try to access the property requests of the canceling entity the program throws 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've already changed fetch from oneToMany to LAZY, tried using @Embeddedid and I always have the same exception. The only way he did not give the error was when I removed the 3 columns of the compound key and left only the unrecognized, but when I access the property it returns only 1 result, and q should return 14.
I no longer know where to search anymore, I've tried in many ways. Thank you for your attention and hope someone can help.