是否有可能增加JPA注释进行超实例变量? [英] Is it possible to add JPA annotation to superclass instance variables?

查看:135
本文介绍了是否有可能增加JPA注释进行超实例变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建是两个不同的表相同的实体。为了做表映射等,为两个实体不同,但只拥有code的其余部分在一个地方 - 一个抽象的超类。最好的事情是可以注解通用的东西,如列名(因为将是相同的)在超类,但不起作用,因为JPA注释不受子类继承。下面是一个例子:

I am creating entities that are the same for two different tables. In order do table mappings etc. different for the two entities but only have the rest of the code in one place - an abstract superclass. The best thing would be to be able to annotate generic stuff such as column names (since the will be identical) in the super class but that does not work because JPA annotations are not inherited by child classes. Here is an example:

public abstract class MyAbstractEntity {

  @Column(name="PROPERTY") //This will not be inherited and is therefore useless here
  protected String property;


  public String getProperty() {
    return this.property;
  }

  //setters, hashCode, equals etc. methods
}

,我想继承和唯一指定儿童专用的东西,比如注释:

Which I would like to inherit and only specify the child-specific stuff, like annotations:

@Entity
@Table(name="MY_ENTITY_TABLE")
public class MyEntity extends MyAbstractEntity {

  //This will not work since this field does not override the super class field, thus the setters and getters break.
  @Column(name="PROPERTY") 
  protected String property;

}

任何意见或我将要创建字段,getter和setter的子类?

Any ideas or will I have to create fields, getters and setters in the child classes?

谢谢,
克里斯

Thanks, Kris

推荐答案

您可能想用注释 @MappedSuperclass 类MyAbstractEntity使Hibernate会导入MyAbstractEntity的配置孩子,你不会有覆盖领域,只需要使用父母的。这注释是冬眠,它具有检查父类太信号。否则,它假定它可以忽略它。

You might want to annotate MyAbstractEntity with @MappedSuperclass class so that hibernate will import the configuration of MyAbstractEntity in the child and you won't have to override the field, just use the parent's. That annotation is the signal to hibernate that it has to examine the parent class too. Otherwise it assumes it can ignore it.

这篇关于是否有可能增加JPA注释进行超实例变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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