So I have two entities, Recipe and User. In recipes we have the User object. User object has a list with all the Recipes that he has written.
Recipe Entity:
@Entity
@Table(name = "recipes")
public class Recipe {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
User Entity:
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@OneToMany(mappedBy = "user")
private List<Recipe> recipesWritten;
When I get the entity Recipe, it returns a User object, which has a list with all the Recipe objects that the user created and it goes on recursively.
I've seen the fix with @JsonManagedReference
which somehow fixes the issue but while debugging I noticed that the object still has recursion and I would like the JSON to include some stuff (without recursion).
Any help would be greatly appreciated
So I have two entities, Recipe and User. In recipes we have the User object. User object has a list with all the Recipes that he has written.
Recipe Entity:
@Entity
@Table(name = "recipes")
public class Recipe {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
User Entity:
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@OneToMany(mappedBy = "user")
private List<Recipe> recipesWritten;
When I get the entity Recipe, it returns a User object, which has a list with all the Recipe objects that the user created and it goes on recursively.
I've seen the fix with @JsonManagedReference
which somehow fixes the issue but while debugging I noticed that the object still has recursion and I would like the JSON to include some stuff (without recursion).
Any help would be greatly appreciated
Share Improve this question edited Nov 25, 2024 at 21:00 Mohamed amine ben hassen 8641 gold badge11 silver badges29 bronze badges asked Nov 20, 2024 at 12:53 Koulhs035Koulhs035 54 bronze badges 8- Did you use the JsonManagedReference with @JsonBackReference ? – jcompetence Commented Nov 20, 2024 at 13:24
- @jcompetence Yes, I did. Still the object while debugging had recursion inside. – Koulhs035 Commented Nov 20, 2024 at 13:28
- At what stage of debugging do you switch to recursion? Maybe this happens in the toString, equals, or hashCode functions? because these functions use all references objects by default (and it will be an endless loop). For example, to calculate the hashcode of a user, you need to calculate the hashcode of the id + hascode of the all recipes that contain the user, and so on. So, implement these functions yourself. – snej0keee Commented Nov 20, 2024 at 14:48
- 1 Please update your code in the question. There are no annotations JsonManagedReference nor JsonBackReference. – Mar-Z Commented Nov 20, 2024 at 14:56
- "I would like the JSON to include some stuff" - please specify what stuff. – Mar-Z Commented Nov 20, 2024 at 14:57
1 Answer
Reset to default 0I see that you should remove the @ManyToOne reference in the recipe class, I don't see why recipe should be mapped to user. The user class has reference to a list of recipes which is the right way and it's enough.
maybe avoid two-way mapping in your designs.