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]
Will the solution be the same? Thank you.
– Braian Silva
I was having the same problem, that solved.
– DiegoAugusto
I switched to Unidirectional the Mangas table, @Manytoone Mangas for Author, and it worked normal, now I’m having problems with JPQL, is bringing Repeated Sleeves.JPQL I want to consult the AUTHOR and Concatenate the sleeves, I’ve used CONCAT and nothing, Returns Repeated values, This is the query in MYSQL
– Braian Silva
@Diegoaugusto I found another way to solve this problem about recursion, I used @ Jsonignoreproperties in Entity Mangas and Author.Get Manga and Author
– Braian Silva