Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.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
Jpa 在子类中重写@OneToMany targetEntity?_Jpa_Jpa 2.0 - Fatal编程技术网

Jpa 在子类中重写@OneToMany targetEntity?

Jpa 在子类中重写@OneToMany targetEntity?,jpa,jpa-2.0,Jpa,Jpa 2.0,考虑以下示例: public interface Bar { } @Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public DefaultBar implements Bar { @Id @GeneratedValue(strategy = GenerationType.TABLE) private long id; } @Entity public class Foo { @Id

考虑以下示例:

public interface Bar {
}

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public DefaultBar implements Bar {

  @Id
  @GeneratedValue(strategy = GenerationType.TABLE)
  private long id;

}

@Entity
public class Foo {

  @Id
  @GeneratedValue(strategy = GenerationType.TABLE)
  private long id;

  @OneToMany(cascade = CascadeType.ALL, targetEntity = DefaultBar.class)
  @JoinColumn(name = "FOO_ID")
  private Collection<Bar> bars;

  public Collection<Bar> getBars() {
    return bars;
  }
}
现在我想对FooFoo进行注释,以便它将“bars”字段的目标实体设置为NewBar类

@Entity
@AssociationOverride(name="bars", ???????)
public class FooFoo extends Foo {

}

我注意到注释@AssociationOverride,但乍一看它不允许覆盖目标实体。或者是吗?

只要在Bar中使用继承,就不需要做任何事情。如果FooFoo与NewBar有关系,它们将被读回


否则,您可以向NewBar添加一个新字段,或者将Bar关系下移到子类。

在JPA上下文中,似乎没有办法做到这一点。Hibernate有特定的元注释来覆盖超类的一元注释

同时,@AssociationOverride注释似乎只适用于标有@MappedSuperclass的类。但是,它们不能用作OneToMany关联中的目标实体

鸡和蛋的问题真的。。。还是这样


Java不允许重写字段。但对于方法来说确实如此。如果超类使用属性访问,则is工作。让子类重写getter,就这样了!:-)

但是怎么做呢?从FooFoo到NewBar的数据库中没有外键。JPA实现者的诡计?
@Entity
@AssociationOverride(name="bars", ???????)
public class FooFoo extends Foo {

}