如何用另一个实体扩展一个实体,但它们都引用同一个表?可能吗 ?结构是这样的:
@Entity
@Table(name = "users")
@NamedQuery(name="User.findAll", query="SELECT u FROM User u")
public class User implements Serializable{
private int id;
private String name;
}
@Entity
@Table(name = "users")
@NamedQuery(name="SubUser.findAll", query="SELECT su FROM SubUser su")
public class SubUser extends User {
@Override
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public int getId() {
return super.getId();
}
//- Other fields and getter setter
}
我试过这种方式扩展 JPA 实体以添加属性和逻辑
但我得到了这个例外
org.hibernate.mapping.SingleTableSubclass cannot be cast to org.hibernate.mapping.RootClass
更新 1
我已经@Id
为 SubUser 放置了,因为@Entity
显示了这个异常
The entity has no primary key attribute defined