2

Following is the table structure:

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 classes:

 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;

 ......

Client code:

 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());
 }

console shows:

surveys size = 1 getting responses from the survey object now.. responses size= ..0

whereas there are 7 responses in the DB for the selected survey.

4

3 回答 3

3

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

从您不完整的代码中很难看出,但一般来说,OneToMany 不应该使用 JoinColumn,它应该使用 mappedBy,并且 ManyToOne 中的连接列应该引用对象的 Id。

还要确保在插入对象时设置关系的双方。

于 2012-10-15T13:51:30.933 回答
0

这个链接有帮助。我将集合设置回惰性。在
持久性类中,在获得结果集后的事务中,我现在将 getResponses 称为:

List<CustomerSurvey> surveys = query.getResultList();
for (CustomerSurvey survey : surveys) {
    Set<SurveyResponse> responses = survey.getResponses();
}

填充响应。

于 2012-10-24T18:49:22.000 回答
0

另一种选择是使用 jpql fetch 查询填充关系:

“从调查调查中选择调查加入获取调查.responses”

我有同样的问题,但仍然无法找出问题所在。在我的情况下,父实体是由 jpql 查询加载的,因此扩展它提供了一种解决方法

于 2013-06-07T15:07:26.093 回答