Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

one to many mapping to a property of superclass

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> mcqQuestions = 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?

like image 741
shailesh Avatar asked Dec 15 '10 07:12

shailesh


1 Answers

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>();
like image 124
shailesh Avatar answered Oct 16 '22 14:10

shailesh