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
}
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With