首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JPA 2.1 @SqlResultSetMapping将内部类绑定到to类

JPA 2.1 @SqlResultSetMapping将内部类绑定到to类
EN

Stack Overflow用户
提问于 2017-04-21 17:28:10
回答 1查看 1.4K关注 0票数 8

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

代码语言:javascript
复制
@SqlResultSetMapping(
        name = "EventSurveysMapping",
        classes = {
                @ConstructorResult(
                        targetClass = Survey.class,
                        columns = {
                                @ColumnResult(name = "surveyid", type = Long.class),
                        })
        })

因此,targetClass Survey.class有:

代码语言:javascript
复制
public class Survey {
    private Long surveyid;
    private List<SurveyQuestion> surveyquestions;
// constructor with mapped fields
}

如何映射List<SurveyQuestion>字段?

SurveyQuestion:

代码语言:javascript
复制
public class SurveyQuestion {
    private Long surveyquestionid;
    private String surveyquestion;
    private List<String> surveyanswers;
}

而且非常相似。我将如何映射List<String>

在尝试映射到List.class时,我得到了一个异常:

代码语言:javascript
复制
@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

民意调查:

代码语言:javascript
复制
@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 
}
EN

回答 1

Stack Overflow用户

发布于 2017-05-08 22:06:57

根据我的经验,当我必须绘制一个集合的地图时,最后我做了这样的事情:

代码语言:javascript
复制
@OneToMany
private Set<SurveyQuestion> surveyanswers;

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43548730

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档