简体   繁体   English

MappedSuperclass-更改子类中的SequenceGenerator

[英]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批注覆盖默认的生成器名称。

  • Base class 基类
    @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;
        }   
    }
  • This approach worked fine in Hibernate 4.1.x, but it didn't in EclipseLink 2.x. 这种方法在Hibernate 4.1.x中很好用,但在EclipseLink 2.x中却没有。

edit 编辑

  • As per the comment, it seems to be working with EclipseLink 2.6.1-RC1. 根据评论,它似乎与EclipseLink 2.6.1-RC1一起使用。

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: 只有两个注释可用于重新定义从映射的超类继承的映射:

  1. AttributeOverride to override column mappings and AttributeOverride覆盖列映射和
  2. AssociationOverride to override join columns / table. AssociationOverride覆盖联接列/表。

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: 然后,我有两个实体UserOrder ,它们都从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_USERSEQ_ORDERS

select SEQ_USER.nextval from dual;
select SEQ_ORDERS.nextval from dual;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MappedSuperclass - 更改子类中的 SequenceGenerator 而不在超类上使用 @GeneratedValue - MappedSuperclass - Change SequenceGenerator in Subclass without @GeneratedValue on the Superclass MappedSuperclass-子类中的SequenceGenerator(休眠5.2.13.Final) - MappedSuperclass - SequenceGenerator in Subclass (hibernate 5.2.13.Final) 使用@MappedSuperclass注释的类上的@SequenceGenerator - @SequenceGenerator on class annotated with @MappedSuperclass Java Hibernate,在子类实体中更改从@MappedSuperclass继承的ManyToMany关系表名 - Java Hibernate, change inherited ManyToMany relationship table name from @MappedSuperclass in the subclass entity Spring 超级实体的存储库 class 从@MappedSuperClass 更改为@Inheritance 时未获取所有子类实体类型 - Spring repository for super entity class not fetching all subclass entity types when changed from @MappedSuperClass to @Inheritance @SequenceGenerator不起作用 - @SequenceGenerator is not working MappedSuperclass扩展了另一个MappedSuperclass - MappedSuperclass extending another MappedSuperclass @MappedSuperclass 和 @OneToMany - @MappedSuperclass and @OneToMany 更改属性ArrayList <Class> 到ArrayList <SubClass> 在一个子类中 - Change attribute ArrayList<Class> to ArrayList<SubClass> in a subclass 注释@SequenceGenerator和编译错误 - Annotation @SequenceGenerator and compilation error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM