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": [
{ ...