是否有可能將內部類映射到targetclass
,如果可能的話,它是如何完成的?我是新來這個@SqlResultSetMapping
功能:JPA 2.1 @SqlResultSetMapping綁定內部類到目標類
@SqlResultSetMapping(
name = "EventSurveysMapping",
classes = {
@ConstructorResult(
targetClass = Survey.class,
columns = {
@ColumnResult(name = "surveyid", type = Long.class),
})
})
所以targetClass
Survey.class
有:
public class Survey {
private Long surveyid;
private List<SurveyQuestion> surveyquestions;
// constructor with mapped fields
}
我怎麼會映射List<SurveyQuestion>
場?
SurveyQuestion:
public class SurveyQuestion {
private Long surveyquestionid;
private String surveyquestion;
private List<String> surveyanswers;
}
而且,和非常相似。我將如何映射List<String>
?
我想什麼時候做映射List.class
得到一個例外:
@SqlResultSetMapping(
name = "EventPollsMapping",
classes = {
@ConstructorResult(
targetClass = Poll.class,
columns = {
@ColumnResult(name="pollid", type = Long.class),
@ColumnResult(name="questionid", type = Long.class),
@ColumnResult(name="pollquestion", type = String.class),
@ColumnResult(name="pollanswers", type = List.class) // this mapping is the cause of the exception
})
})
例外:
org.eclipse.persistence.exceptions.ConversionException異常 說明:對象[它是Primary ID,它是唯一ID],類 [class java.lang.String],無法轉換爲[interface java.util.List]
投票:
@XmlRootElement
@XmlType (propOrder={"pollid",
"pollquestionid",
"pollquestion",
"pollanswers"
})
public class Poll {
private Long pollid;
private Long pollquestionid;
private String pollquestion;
private List<String> pollanswers;
public Poll(){}
public Poll(Long pollid, Long pollquestionid, String pollquestion, List<String> pollanswers) {
super();
this.pollid = pollid;
this.pollquestionid = pollquestionid;
this.pollquestion = pollquestion;
this.pollanswers = pollanswers;
}
// setters & getters
}
您能否顯示'Poll'類的代碼+用於'Survey'和'SurveyQuestion'的相關映射? –
只是爲了澄清,impl。是EclipseLink而不是Hibernate。 –
@ O.Badr,添加了投票類。調查和調查問題的映射已經存在。 –