JPQL Not recognizing the parameter

Asked

Viewed 167 times

1

I need to perform a query using JPQL or Spring-data keyword method. The problem is that the parameter inserted in the query is being ignored, I still could not identify the cause of it.

Since the findByDescription method is ignoring the parameter (Description) the query is returning all elements of the database referring to this table, with no criteria.

Repository code shows the method that is problematic, although it is using keyword methodology, I also tried using JPQL:

@Query ("SELECT obj FROM Categoria obj WHERE descricao LIKE %:descricao%")
    List<Categoria> findByDescricao(@Param("descricao") String descricao);

The result was the same. Below follows the code of the classes involved. Thank you very much for the help.

Class @Resource

  import java.net.URI;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Optional;

    import javax.validation.Valid;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

    import com.eclodir.voucomprei.model.dto.CategoriaDTO;
    import com.eclodir.voucomprei.model.entity.Categoria;
    import com.eclodir.voucomprei.service.CategoriaService;

    @RestController
    @RequestMapping (value="/categorias")
    public class CategoriaResource {

        @Autowired
        CategoriaService categoriaService;

        @GetMapping
        public ResponseEntity<List<CategoriaDTO>> findByDescricao (@RequestParam (value="descricao", defaultValue="") String descricao) {
            List<CategoriaDTO> categoriasDTO = new ArrayList<>();
            categoriasDTO = categoriaService.findAll();
            return ResponseEntity.ok().body(categoriasDTO);
        }

    }

The second class (@Service):

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eclodir.voucomprei.exception.ObjectNotFoundException;
import com.eclodir.voucomprei.model.dto.CategoriaDTO;
import com.eclodir.voucomprei.model.entity.Categoria;
import com.eclodir.voucomprei.repository.CategoriaRepository;
import com.eclodir.voucomprei.service.interfaces.CategoriaServiceInterface;

@Service
public class CategoriaService implements CategoriaServiceInterface {

    private static final Logger log = LoggerFactory.getLogger(CategoriaService.class);

    @Autowired
    CategoriaRepository categoriaRepository;


    @Override
    public List<CategoriaDTO> findByDescricao(String descricao) {
        List<Categoria> categorias = categoriaRepository.findByDescricaoContaining(descricao);
        if (categorias.isEmpty()) {
            throw new ObjectNotFoundException("Categoria não encontrada");
        }
        List<CategoriaDTO> categoriasDTO = new ArrayList<>();
        for (Categoria categoria : categorias) {
            categoriasDTO.add(toDTO(categoria));
        }
        return categoriasDTO;
    }

    @Override
    public Categoria fromDTO(CategoriaDTO categoriaDTO) {
        Categoria categoria = new Categoria();
        categoria.setDescricao(categoriaDTO.getDescricao());
        return categoria;
    }

    @Override
    public CategoriaDTO toDTO(Categoria categoria) {
        CategoriaDTO categoriaDTO = new CategoriaDTO();
        categoriaDTO.setId(categoria.getId());
        categoriaDTO.setDescricao(categoria.getDescricao());
        return categoriaDTO;
    }


}

The interface (Repository):

package com.eclodir.voucomprei.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.eclodir.voucomprei.model.entity.Categoria;

@Repository
public interface CategoriaRepository extends JpaRepository<Categoria, Long> {

    List<Categoria> findByDescricaoContaining(@Param("descricao") String descricao);
}

Obs.: The Category class has only the fields Long id, String Description.

  • A kick, already tried to change your string to: @Query ("SELECT obj FROM Categoria obj WHERE obj.descricao LIKE '%:descricao%'") where the description is part of the obj and its parameter within ' ' for being String

  • Try activating Hibernate logs and see which query is being generated.

  • I just tried Paul, the same result. O Log Leonardo: Hibernate: select categoria0_.id as id1_0_, categoria0_.Description as descrica2_0_, categoria0_.dt_insert as dt_inser3_0_, categoria0_.dt_update as dt_updat4_0_ from category categoria0_ It’s like he doesn’t recognize the Where clause.

  • Try: "SELECT c FROM Categoria c WHERE c.descricao LIKE CONCAT('%',:descricao,'%')"

  • I found out, the problem was in the Resource class. The method was not pointed to the correct query, it was pointing findAll() so the error. I made the correction and it is already working.

1 answer

0


The class method below was pointing to the wrong search method, so the parameterized search was not working:

public class Categoriaresource {

    @Autowired
    CategoriaService categoriaService;

    @GetMapping
    public ResponseEntity<List<CategoriaDTO>> findByDescricao (@RequestParam (value="descricao", defaultValue="") String descricao) {
        List<CategoriaDTO> categoriasDTO = new ArrayList<>();
        categoriasDTO = categoriaService.findAll();
        return ResponseEntity.ok().body(categoriasDTO);
    }

}

As you can see he was pointing to the findAll() and not to the findByDescricao(String descricao) made the correction, working perfectly.

  • you can accept your own answer as the correct one :)

Browser other questions tagged

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