2017-04-21 45 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實體一起使用。 –