Infinite looping in bidirectional relationship - Manytomany

Asked

Viewed 341 times

0

I’m building a Rest API in Springboot. In it I have 2 related classes, and when I do a get in one of them they return me a looping Infinite through your foreign keys

I’ve tried using the fetchType.Lazy but with no results.

They relate through an item as this category will be from a later project.

User class

@Entity
@Table(name = "tb_user")
public class UserListed {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "cd_user")
private Long id;

@Size(max = 50)
@NotBlank
@Column(name = "nm_user")
private String name;

@Size(max = 21)
@NotBlank
@Column(name = "nm_username", unique = true)
private String username;

@Size(max = 120)
@NotBlank
@Column(name = "nm_email", unique = true)
private String email;

@Size(max = 255, min = 8)
@NotBlank
@Column(name = "nm_password")
private String password;

@Size(max = 120)
@Column(name = "ds_biography")
private String biography;

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "category_user",joinColumns = 
    {@JoinColumn(name = "cd_user")}, inverseJoinColumns = 
    {@JoinColumn(name = "cd_category")})
private List<Category> categories;
// get e set

Class Category

@Entity
@Table(name = "tb_category")
public class Category {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "cd_category")
    private Long id;

    @Size(max = 24)
    @NotEmpty
    @Column(name = "nm_category")
    private String name;

    @ManyToOne
    @JoinColumn(name = "cd_project")
    private Project project;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "category_user", joinColumns = 
            {@JoinColumn(name = "cd_category")}, inverseJoinColumns  =
            {@JoinColumn(name = "cd_user")})
    private List<UserListed> users;
    //get e set

Return:

{
    "id": 9,
    "name": "greatcode",
    "username": "@greatcodeGC",
    "email": "[email protected]",
    "password": "$2a$10$pIYyhZo/iUKtq99g.Mbl1eYns/ABQddhFxvxDIeZ/Id2JFra9xBNO",
    "biography": "Uma empresa criada por jovens estudantes de TI",
    "categories": [
        {
            "id": 2,
            "name": "ScrumMaster",
            "project": null,
            "users": [
                {
                    "id": 9,
                    "name": "greatcode",
                    "username": "@greatcodeGC",
                    "email": "[email protected]",
                    "password": "$2a$10$pIYyhZo/iUKtq99g.Mbl1eYns/ABQddhFxvxDIeZ/Id2JFra9xBNO",
                    "biography": "Uma empresa criada por jovens estudantes de TI",
                    "categories": [
                        { ...

1 answer

0


You can jot down the relationship of their entities with @JsonManagedReference and @JsonBackReference:

  • @JsonManagedReference is placed in the part of the reference that wants to advance in serialization. That is, the part that is serialized normally.
  • @JsonBackReference is placed in the part of the reference that does not want in the serialization. That is, the part that will be omitted.

In your case, write down the relationship of User with Category with @JsonManagedReference and of Category with User with @JsonBackReference.

Another alternative is to use the @JsonIgnore on one side of the relationship:

@JsonIgnore
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "category_user", joinColumns = 
        {@JoinColumn(name = "cd_category")}, inverseJoinColumns  =
        {@JoinColumn(name = "cd_user")})
private List<UserListed> users;

However, I suggest that the best way to deal with this problem is not to serialize entities; create a class "DTO" (UserResponse, CategoryResponse, etc), choose only the fields you want to return and convert the values of the entities to these Dtos. Thus, the changes you make to the entity will not affect your API.

Browser other questions tagged

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