Note: Please don't mark it as duplicate before reading completely
Case : I have three classes named User, Post and Tag
User <-> Post (OneToMany Bi-directional)
Post <-> Tag (ManyToMany)
Solution I want :
Mapping should work like If i call
getUserById
, I should get posts related to the user and tags related to the posts.Same with Posts and Tags, If I call
getPostById
I should get the user and tags and if I callgetTagByName
I should get all posts related to tags
Solutions I have tried :
@JsonMappedReference
,@JsonBackReference
- Worked for read operations but failed for creating/writing@JsonIdentityInfo
- Did not worked@JsonIgnore
- Worked but I don't want to ignore as am not getting desired solution mentioned above@ToString.Exclude
,@EqualsAndHashCode.Exclude
- Did not workedAlso tried with my own
getters
andsetters
and@ToString
methods - Did not worked either
This is a springboot project
Here are my classes
User.java
@Entity
@Table(name = "user")
@Data
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "username")
private String userName;
@Column(name = "password")
private String password;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "created_at")
@CreationTimestamp
private Timestamp createdAt;
@Column(name = "updated_at")
@UpdateTimestamp
private Timestamp updatedAt;
@OneToMany(
mappedBy = "user",
cascade = {CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH})
private List<Post> posts;
Post.java
@Entity
@Table(name = "post")
@Data
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@ManyToOne(
cascade = {CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH})
@JoinColumn(name = "user_id")
private User user;
@Column(name = "title")
private String postTitle;
@Column(name = "content")
private String postContent;
@Column(name = "status")
private String postStatus;
@Column(name = "created_at")
@CreationTimestamp
private Timestamp createdAt;
@Column(name = "updated_at")
@UpdateTimestamp
private Timestamp updatedAt;
@ManyToMany(
cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH})
@JoinTable(
name = "post_tag",
joinColumns = @JoinColumn(name = "post_id"),
inverseJoinColumns = @JoinColumn(name = "tag_id"))
private List<Tag> tags;
Tag.java
@Entity
@Table(name = "tag")
@Data
public class Tag {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "name")
private String tagName;
@Column(name = "created_at")
@CreationTimestamp
private Timestamp createdAt;
@Column(name = "updated_at")
@UpdateTimestamp
private Timestamp updatedAt;
@ManyToMany(
cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH})
@JoinTable(
name = "post_tag",
joinColumns = @JoinColumn(name = "tag_id"),
inverseJoinColumns = @JoinColumn(name = "post_id"))
private List<Post> posts;
So with above classes I ran into infinite loop problem, If I use getUserById
post object is user object is showing Unable to evaluate the expression Method threw 'org.hibernate.LazyInitializationException' exception.
If call getAllPosts
OR getAllTags
tags object in post object is showing the same error or vice versa