2017-04-21 14 views
7

là nó có thể ánh xạ các lớp bên trong để targetclass, nếu có thể, nó được thực hiện như thế nào? Tôi mới đến @SqlResultSetMapping chức năng này:JPA 2.1 @SqlResultSetMapping ràng buộc lớp bên trong để targetclass

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

Vì vậy, các targetClassSurvey.class có:

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

Làm thế nào tôi sẽ lập bản đồ các lĩnh vực List<SurveyQuestion>?

SurveyQuestion:

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

Ngoài ra, và rất giống nhau. Làm cách nào để tôi lập bản đồ cho một List<String>?

tôi nhận được một ngoại lệ khi cố gắng làm bản đồ để 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 
         }) 
     }) 

Ngoại lệ:

org.eclipse.persistence.exceptions.ConversionException Exception Mô tả: Đối tượng [Nó là ID chính, là ID duy nhất], thuộc lớp [lớp java.lang.String], không thể được chuyển đổi thành [giao diện java.util.List]

Thăm dò ý kiến:

@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

Bạn có thể hiển thị mã của lớp 'Poll' + ánh xạ tương đối được sử dụng cho' Survey' và 'SurveyQuestion' không? –

+0

Chỉ cần làm rõ, impl. là EclipseLink không Hibernate. –

+0

@ O.Badr, đã thêm lớp Thăm dò ý kiến. Ánh xạ cho khảo sát và khảo sát đã có sẵn. –

Trả lời

1

Theo kinh nghiệm của tôi, Khi tôi đã để ánh xạ một Bộ sưu tập của sự vật, cuối cùng tôi đã làm một cái gì đó như thế này:

@OneToMany 
private Set<SurveyQuestion> surveyanswers; 

Tất cả điều này nếu bạn sử dụng phần mở rộng của nhà cung cấp JPA của bạn hỗ trợ thu thập các loại cơ bản. (ví dụ: Hibernate có chú thích @CollectionOfElements).

+0

Hibernate [@CollectionOfElements] (https://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/annotations/CollectionOfElements.html) được thay thế bằng [@ElementCollection] (https: //docs.jboss .org/hibernate/jpa/2.1/api/javax/persistence/ElementCollection.html) –

+0

Cảm ơn câu trả lời của bạn nhưng điều này không liên quan gì đến câu hỏi của tôi, '@ ConstructorResult' được sử dụng với các thực thể NON. –

Các vấn đề liên quan