Result of the GET method brings all database relations and subrelations

Asked

Viewed 96 times

2

I have an application where I have a listing screen

inserir a descrição da imagem aqui

I’m doing a GET method on my Spring Boot API

Controller class

@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 method GET brings all relations, causing an excessive delay, and its subrelations, and I need only the name of the State, name of the Municipality and name of the Entity.

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

See in the image.

inserir a descrição da imagem aqui

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

1 answer

0

The error was in my related classes, for example, in municipality, entity and Uf I had a mapping @OneToMany and within these classes more mapping @OneToMany that were unnecessary. Soon, when I made a GET in districts I had a sponse with all the mappings, including bidirectional ones that were unnecessary, causing a slowness.

Browser other questions tagged

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