[英]MappedSuperclass - Change SequenceGenerator in Subclass
I'm using JPA2 with Hibernate and try to introduce a common base class for my entities. 我在Hibernate中使用JPA2,并尝试为我的实体引入一个通用的基类。 So far it looks like that:
到目前为止看起来像这样:
@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;
}
}
However, for every table theres a sequence $entityname_seq
which I want to use as my sequence generator. 但是,对于每个表,都有一个序列
$entityname_seq
,我想将其用作序列发生器。 How can I set that from my subclass? 我该如何在子类中进行设置? I think I need to override @GeneratedValue and create a new SequenceGenerator with @SequenceGenerator.
我想我需要重写@GeneratedValue并使用@SequenceGenerator创建一个新的SequenceGenerator。
Yes, it is possible. 对的,这是可能的。 You can override the default generator name with the
@SequenceGenerator
annotation. 您可以使用
@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;
}
}
Sequence (SQL) 序列(SQL)
create sequence role_seq;
Derived class 派生类
@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;
}
}
edit 编辑
In JPA that cannot be done with annotations. 在JPA中,注释无法完成。 Annotation itself cannot be overridden.
注释本身不能被覆盖。 Entity inherits all the mapping information from MappedSuperClass .
实体从MappedSuperClass继承所有映射信息。 There is only two annotations that can be used to redefine mappings inherited from mapped superClass:
只有两个注释可用于重新定义从映射的超类继承的映射:
Neither of them helps with GeneratedValue. 它们都不对GeneratedValue有帮助。
With EclipseLink, you can use a Customizer
. 通过EclipseLink,您可以使用
Customizer
。 DescriptorCustomizer
interface defines a way to customize all the information about a jpa descriptor (aka a persistent entity). DescriptorCustomizer
接口定义了一种自定义有关jpa描述符(又名持久性实体)的所有信息的方法。
public class SequenceCustomizer implements DescriptorCustomizer {
@Override
public void customize(ClassDescriptor descriptor) throws Exception {
descriptor.setSequenceNumberName(descriptor.getTableName());
}
}
and in your mapped superclass: 并在您映射的超类中:
@MappedSuperclass
@Customizer(SequenceCustomizer.class)
public abstract class AbstractEntity implements Serializable {
...
}
I'm writing this as it gets too unreadable as the comment on the accepted answer: 我正在写这篇文章,因为它对接受的答案的评论太不可读了:
I have a BaseEntity
that every other Entity inherits from: 我有一个
BaseEntity
,其他所有实体都继承自该实体:
BaseEntity.java: BaseEntity.java:
@MappedSuperclass
public abstract class BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_ID")
private Long id;
I then have two Entities User
and Order
that both inherit from BaseEntity
whilst also having the @SequenceGenerator
annotation: 然后,我有两个实体
User
和Order
,它们都从BaseEntity
继承,同时还具有@SequenceGenerator
批注:
User.java: User.java:
@SequenceGenerator(name = "SEQ_ID", sequenceName = "SEQ_USER", allocationSize = 1)
public class User extends BaseEntity { ... }
Order.java: Order.java:
@SequenceGenerator(name = "SEQ_ID", sequenceName = "SEQ_ORDER", allocationSize = 1)
public class Order extends BaseEntity { ... }
It works on H2 at least with 2 Sequences SEQ_USER
and SEQ_ORDERS
: 它至少可以在H2上使用2个序列
SEQ_USER
和SEQ_ORDERS
:
select SEQ_USER.nextval from dual;
select SEQ_ORDERS.nextval from dual;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.