gpt4 book ai didi

java - 延迟加载在 spring data jpa 一对多双向映射中不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 09:59:04 24 4
gpt4 key购买 nike

spring data jpa 项目,其中我有两个具有一对多双向映射的实体,我试图惰性地影响对象,但响应就像急切的获取。

回复:

[ { “id”:1, “标题”:“kyoot”, “发表评论”:[ { “id”:1, “评论”:“acsad”, “帖子”:1 }, { “id”:2, “评论”:“cadfs”, “帖子”:1 } ] }, { “id”:2, “标题”:“afhv”, “发表评论”:[ { “id”:3, “评论”:“vdv”, “帖子”:2 }, { “id”:4, “评论”:“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();
}

}

最佳答案

这部分你只对实体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;

关于java - 延迟加载在 spring data jpa 一对多双向映射中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53731144/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com