one to many mapping to a property of superclass

大兔子大兔子 提交于 2019-12-08 15:44:25

问题


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?


回答1:


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>();



回答2:


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.



来源:https://stackoverflow.com/questions/4447613/one-to-many-mapping-to-a-property-of-superclass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!