gpt4 book ai didi

java-8 - JPA 2.1 @SqlResultSetMapping 将内部类绑定(bind)到目标类

转载 作者:行者123 更新时间:2023-12-04 08:51:05 26 4
gpt4 key购买 nike

是否可以将内部类映射到 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> field ?

调查问题:
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
})
})

异常(exception):

org.eclipse.persistence.exceptions.ConversionException Exception Description: The object [It is Primary ID, It is unique ID], of class [class java.lang.String], could not be converted to [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
}

最佳答案

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

@OneToMany
private Set<SurveyQuestion> surveyanswers;

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

关于java-8 - JPA 2.1 @SqlResultSetMapping 将内部类绑定(bind)到目标类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43548730/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com