JPQL query problem

1

What I need to do is list the suppliers that have responded to a single request. I make 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'm doing is like this

SELECT e.DataSupplier FROM Contribution e, Order f where e.idOppointed = 1

The contribution 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 by means of the column "requestid", therefore I can not send the 1, but I do not know how to send the order object that I have the id 1.

If you could help me, I would really appreciate it.

    
asked by Lina Cortés 10.06.2016 в 06:27
source

3 answers

1

You must do a JOIN for this query. The JOIN clause will bring you the union of 2 tables by means of a field in common. In this case, Aporte and Pedido are related by idPedido in JPA (and that is mapped to id_pedido in the table usually).

Your query would be as follows:

SELECT e.cedularProveedor FROM Aporte e JOIN e.idPedido p WHERE p.idPedido = :idPedido

What is translated to:

SELECT e.cedular_proveedor FROM aporte e INNER JOIN Pedido p ON e.id_pedido = p.id_pedido WHERE e.id_pedido = ?

This way you get the contributions that correspond to a specific order.

    
answered by 10.06.2016 / 13:30
source
0

You do not show the code of the object Pedido , but it seems that you are confusing the object itself with its id.

The query should be:

SELECT e.cedulaProveedor FROM Aporte e, Pedido f where e.idPedido.id =1

I think the most logical thing would be to rename your attribute:

private Pedido idPedido;

By:

private Pedido pedido;

In this way, the query would be more consistent:

SELECT e.cedulaProveedor FROM Aporte e, Pedido f where e.pedido.id =1

I hope I have helped you.

    
answered by 10.06.2016 в 11:11
0

This JPQL query shows you the suppliers have responded to a single request:

SELECT aporte.cedulaproveedor, count( distinct aporte.pedido.idpedido ) 
FROM Aporte aporte 
GROUP BY aporte.cedulaproveedor HAVING count( distinct aporte.pedido.idpedido ) = 1

Principally, there are three useful clauses:

  • GROUP BY: allows you to group the contributions by each provider
  • The count distinct: allows you to know how many orders have been made by each provider
  • HAVING: allows you to specify that the number of orders per supplier you are looking for is 1

I have tested the query with the Eclipse Hibernate Tools plugin, very practical!

    
answered by 10.06.2016 в 14:45