Problem using @Jsonignore Spring Boot

Asked

Viewed 656 times

1

I have a @Manytoone relationship between Manga and Author, where Many Manga can have an Author and an Author can have many Manga.

I would like to list all the Manga with each Author, and when searching for the Author, bring a list with all the Manga that the Author created.

Using @Jsonignore in the Author table, when I do GET in the Sleeves table, it returns All Sleeves with each author, but in the Author table, if GET does not return each Author Sleeve. Remove @Jsonignore, stay in a loop until error.

Sleeves:

@Entity
@Table(name = "Mangas")
public class MangasEntity {


    private Long id;
    private String nome;
    private  Status status;
    private Date dataLancado;   
    private AutorEntity autor;

    @ManyToOne  
    @JoinTable(name="mangas_autor",
    joinColumns=@JoinColumn(name="manga_id",referencedColumnName="id"),
    inverseJoinColumns = 
    @JoinColumn(name="autor_id",referencedColumnName="id"))
    public AutorEntity getAutor() {
        return autor;
    }

Author

@Entity
@Table(name = "Autor")
    public class AutorEntity {

    private Long id;
    private String nome;    
    @JsonIgnore
    private Set<MangasEntity> manga;

    @OneToMany(mappedBy="autor",targetEntity= MangasEntity.class,fetch = FetchType.LAZY)    
    @Cascade({CascadeType.ALL,CascadeType.DELETE})
    public Set<MangasEntity> getManga() {
        return manga;
    }

Autorrepository

public interface AutorRepository extends JpaRepository<AutorEntity, Long> {

    @Query("SELECT  a.id, a.nome FROM AutorEntity a  ") //Busca id e nome do Autor
    List<AutorEntity> buscarAutores();

    @Query("Select a FROM AutorEntity a") // Deve retornar cada Autor com seus mangas
    public List<AutorEntity> buscarAutorEMangas();

Result of the consultation in the table Sleeves

Error when viewing Mangas without @Jsonignore in Author table.

at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.8.5.jar:2.8.5]
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:149) ~[jackson-databind-2.8.5.jar:2.8.5]
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:112) ~[jackson-databind-2.8.5.jar:2.8.5]
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25) ~[jackson-databind-2.8.5.jar:2.8.5]
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:704) ~[jackson-databind-2.8.5.jar:2.8.5]
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:690) ~[jackson-databind-2.8.5.jar:2.8.5]
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.8.5.jar:2.8.5]
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:704) ~[jackson-databind-2.8.5.jar:2.8.5]
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:690) ~[jackson-databind-2.8.5.jar:2.8.5]
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.8.5.jar:2.8.5]
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:149) ~[jackson-databind-2.8.5.jar:2.8.5]
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:112) ~[jackson-databind-2.8.5.jar:2.8.5]
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25) ~[jackson-databind-2.8.5.jar:2.8.5]
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:704) ~[jackson-databind-2.8.5.jar:2.8.5]
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:690) ~[jackson-databind-2.8.5.jar:2.8.5]
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.8.5.jar:2.8.5]
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:704) ~[jackson-databind-2.8.5.jar:2.8.5]
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:690) ~[jackson-databind-2.8.5.jar:2.8.5]
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.8.5.jar:2.8.5]
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:149) ~[jackson-databind-2.8.5.jar:2.8.5]
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:112) ~[jackson-databind-2.8.5.jar:2.8.5]
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25) ~[jackson-databind-2.8.5.jar:2.8.5]
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:704) ~[jackson-databind-2.8.5.jar:2.8.5]
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:690) ~[jackson-databind-2.8.5.jar:2.8.5]
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.8.5.jar:2.8.5]
No answers

Browser other questions tagged

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