【发布时间】:2012-12-04 17:20:33
【问题描述】:
在我当前的项目中,我有一个如下所示的继承结构:
@Entity
@Table(name="groups")
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorValue("G")
@DiscriminatorColumn(name="group_type")
public class Group{ //some annotations removed
private Long id;
private String name;
private Set<Subject> subjects;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="parent_group_id")
private Group parent; ##### tree parent ####
@OneToMany(cascade=CascadeType.ALL, mappedBy="parent")
private Set<Group> subGroups; ##### tree children #####
...
}
我的Group 对象可以通过包含其他Group 对象的列表而具有树状结构。
由于有些组有点特殊,所以有第二个类扩展了这个类:
@Entity
@DiscriminatorValue("C")
@Table(name="fix_groups")
public class FixGroup extends Group{
private Layout lay;
private Set<Person> instr;
...
}
我尝试使用联合多表方法(如此处所述:http://en.wikibooks.org/wiki/Java_Persistence/Inheritance#Joined.2C_Multiple_Table_Inheritance),但它似乎不适用于像Group 这样的非抽象超类!
我得到以下异常:
Caused by: java.lang.ClassCastException: org.hibernate.mapping.JoinedSubclass
cannot be cast to org.hibernate.mapping.RootClass
除了将Group 声明为抽象并创建一个仅扩展它的新类Group2 之外,还有其他解决方案吗?
如果我这样做了,这个自我引用Set<Group> subGroups 还会导致问题吗?
【问题讨论】:
标签: java hibernate jpa persistence javabeans