JPA 2.1 @SqlResultSetMapping将内部类绑定到targetclass

est*_*con 8 java-8 jpa-2.1

是否有可能将内部类映射到targetclass,如果可能的话,它是如何完成的?我是这个@SqlResultSetMapping功能的新手:

@SqlResultSetMapping(
        name = "EventSurveysMapping",
        classes = {
                @ConstructorResult(
                        targetClass = Survey.class,
                        columns = {
                                @ColumnResult(name = "surveyid", type = Long.class),
                        })
        })
Run Code Online (Sandbox Code Playgroud)

所以targetClass Survey.class有:

public class Survey {
    private Long surveyid;
    private List<SurveyQuestion> surveyquestions;
// constructor with mapped fields
}
Run Code Online (Sandbox Code Playgroud)

我该如何绘制该List<SurveyQuestion>字段?

SurveyQuestion:

public class SurveyQuestion {
    private Long surveyquestionid;
    private String surveyquestion;
    private List<String> surveyanswers;
}
Run Code Online (Sandbox Code Playgroud)

而且,非常相似.我该如何映射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
                        })
        })
Run Code Online (Sandbox Code Playgroud)

例外:

org.eclipse.persistence.exceptions.ConversionException异常说明:类[类java.lang.String]的对象[它是主ID,它是唯一ID]无法转换为[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 
}
Run Code Online (Sandbox Code Playgroud)

Dam*_*ero 1

根据我的经验,当我必须映射一系列事物时,最后我做了这样的事情:

@OneToMany
private Set<SurveyQuestion> surveyanswers;
Run Code Online (Sandbox Code Playgroud)

如果您使用支持基本类型集合的 JPA 提供程序的扩展,则所有这些都是如此。(例如 Hibernate 有 @CollectionOfElements 注释)。