I have a superclass Questions
and its subclass MultipleChoiceQuestions
Superclass has a field activity
I want to create a Set<MultipleChoiceQuestions>
and use OneToMany
annotation using mappedBy = "activity"
e.g.
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "activity" )
private Set<NQIMultipleChoiceQuestions> mcqQues开发者_StackOverflow社区tions = new HashSet<NQIMultipleChoiceQuestions>();
I am getting this error:
org.hibernate.AnnotationException: mappedBy reference an unknown target entity property
However, it works fine if I create a set of superclass entities,
e.g.
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "activity")
private Set<NQIQuestions> questions = new HashSet<NQIQuestions>();
Is there a way to map to property of superclass?
Found the solution for this... :)
We can achieve this just by defining the targetEntity = ? in the OneToMany definition..
eg..
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "activity" , targetEntity=NQIQuestions.class)
private Set<NQIMultipleChoiceQuestions> mcqQuestions = new HashSet<NQIMultipleChoiceQuestions>();
Probably you use Hibernate and it does not support this feature (Hibernate ORM HHH-4233: cant bind a child using mappedby relating to a parent attribute(polymorphism). The feature is rejected for controversial reasons. There is a comment there by Nicholas Stuart which provides more links about the subject, including this one giving some workarounds: Chris Wong's Development Blog: Polymorphic one to many relationships in Hibernate.
Once we know it's only Hibernate problem, we can switch to something else. OpenJPA, EclipseLink do support it. Please add a comment if there are more frameworks to list here.
精彩评论