使用“mappedBy”的多个多对多关系与超级会员
我正在使用 Hibernate 开发一个应用程序,并尝试对以下场景进行建模:
我有一个 Activity 抽象类,定义如下:
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="activityType")
@Table(name = "BF_ACTIVITY")
public abstract class Activity extends PersistableObject {
@ManyToOne
@JoinColumn(name = "ASSIGNED_TO")
protected Contactable assignedTo;
@ManyToOne
@JoinColumn(name = "RAISED_BY")
protected Contactable raisedBy;
注意我正在使用单表继承(因此所有实现对象将使用同一个表)并设置了 DiscriminatorColumn 。
现在我有两个扩展 Activity 的对象:ToDo 和 Status:
@Entity
@Table(name = "BF_TODO")
@DiscriminatorValue("Todo")
public class ToDo extends Activity {
...
@Entity
@Table(name = "BF_STATUS")
@DiscriminatorValue("Status")
public class Status extends Activity {
...
再次注意,对于这两个实现,我都设置了 DiscriminatorValue。
最后,我想要一个 Person 对象,并且该人可以有一个 Status 列表和一个 ToDo 列表 - 我还想捕获双向关系,因此使用mappedBy配置对其进行建模,但在这两种情况下都使用存在于超类“Activity”中的“raisedBy”字段:
public abstract class Person {
@ManyToMany(mappedBy="raisedBy", targetEntity=Activity.class)
private List<ToDo> todoItems = new ArrayList<ToDo>();
由于我将mappedBy与超类中的成员变量“raisedBy”一起使用,所以我还指定了targetEntity(否则它无法在ToDo中找到该字段目的)。
问题是当我尝试调用 getTodoItems() 时 - 它实际上只是返回由“raisedBy”链接到当前人的所有“Activity”对象。例如,它会抛出一个强制转换异常,因为它需要 ToDo 列表,但 Hibernate 也在列表中返回 Status 对象。
我希望mappedBy配置和DiscriminatorValue足以完成这项工作——有人遇到过这个问题或解决了它吗?
谢谢
编辑
我刚刚找到这篇文章:
有人能给我指出一个好的@Where 概述和示例的方向吗?我可以按如下方式更新我的人以将 @Where 与鉴别器列一起使用吗?
public abstract class Person {
@ManyToMany(mappedBy="raisedBy", targetEntity=Activity.class)
@Where(clause="activityType=Todo")
private List<ToDo> todoItems = new ArrayList<ToDo>();
I am developing an application using Hibernate and am trying to model the following scenario:
I have an Activity Abstract class, defined as follows:
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="activityType")
@Table(name = "BF_ACTIVITY")
public abstract class Activity extends PersistableObject {
@ManyToOne
@JoinColumn(name = "ASSIGNED_TO")
protected Contactable assignedTo;
@ManyToOne
@JoinColumn(name = "RAISED_BY")
protected Contactable raisedBy;
Note I am using Single Table inheritance (so all implementing objects will use the same table) and have set a DiscriminatorColumn.
Now I have two objects that extend Activity: ToDo and Status:
@Entity
@Table(name = "BF_TODO")
@DiscriminatorValue("Todo")
public class ToDo extends Activity {
...
@Entity
@Table(name = "BF_STATUS")
@DiscriminatorValue("Status")
public class Status extends Activity {
...
Again note that for both implementations i have set a DiscriminatorValue.
Finally, I want to have a Person object, and the person can have a list of Status and a list of ToDo - I also want to capture the bi-directional relationship, so am modelling it using the mappedBy configuration, but in both cases using the "raisedBy" field that exists in the super class "Activity":
public abstract class Person {
@ManyToMany(mappedBy="raisedBy", targetEntity=Activity.class)
private List<ToDo> todoItems = new ArrayList<ToDo>();
As i am using mappedBy with the member variable "raisedBy" from the super class I have also specified the targetEntity (otherwise it is not able to find the field in the ToDo object).
The problem is when I try to call getTodoItems() - it is actually just returning all "Activity" objects linked by "raisedBy" to the current person. e.g. it throws a cast exception because it is expecting the list of ToDos but Hibernate is returning Status objects in the list as well.
I was hoping the mappedBy config along with DiscriminatorValue would be enough to make this work - has anyone come across this or resolved it?
Thanks
EDIT
I have just found this post:
Can someone point me in the direction of a good @Where overview and example? could I just update my person as follows to use @Where with the discriminator column?
public abstract class Person {
@ManyToMany(mappedBy="raisedBy", targetEntity=Activity.class)
@Where(clause="activityType=Todo")
private List<ToDo> todoItems = new ArrayList<ToDo>();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加注释:
@Where(clause = "activityType= 'Todo'")
但这是一个hibernate注释,而不是JPA
Add the annotation :
@Where(clause = "activityType= 'Todo'")
But it's a hibernate annotation, not JPA