我有一个父类Person,两个子类RoleA和RoleB,RoleA下可以有多个RoleB,反之亦然,我的目标是让一个人同时拥有两个角色。
但是,休眠状态会引发WrongClassException。据我了解,一个Person对象被强制转换为RoleA,并且不能同时成为RoleB。请告诉我一些修复/解决方法,谢谢!
PS:我在role_a和role_b表中都有人ID 2
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "person")
public class Person implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id")
protected Long id;
}
@Entity
@Table(name = "role_a")
@PrimaryKeyJoinColumn(name = "role_a_id")
public class RoleA extends Person implements Serializable {
@ManyToMany
@JoinTable(
name = "role_a_b_join",joinColumns = @JoinColumn(name = "role_a_id",columnDefinition =
"bigint(20)"),inverseJoinColumns = @JoinColumn(name = "role_b_id",columnDefinition = "bigint(20)"))
private Set<RoleB> roleB;
}
@Entity
@Table(name = "role_b")
@PrimaryKeyJoinColumn(name = "role_b_id")
public class RoleB extends Person implements Serializable {
@ManyToMany(mappedBy = "roleB")
private Set<RoleA> roleA;
}
错误堆栈跟踪:
org.hibernate.WrongClassException: Object [id=2] was not of the specified
subclass [RoleB] : loaded object was of wrong class class RoleA