【问题标题】:JPA 2.1 @SqlResultSetMapping binding inner class to targetclassJPA 2.1 @SqlResultSetMapping 将内部类绑定到目标类
【发布时间】: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&lt;SurveyQuestion&gt; 字段?

调查问题:

public class SurveyQuestion {
    private Long surveyquestionid;
    private String surveyquestion;
    private List<String> surveyanswers;
}

而且,非常相似。我将如何映射List&lt;String&gt;

尝试映射到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类的代码+用于SurveySurveyQuestion的相对映射吗?
  • 只是为了澄清,impl。是 EclipseLink 不是 Hibernate。
  • @O.Badr,添加了 Poll 类。调查和调查问题的映射已经存在。
  • 能否给出用于映射到目标类的查询(例如EventSurveysMapping)?
  • 我也有类似的要求。你找到方法了吗?

标签: java-8 jpa-2.1


【解决方案1】:

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

@OneToMany
private Set<SurveyQuestion> surveyanswers;

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-30
  • 1970-01-01
  • 1970-01-01
  • 2011-01-20
  • 1970-01-01
  • 2014-10-06
相关资源
最近更新 更多