在Hibernate文档的性能部分中提到:
我不明白它如何解决问题。可以给出一个现实世界的例子和解释吗?解决N+1查询问题的完全不同的方法是使用二级缓存。
@Entity(name = "Post")
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "post")
private List<Comment> comments = new ArrayList<>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Comment> getComments() {
return comments;
}
public void addComment(Comment comment) {
comments.add(comment);
comment.setPost(this);
}
}
@Entity(name = "Comment")
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
private Post post;
public Comment() {
}
public Comment(String review) {
this.review = review;
}
private String review;
public Long getId() {
return id;
}
public Post getPost() {
return post;
}
public void setPost(Post post) {
this.post = post;
}
public void setReview(String review) {
this.review = review;
}
}
List<Comment> comments = session.createQuery(
"select c from Comment c ").list();
for(Comment comment : comments) {
Post post = comment.getPost();
}
然后,对于每个评论,您需要运行一个额外的查询来获取关联的评论文章。
如果启用第二级缓存:
@Entity(name = "Post")
@Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Post {
...
}
List<Comment> comments = session.createQuery(
"select c from Comment c fetch c.post ").list();