spring data jpa项目,其中我有两个具有一对多双向映射的实体,我试图懒惰地感染对象,但作为响应,就像渴望获取一样。
响应:
[ { “ id”:1 “ title”:“ kyoot”, “ postComments”:[ { “ id”:1 “ review”:“ acsad”, “帖子”:1 }, { “ id”:2 “ review”:“ cadfs”, “帖子”:1 } ] }, { “ id”:2 “ title”:“ afhv”, “ postComments”:[ { “ id”:3, “评论”:“ vdv”, “帖子”:2 }, { “ id”:4 “ review”:“ acs”, “帖子”:2 } ] } ]
The classes:
@Entity
@Table(name = "post_comment")
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="id",scope = PostComment.class)
public class PostComment implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@Column(name = "id")
@GeneratedValue
private Long id;
@Column(name = "review")
private String review;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id",nullable = false)
private Post post;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getReview() {
return review;
}
public void setReview(String review) {
this.review = review;
}
public Post getPost() {
return post;
}
public void setPost(Post post) {
this.post = post;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PostComment other = (PostComment) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
package com.srinivas.assetz.domain;
import java.io.Serializable;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
@Entity
@Table(name = "post")
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="id",scope = Post.class)
public class Post implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "title")
private String title;
@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true,fetch = FetchType.LAZY)
private List<PostComment> postComments;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<PostComment> getPostComments() {
return postComments;
}
public void setPostComments(List<PostComment> postComments) {
this.postComments = postComments;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Post other = (Post) obj;
if (id != other.id)
return false;
return true;
}
}
@Repository
public interface PostRepository extends CrudRepository<Post, Integer> {
}
public interface PostService {
public Iterable<Post> getAllPosts();
}
@Service
public class PostServiceImpl implements PostService {
@Autowired
PostRepository postRepository;
@Override
@Transactional
public Iterable<Post> getAllPosts() {
// TODO Auto-generated method stub
return postRepository.findAll();
}
}
答案 0 :(得分:0)
在这一部分中,您仅对实体PostComment懒惰:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id",nullable = false)
private Post post;
但是您的实体Post就是这样(您在那里没有懒惰):
@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
private List<PostComment> postComments;
这就是为什么在检索Post时也会得到PostComment的原因。因此,为了避免在实体Post上包含懒惰,就像这样:
@OneToMany(fetch=FetchType.LAZY, mappedBy = "post", cascade = CascadeType.ALL,
orphanRemoval = true)
private List<PostComment> postComments;