gpt4 book ai didi

java - 在任何情况下,父级都不会延迟加载子级

转载 作者:行者123 更新时间:2023-11-30 05:52:33 27 4
gpt4 key购买 nike

我正在开发一个 REST API,我有一个 Question 父级和 Answer(以及与两者关联的 User)子级。我想要实现的是,使用 questionService.getAllQuestions() 仅从数据库加载问题和用户。当我调用 QuestionService.getQuestion(id) 时,我希望从数据库加载该问题以及该问题包含的所有答案和用户(回答者)。所有数据都会被Spring-boot自动转换为JSON对象

但在我目前的情况下,即使使用 getAllQuestions(),所有数据都是从数据库加载的。这对于问题概述请求毫无意义,但对于问题详细信息请求是必需的。

问题父实体:

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;

import javax.persistence.*;
import java.sql.Timestamp;
import java.util.Set;

@Entity
@Table(name = "vragen")
public class Question extends BaseEntity {
@Column(name = "vraag")
private String question;

@Column(name = "bericht")
private String message;

@ManyToOne
@JoinColumn(name = "gebruiker")
private Gebruiker gebruiker;

@Column(name = "beantwoord", insertable = false)
private boolean answered;

@Column(name = "gepost_op", insertable = false)
private Timestamp postedOn;

@Column(name = "actief", insertable = false)
@JsonIgnore
private boolean active;

@OneToMany(mappedBy = "question")
@JsonManagedReference
private Set<Answer> answers;


public Question() {}

public Question(String question, String message, Gebruiker gebruiker) {
this.question = question;
this.message = message;
this.gebruiker = gebruiker;
}

public String getQuestion() {
return question;
}

public void setQuestion(String question) {
this.question = question;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public Gebruiker getGebruiker() {
return gebruiker;
}

public void setGebruiker(Gebruiker gebruiker) {
this.gebruiker = gebruiker;
}

public boolean isAnswered() {
return answered;
}

public void setAnswered(boolean answered) {
this.answered = answered;
}

public Timestamp getPostedOn() {
return postedOn;
}

public boolean isActive() {
return active;
}

public void setActive(boolean active) {
this.active = active;
}

public Set<Answer> getAnswers() {
return answers;
}

public void setAnswers(Set<Answer> answers) {
this.answers = answers;
}
}

应答实体:

import com.fasterxml.jackson.annotation.JsonBackReference;

import javax.persistence.*;
import javax.validation.constraints.Size;
import java.sql.Timestamp;

@Entity
@Table(name = "antwoorden")
public class Answer extends BaseEntity {
@Size(min = 10)
@Column(name = "bericht")
private String message;

@ManyToOne
@JoinColumn(name = "gebruiker")
private Gebruiker gebruiker;

@ManyToOne
@JoinColumn(name = "vraag")
@JsonBackReference
private Question question;

@Column(name = "gepost_op", insertable = false)
private Timestamp postedOn;

@Column(name = "actief", insertable = false)
private boolean active;


public Answer() {}

public Answer(String message, Gebruiker gebruiker, Question question) {

}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public Gebruiker getGebruiker() {
return gebruiker;
}

public void setGebruiker(Gebruiker gebruiker) {
this.gebruiker = gebruiker;
}

public Question getQuestion() {
return question;
}

public void setQuestion(Question question) {
this.question = question;
}

public Timestamp getPostedOn() {
return postedOn;
}

public boolean isActive() {
return active;
}

public void setActive(boolean active) {
this.active = active;
}
}

@OneToMany 默认情况下应该延迟加载答案,即使我明确设置该选项,这种情况也不会发生。

是否可以使用 JPA 实现此目的,还是需要在自定义实现中手动编写查询?

最佳答案

我注意到是 Jackson 造成的,特别是 @JsonManagedReference。删除它并添加 @JsonBackReference 可以使其正确延迟加载,尽管该注释在这里并未真正到位。

参见Configure Jackson to omit lazy-loading attributes in Spring Boot真正的解决方案

关于java - 在任何情况下,父级都不会延迟加载子级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53650310/

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