JPQL Not recognizing the parameter

1

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

As the findByDescricão method is ignoring the parameter (description) the query is returning all the elements of the database referring to this table, without any criteria.

The Repository code shows the method that has a problem, although it is using the 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 is the code for the classes involved. Many thanks for the help.

@Resource class

  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);
}

Note: The Category class has only the Long id, String description field.

    
asked by anonymous 24.04.2018 / 10:35

1 answer

0

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

public class CategoryResource {

    @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 it was pointing to findAll() and not to findByDescricao(String descricao) made the fix, working perfectly.

    
24.04.2018 / 14:26