【发布时间】:2016-04-08 21:09:40
【问题描述】:
我想要一个对所有文档类型都通用的超类:
@Entity
public abstract class Doc implements Serializable
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected long docId;
public long getDocId()
{
return docId;
}
public void setDocId(long docId)
{
this.docId = docId;
}
}
我想为每种文档类型创建子类:
@Entity
@Table(name = "DocTypeA")
public class DocTypeA extends Doc implements Serializable
{
// other child fields
}
但它给出了一个错误并说 DocTypeA 需要一个主键。如何隔离主键并将其放在超类中?因为所有的子类都会有相同的 id 字段。
我正在使用 EclipseLink。
我的另一个问题是:为什么我需要将@Entity 放在抽象类中?作为一个抽象类,它不能被实例化,那么将它标记为实体有什么意义呢?真的有必要吗?我不会坚持超类。我只需要它来隔离所有子类中通用的代码。
堆栈跟踪很长,相关部分粘贴在下面:
Exception Description: Entity class [class repository.DocTypeA] has no primary key specified. It should define either an @Id, @EmbeddedId or an @IdClass. If you have defined PK using any of these annotations then make sure that you do not have mixed access-type (both fields and properties annotated) in your entity class hierarchy.
【问题讨论】:
-
准确和完整的错误堆栈跟踪是什么?您希望您的数据库架构是什么样的?
-
当您遇到该异常时,您确定 Doc 类确实用
@Entity进行了注释吗?
标签: java jpa eclipselink