SQL code problem in the @Query java annotation

Asked

Viewed 61 times

-1

I am trying to perform a command through Inner Join in an annotation @Query in Java, but an error occurs. The code usually works when performing an SQL query without using Inner Join. It follows the codes used:

REPOSITORY/PRODUCT INTERFACE METHOD:

@Query(value = "select tb_produto.id, tb_produto.marca, tb_produto.nome from tb_produto 
inner join tb_tipo_eletro on tb_produto.tipoeletro_id = tb_tipo_eletro.id 
where tipoeletro_id = :id and preco <= :preco", nativeQuery = true)
    public List<Produto> filtragem (@Param("id") long id, @Param("preco" ) float preco);

PUBLICATION OF THE METHOD IN THE CONTROLLER PRODUCT CLASS:

@GetMapping("/filtragem/{id}/{preco}")
    public ResponseEntity<List<Produto>> filtragem(@PathVariable long id, @PathVariable float preco) {
        return ResponseEntity.ok(produto.filtragem(id, preco));

1 answer

0

Try to query like this:

@Query(value = "select p.id, p.marca, p.nome " +
    "from tb_produto as p " +
    "inner join tb_tipo_eletro as te on p.tipoeletro_id = te.id " +
    "where p.tipoeletro_id = :id and preco <= :preco", nativeQuery = true)
public List<Produto> filtragem(@Param("id") long id, @Param("preco") float preco);

One detail that is important to validate is whether this column preco exists in both columns. If it exists it is important to put the alias of the table in which you want to query. That is, it would look like this:

p.preco or te.preco

If possible, it would be important to post the error that is happening here too (message, stacktrace and etc.) to help identify the error

Browser other questions tagged

You are not signed in. Login or sign up in order to post.