我想继承多个@Entity
s,并且每个都应该按数据库序列增加主键,使用:
id bigint NOT NULL DEFAULT nextval('id_seq'::regclass)
因此我尝试使用 GenerationType.IDENTITY
如下:
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class BaseEntity {
//auto generates the id on database side
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
@Entity
public class MyEntity extends BaseEntity {
}
结果:
org.hibernate.MappingException: Cannot use identity column key generation with <union-subclass> mapping for BaseEntity.class
最佳答案
您有多种选择。
您可以使用@MappedSuperclass
,而不是使用@Inheritance
,我相信您想要完成的事情会很好地工作。
如果您需要@Inheritance
,则需要使用基于PROPERTY
的注释而不是FIELD
并覆盖每个中的getter子类如下:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Override
public Long getId() { ... }
关于java - 如何使用每个类继承创建 IDENTITY 主键列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38245100/