如何在 jpa 配置中覆盖 id 列的 getter [英] How override getter of id column in jpa configuration

查看:31
本文介绍了如何在 jpa 配置中覆盖 id 列的 getter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 JPA 中的 @Id 有如下描述的问题?有一个通用类如下?

 @MappedSuperclass公共抽象类 BaseEntity实现可序列化{私有静态最终长serialVersionUID = 4295229462159851306L;私人T id;公共 T getId() {返回标识;}公共无效setId(T id){this.id = id;}}

还有一个类从它继承而来,如下所示?

@Entity@Table(name = "DOC_CHANGE_CODE" )公共类 ChangeCode 扩展了 BaseEntity{@ID@GeneratedValue(generator = "sequence_db", strategy = GenerationType.SEQUENCE)@SequenceGenerator(name = "sequence_db", sequenceName = "SEQ_DOC_CHANGE_CODE", allocationSize = 1)公共长getId(){返回 super.getId();}}

因为任何子类都有自己的序列,我必须指定任何子类@Id,因为我覆盖了它的 getter 并在它上面放了一些注释.不幸的是它不能正常工作.如何解决问题并实现目标?

解决方案

无法从基类覆盖 @Id.

唯一的方法是提供 您自己的自定义IentifierGenerator 并提供基于子类的不同逻辑(例如,使用基于子类名称的序列名称).>

这就是为什么 @MappedSuperclass 中添加 @Id 属性仅对指定的生成器或 IDENTITY 有意义.

I have a problem for @Id in JPA that is described as follow? There is a generic class as follow?

    @MappedSuperclass
    public abstract class BaseEntity<T> implements Serializable {

        private static final long serialVersionUID = 4295229462159851306L;

        private T id;
        public T getId() {
          return id;
        }
        public void setId(T id) {
           this.id = id;
        }

    }

There is another class that extends from it as follow?

@Entity
@Table(name = "DOC_CHANGE_CODE" )
 public class ChangeCode extends BaseEntity<Long> {

     @Id
     @GeneratedValue(generator = "sequence_db", strategy = GenerationType.SEQUENCE)
     @SequenceGenerator(name = "sequence_db", sequenceName = "SEQ_DOC_CHANGE_CODE", allocationSize = 1)     
     public Long getId () {
       return super.getId();
     }
}

Because any sub class has its own sequence, I must specific any subclass @Id, because of that I override the its getter and put some annotations in top of that. Unfortunately it does not work correctly. How do I fix problem and get my goal?

解决方案

It's not possible to override the @Id from a base class.

The only way to do it is to provide your own custom IentifierGenerator and provide a different logic based on the subclass (e.g. using a sequence name based on the subclass name).

That's why adding the @Id attribute in the @MappedSuperclass only makes sense for the assigned generator or for IDENTITY.

这篇关于如何在 jpa 配置中覆盖 id 列的 getter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆