im trying to return to a JPA data (converted to DTO, ofcourse) where it has a @OneToMany
and @ManyToOne
bidirectional relationship. Im currently apply thing fix. The problem is that the output is recusrive. comments has post has comments then has posts (comments -> post -> coments -> so on..).
I only wnat to have something like this
{
"post_id": 1
"user": {
// user data
},
"description": "some description",
"images": "{images,images}",
"tags": "{tags, tags}",
"comments": [
{
//some comments data
},
{
//some comments data
}
]
"lastUpdated": "2020-04-08T14:23:18.000+00:00"
}
Here are my code
This is my Posts.class
@Data
@Entity
@Table(name = "posts")
@EntityListeners(AuditingEntityListener.class)
public class Posts {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "post_id")
private Long post_id;
@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
private Users user;
@Column(name = "description")
private String description;
@Column(name="images")
private String images;
@Column(name="tags")
private String tags;
@OneToMany(
fetch = FetchType.LAZY,
mappedBy = "post",
cascade = CascadeType.ALL,
orphanRemoval = true
)
@JsonIgnoreProperties(value = { "comments" ,"hibernateLazyInitializer", "handler" }, allowSetters = true)
//@JsonManagedReference
private List<Comments> comments;
@LastModifiedDate
@Column(name = "last_updated")
private Date lastUpdated;
public void addComments(Comments comment) {
this.comments.add(comment);
}
}
Here is my PostDTO.class
@Data
public class PostDTO {
private Long post_id;
private UserDTO user;
private String description;
private String images;
private String tags;
private List<CommentsDTO> comments;
private Date lastUpdated;
}
This is my Comments.class
@Data
@Entity
@Table(name = "comments")
@EntityListeners(AuditingEntityListener.class)
public class Comments {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="comment_id")
private Long comment_id;
@OneToOne
@JoinColumn(name = "user_id")
private Users user;
@Column(name = "description")
private String description;
@Column(name="images")
private String images;
@ManyToOne
@JoinColumn(name ="post_id" , nullable = false)
@JsonIgnoreProperties(value = { "post" ,"hibernateLazyInitializer", "handler" }, allowSetters = true)
//@JsonBackReference
private Posts post;
@Column(name="last_updated")
@LastModifiedDate
private Date lastUpdated;
}
Here is my CommentsDTO.class
@Data
public class CommentsDTO {
private Long comment_id;
private UserDTO user;
private String description;
private PostDTO post;
private String images;
private Date lastUpdated;
}
Here is my REST Controller
@GetMapping
public @ResponseBody ResponseEntity<List<PostDTO>> getAll() throws Exception {
return new ResponseEntity<List<PostDTO>>(service.getAll(), HttpStatus.OK);
}
Here is my service
public List<PostDTO> getAll() throws Exception {
return repo.findAll()
.stream()
.map(this::convert)
.collect(Collectors.toList());
}
private PostDTO convert(Posts e) {
return mapper.map(e, PostDTO.class);
}
Hope someone can shed light on my issue. Kinda lost as of this time.