一对多映射到超类的属性 [英] one to many mapping to a property of superclass
问题描述
我有一个超类问题
及其子类 MultipleChoiceQuestions
超类有一个字段 activity
我想要创建 Set< MultipleChoiceQuestions> ;
并使用 OneToMany
注释使用 mappedBy =activity
例如
@OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL,mappedBy =activity)
private Set< NQIMultipleChoiceQuestions> mcqQuestions = new HashSet< NQIMultipleChoiceQuestions>();
我得到这个错误:
org.hibernate.AnnotationException:mappedBy引用未知的目标实体属性
然而,如果我创建了一组超类实体,它可以正常工作,例如
$ bcode> @OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL,mappedBy =activity)
private Set< NQIQuestions> questions = new HashSet< NQIQuestions>();
有没有办法映射到超类的属性?
例如..
@OneToMany (fetch = FetchType.EAGER,cascade = CascadeType.ALL,mappedBy =activity,targetEntity = NQIQuestions.class)
private Set< NQIMultipleChoiceQuestions> mcqQuestions = new HashSet< NQIMultipleChoiceQuestions>();
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?
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>();
这篇关于一对多映射到超类的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!