Result of the GET method brings all the relations and the subrelations of the bank

2

I have an application in which I have a list screen

I'mdoingaGETmethodonmySpringBootAPI

Classcontroller

@RequestMapping(method=RequestMethod.GET,value="/distritos", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Collection<Distritos>> buscarTodosDistritos() {
        Collection<Distritos> distritosBuscados = distritosService.buscarTodos();
            return new ResponseEntity<>(distritosBuscados, HttpStatus.OK);
    } 

Service Class

public Collection<Distritos> buscarTodos(){
                return distritosRepository.findAll();
            } 

Repository Class

@Repository
public interface DistritosRepository extends JpaRepository<Distritos, Integer> {

} 

class Entity

@Entity
@Table(name = "distritos", schema="glb")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Distritos.findAll", query = "SELECT d FROM Distritos d"),
    @NamedQuery(name = "Distritos.findByIdDistrito", query = "SELECT d FROM Distritos d WHERE d.idDistrito = :idDistrito"),
    @NamedQuery(name = "Distritos.findByNome", query = "SELECT d FROM Distritos d WHERE d.nome = :nome"),
    @NamedQuery(name = "Distritos.findByCodigoDne", query = "SELECT d FROM Distritos d WHERE d.codigoDne = :codigoDne"),
    @NamedQuery(name = "Distritos.findByFlagAtivo", query = "SELECT d FROM Distritos d WHERE d.flagAtivo = :flagAtivo")})




@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "idDistrito", scope= Distritos.class)        
public class Distritos implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id_distrito")
    private int idDistrito;


    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 70)
    @Column(name = "nome")
    private String nome;


    @Size(max = 8)
    @Column(name = "codigo_dne")
    private String codigoDne;


    @Column(name = "flag_ativo")
    private Integer flagAtivo;


    @JoinColumn(name="idEntidade")
    @ManyToOne
    private Entidades entidade;


    @JoinColumn(name="idMunicipio")
    @ManyToOne(optional = false)
    private Municipios municipio;


    @JoinColumn(name="idUf")
    @ManyToOne(optional = false)
    private Ufs uf;

    public Distritos() {
    }

   gets and sets....
   } 

And in my front end JS I have

carregarDados = function() {

        token = localStorage.getItem("userToken");

        $http({
            method : 'GET',
            url : 'http://localhost:8080/user/distritos'
        }).then(function(response) {
            $scope.distritos = response.data;

        }, function(response) {
            console.log(response.data);
            console.log(response.status);
        });
    }; 

The problem is that the return of the GET method causes all relations, causing an excessive delay, and their subrelations, being that I only need the name of the State, name of the Municipality and name of the Entity.

The strange thing is that the result of the second listing of the array brings exactly the information I need without subre- lations.

Look at the image.

How can I resolve this issue by bringing only the information I want on the screen?

    
asked by anonymous 05.10.2017 / 16:04

1 answer

0

The error was in my related classes, for example, in municipality, entity and uf I had a @OneToMany mapping and within those classes plus @OneToMany mappings that were unnecessary. So when I was doing GET in districts I had a response with all the mappings, including bidirectional ones that were unnecessary, causing a slowness.

    
06.10.2017 / 20:46