一对多映射到超类的属性 [英] one to many mapping to a property of superclass

查看:118
本文介绍了一对多映射到超类的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个超类问题及其子类 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引用未知的目标实体属性

然而,如果我创建了一组超类实体,它可以正常工作,例如

  $ b 

code> @OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL,mappedBy =activity)
private Set< NQIQuestions> questions = new HashSet< NQIQuestions>();

有没有办法映射到超类的属性?

?在OneToMany定义中。

例如..

  @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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆