gpt4 book ai didi

JPA @OneToMany 不获取 child

转载 作者:行者123 更新时间:2023-12-04 05:29:02 24 4
gpt4 key购买 nike

表结构如下:

desc customer_survey
Name Null Type
----------- -------- ------------
SURVEYID NOT NULL VARCHAR2(10)
CUSTNO NOT NULL VARCHAR2(10)
SRNO NUMBER(10)
AVGRATINGS NUMBER(5,2)
COMMENTS VARCHAR2(50)
SENTON DATE
RESPONDEDON DATE

desc Survey_response
Name Null Type
---------------- -------- ------------
SURVEYRESPONSEID NOT NULL NUMBER(10)
RATINGS NOT NULL NUMBER(2)
QNO NOT NULL VARCHAR2(10)
SURVEYID NOT NULL VARCHAR2(10)

Java 类:

 public class CustomerSurvey implements Serializable {

@OneToMany(fetch=FetchType.EAGER, mappedBy="customerSurvey",
cascade=CascadeType.ALL)
private Set<SurveyResponse> responses;
......

public class SurveyResponse {

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="SURVEYID", referencedColumnName="surveyId")
private CustomerSurvey customerSurvey;

......

客户端代码:

 List<CustomerSurvey> surveys = workService.getSurveysByCustomer("testCNo2");
System.out.println("surveys size = " + surveys.size());

for(CustomerSurvey survey: surveys) {
System.out.println("getting responses from the survey object now..");
Set<SurveyResponse> responses = survey.getResponses();
System.out.println("responses size= .." + responses.size());
}

控制台显示:

调查规模 = 1 现在从调查对象那里得到回应.. 响应大小= ..0

而数据库中有 7 个针对所选调查的回复。

最佳答案

启用日志记录并查看 SQL 是否正确。

很难从您不完整的代码中分辨出来,但通常 OneToMany 不应使用 JoinColumn,而应使用 mappedBy,而 ManyToOne 中的连接列应引用对象的 ID。

同时确保在插入对象时设置关系的两边。

关于JPA @OneToMany 不获取 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12866209/

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