2017-04-21 49 views
7

是否有可能将内部类映射到targetclass,如果可能的话,它是如何完成的?我是新来这个@SqlResultSetMapping功能:JPA 2.1 @SqlResultSetMapping绑定内部类到目标类

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

所以targetClassSurvey.class有:

public class Survey { 
    private Long surveyid; 
    private List<SurveyQuestion> surveyquestions; 
// constructor with mapped fields 
} 

我怎么会映射List<SurveyQuestion>场?

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异常 说明:对象[它是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 
} 
+0

您能否显示'Poll'类的代码+用于'Survey'和'SurveyQuestion'的相关映射? –

+0

只是为了澄清,impl。是EclipseLink而不是Hibernate。 –

+0

@ O.Badr,添加了投票类。调查和调查问题的映射已经存在。 –

回答

1

以我的经验,当我不得不映射的事物的集合,最后我做了这样的事情:

@OneToMany 
private Set<SurveyQuestion> surveyanswers; 

所有这一切,如果你是使用支持基本类型集合的JPA提供程序的扩展。 (例如Hibernate有@CollectionOfElements注解)。

+0

Hibernate [@CollectionOfElements](https://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/annotations/CollectionOfElements.html)由[@ElementCollection](https://docs.jboss)取代.org/hibernate/jpa/2.1/api/javax/persistence/ElementCollection.html) –

+0

感谢您的回答,但这与我的问题没有多大关系,'@ ConstructorResult'与NON实体一起使用。 –