Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 可以向超类实例变量添加JPA注释吗?_Java_Inheritance_Jpa_Annotations - Fatal编程技术网

Java 可以向超类实例变量添加JPA注释吗?

Java 可以向超类实例变量添加JPA注释吗?,java,inheritance,jpa,annotations,Java,Inheritance,Jpa,Annotations,我正在为两个不同的表创建相同的实体。为了使两个实体的表映射等不同,但只有一个地方的代码的其余部分-一个抽象超类。最好的办法是能够在超类中注释通用的东西,例如列名(因为列名是相同的),但这不起作用,因为子类不会继承JPA注释。以下是一个例子: public abstract class MyAbstractEntity { @Column(name="PROPERTY") //This will not be inherited and is therefore useless here

我正在为两个不同的表创建相同的实体。为了使两个实体的表映射等不同,但只有一个地方的代码的其余部分-一个抽象超类。最好的办法是能够在超类中注释通用的东西,例如列名(因为列名是相同的),但这不起作用,因为子类不会继承JPA注释。以下是一个例子:

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
}
我想继承它,只指定特定于子对象的内容,如注释:

@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吗

谢谢,
Kris

@MappedSuperclass
注释基类应该完全符合您的要求。

将超类标记为

@MappedSuperclass

然后从子类中删除属性。

您可能希望使用
@MappedSuperclass
类注释MyAbstractEntity,这样hibernate将在子类中导入MyAbstractEntity的配置,而不必重写该字段,只需使用父类的。该注释是休眠的信号,它也必须检查父类。否则,它会假定它可以忽略它。

下面是一个示例,其中有一些解释可能会有所帮助

@MappedSuperclass:

  • 这是一个方便班
  • 用于存储子类可用的共享状态和行为
  • 是不持久的
  • 只有子类是持久的
@继承指定三种映射策略之一:

  • 单桌
  • 加入
  • 每班一张表
  • @DiscriminatorColumn用于定义用于区分子对象的列

    @DiscriminatorValue用于指定用于区分子对象的值

    以下代码产生以下结果:

    您可以看到id字段在两个表中,但仅在AbstractEntityId@MappedSuperclass中指定

    此外,@DisciminatorColumn在PARTY表中显示为PARTY_类型

    @DiscriminatorValue在PARTY表的PARTY_TYPE列中显示为Person作为记录

    非常重要的是,AbstractEntityId类根本不会持久化

    我没有指定@Column注释,而是依赖于默认值

    如果您添加了一个扩展参与方的组织实体,并且下一个继续存在,则参与方表将具有:

    • id=2
    • PARTY_TYPE=“组织”
    组织表的第一个条目应包括:

    • id=2
    • 与组织特别相关的其他属性值
    @映射超类 @SequenceGenerator(name=“SequenceGenerator”, initialValue=1,allocationSize=1) 公共类AbstractEntityId实现可序列化{ 私有静态最终长serialVersionUID=1L; @身份证 @GeneratedValue(generator=“sequenceGenerator”) 保护长id; 公共抽象实体ID(){} 公共长getId(){ 返回id; } } @实体 @继承(策略=InheritanceType.JOINED) @鉴别器列(name=“PARTY\u TYPE”, discriminatorType=discriminatorType.STRING) 公共类参与方扩展了AbstractEntityId{ 公共党(){} } @实体 @鉴别器值(“人”) 公营阶层人士扩展党{ 私人字符串givenName; 私有字符串familyName; 私有字符串preferredName; @时态(TemporalType.DATE) 私人出生日期; 私人字符串性别; 公众人物(){} //getter和setter等。 }
    希望这有帮助:)

    (吹毛求疵)他没有提到冬眠,是吗?他可以使用不同的JPA提供商。当然,你的推理并不适用,我实际上在使用hibernate,只是忘了这么说:) @MappedSuperclass @SequenceGenerator(name = "sequenceGenerator", initialValue = 1, allocationSize = 1) public class AbstractEntityId implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(generator = "sequenceGenerator") protected Long id; public AbstractEntityId() {} public Long getId() { return id; } } @Entity @Inheritance(strategy = InheritanceType.JOINED) @DiscriminatorColumn(name = "PARTY_TYPE", discriminatorType = DiscriminatorType.STRING) public class Party extends AbstractEntityId { public Party() {} } @Entity @DiscriminatorValue("Person") public class Person extends Party { private String givenName; private String familyName; private String preferredName; @Temporal(TemporalType.DATE) private Date dateOfBirth; private String gender; public Person() {} // getter & setters etc. }