JPA 2.1 @sqlresultSet映射绑定内部类与TargetClass



是否可以将内部类映射到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异常 描述:对象[是主要ID,它是唯一ID],类别为class [类Java.lang.String],无法转换为[接口 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 
}

根据我的经验,当我不得不绘制一件事情时,我终于做了这样的事情:

@OneToMany
private Set<SurveyQuestion> surveyanswers;

如果您使用的是JPA提供商支持基本类型集合的所有这些。(例如Hibernate具有@CollectionFolements注释(。

最新更新