What I need to do is list the vendors that have responded to a single request. I do the query but I get the error (Object comparisons can only be used with OneToOneMappings. Other mapping comparisons must be done through query keys or direct attribute level comparisons)
The query I am doing is like this
SELECT e.Provider ID FROM Contribution e, Order f where e.idOrder=1
The contributing entity is like this
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "idAporte")
private Integer idAporte;
@Basic(optional = false)
@NotNull
@Column(name = "cantidadAportada")
private int cantidadAportada;
@Basic(optional = false)
@NotNull
@Column(name = "precioAporte")
private int precioAporte;
@JoinColumn(name = "idPedido", referencedColumnName = "idPedido")
@ManyToOne(optional = false)
private Pedido idPedido;
@JoinColumn(name = "idProducto", referencedColumnName = "codigoProducto")
@ManyToOne(optional = false)
private Producto idProducto;
@JoinColumn(name = "cedulaProveedor", referencedColumnName = "cedulaProveedor")
@ManyToOne(optional = false)
private Proveedor cedulaProveedor;
I know that the error is because I have a relationship between the Contributions and Orders tables and they are related through the "idOrder" column, therefore I cannot send the 1, but I do not know how to send the order object that has the id 1 .
If you could help me I would really appreciate it.
You must do a JOIN for this query. The JOIN clause will bring you the union of 2 tables through a common field. In this case,
Aporte
andPedido
are related viaidPedido
in JPA (and which is mapped toid_pedido
in the table usually).Your query would look like this:
Which translates to:
This way you get the contributions that correspond to a specific request.
You don't show the code of the object
Pedido
, but it seems that you are confusing the object itself, with its id.The query should be:
I think the most logical thing would be to rename your attribute:
By:
In this way, the query would be more coherent:
I hope I've helped.
This JPQL query shows you which vendors have responded to a single request:
Mainly, there are three useful clauses:
I have tested the query with the Eclipse Hibernate Tools plugin, very practical!