【发布时间】:2017-09-18 19:28:51
【问题描述】:
是否可以将内部类映射到targetclass,如果可能,它是如何完成的?我是这个 @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> 字段?
调查问题:
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,添加了 Poll 类。调查和调查问题的映射已经存在。
-
能否给出用于映射到目标类的查询(例如
EventSurveysMapping)? -
我也有类似的要求。你找到方法了吗?