Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA 2.1 @SqlResultSetMapping binding inner class to targetclass

Tags:

java-8

jpa-2.1

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 
}
like image 220
Esteban Rincon Avatar asked Apr 21 '17 17:04

Esteban Rincon


1 Answers

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).

like image 176
A Monad is a Monoid Avatar answered Nov 07 '22 12:11

A Monad is a Monoid