我正在将JPA2与Hibernate一起使用,并尝试为我的实体引入一个公共基类。到目前为止它看起来像是:
@MappedSuperclass
public abstract class BaseEntity {
@Id
private Long id;
@Override
public int hashCode() {
// ...
}
@Override
public boolean equals(Object obj) {
// ...
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
}
然而,对于每个表都有一个序列$entityname_seq
,我想用它作为我的序列生成器。如何从我的子类中设置它?我想我需要覆盖@GeneratedValue并使用@SequenceGenerator创建一个新的SequenceGenerator。
答案 0 :(得分:30)
是的,有可能。您可以使用@SequenceGenerator
注释覆盖默认生成器名称。
@MappedSuperclass
public abstract class PersistentEntity implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "default_gen")
protected Long id = 0L;
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
}
序列(SQL)
create sequence role_seq;
派生类
@Entity
@Table(name = "role")
@SequenceGenerator(name = "default_gen", sequenceName = "role_seq", allocationSize = 1)
public class Role extends PersistentEntity implements Serializable
{
private static final long serialVersionUID = 1L;
@NotNull
@Size(max = 32)
private String name;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
修改强>
答案 1 :(得分:12)
在JPA中无法使用注释完成。注释本身不能被覆盖。实体从MappedSuperClass继承所有映射信息。只有两个注释可用于重新定义从映射的superClass继承的映射:
它们都没有帮助GeneratedValue。
答案 2 :(得分:4)
使用EclipseLink,您可以使用Customizer
。 DescriptorCustomizer
接口定义了一种自定义jpa描述符(也称为持久实体)的所有信息的方法。
public class SequenceCustomizer implements DescriptorCustomizer {
@Override
public void customize(ClassDescriptor descriptor) throws Exception {
descriptor.setSequenceNumberName(descriptor.getTableName());
}
}
并在您映射的超类中:
@MappedSuperclass
@Customizer(SequenceCustomizer.class)
public abstract class AbstractEntity implements Serializable {
...
}
答案 3 :(得分:1)
我正在写这篇文章,因为它对接受的答案的评论太不可读了:
我有一个BaseEntity
,其他所有实体都继承自:
BaseEntity.java:
@MappedSuperclass
public abstract class BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_ID")
private Long id;
然后,我有两个实体User
和Order
都继承自BaseEntity
,同时还具有@SequenceGenerator
注释:
User.java:
@SequenceGenerator(name = "SEQ_ID", sequenceName = "SEQ_USER", allocationSize = 1)
public class User extends BaseEntity { ... }
Order.java:
@SequenceGenerator(name = "SEQ_ID", sequenceName = "SEQ_ORDER", allocationSize = 1)
public class Order extends BaseEntity { ... }
它在H2上至少具有两个序列SEQ_USER
和SEQ_ORDERS
:
select SEQ_USER.nextval from dual;
select SEQ_ORDERS.nextval from dual;