是否有可能将内部类映射到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>字段?
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异常描述:它是主ID的对象,它是类java.lang.String的唯一ID,无法转换为接口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
}发布于 2017-05-08 22:06:57
根据我的经验,当我必须绘制一个集合的地图时,最后我做了这样的事情:
@OneToMany
private Set<SurveyQuestion> surveyanswers;如果您使用的是支持基本类型集合的JPA提供程序的扩展,那么所有这些都会发生。(例如,Hibernate有@CollectionOfElements注释)。
https://stackoverflow.com/questions/43548730
复制相似问题