one to many mapping to a property of superclass
Asked Answered
O

2

10

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?

Oar answered 15/12, 2010 at 7:21 Comment(5)
@sahil, the two lines you showed - where you claimed that the first one did not work, but the second one did - differ only in the variable name of the Set.Disregardful
@binil : ya.. actually in second case, questions is the base class, whereas mcqQuestion is subclass.Oar
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "activity") private Set<NQIQuestions> questions = new HashSet<NQIQuestions>();Oar
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "activity" ) private Set<NQIMultipleChoiceQuestions> mcqQuestions = new HashSet<NQIMultipleChoiceQuestions>();Oar
NQIQuestions is superclass of NQIMultipleChoiceQuestionsOar
O
19

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>();
Oar answered 15/12, 2010 at 11:24 Comment(3)
But doesn't the set now also include objects of other subtypes?Leavenworth
Unfortunately, this is true. A possible solution with Hibernate would be to use @Where(clause="...") with a suitable where-clause to only load the correct subtype(s).Tipsy
Thanks! targetEntity just solved my problem. Much appreciated!Feather
A
0

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.

Amorist answered 7/10, 2017 at 11:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.