8

is it possible to map inner classes to the targetclass, if possible, how is it done ? I'm new to this @SqlResultSetMapping functionality:

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

So the targetClass Survey.class has:

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

How would I map the List<SurveyQuestion> field ?

SurveyQuestion:

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

Also, and very similar. How would I map a List<String> ?

I get an exception when trying to do mapping to 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]

Poll:

@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 
}
Esteban Rincon
  • 2,040
  • 3
  • 27
  • 44

1 Answers1

1

In my experience, When I had to map a Collection of things, finally I did something like this:

@OneToMany
private Set<SurveyQuestion> surveyanswers;

All of this if you're using an extension of your JPA provider supporting collection of basic types. (e.g. Hibernate has the @CollectionOfElements annotation).

developer_hatch
  • 15,898
  • 3
  • 42
  • 75
  • Hibernate [@CollectionOfElements](https://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/annotations/CollectionOfElements.html) is replaced by [@ElementCollection](https://docs.jboss.org/hibernate/jpa/2.1/api/javax/persistence/ElementCollection.html) – O.Badr May 09 '17 at 07:58
  • 1
    Thank you for your answer but this doesn't have much to do with my question, `@ConstructorResult` is used with NON entities. – Esteban Rincon May 09 '17 at 13:07