【问题标题】:How to declare a Interface Descriptor for independent entities under common interface using EclipseLink?如何使用EclipseLink在通用接口下为独立实体声明接口描述符?
【发布时间】:2011-04-04 15:06:41
【问题描述】:

我们有这样的情况,我们需要在一个属性下关联不同的实体,并在查询中使用这个属性。 该项目使用 JPAEclipseLink 作为实现。

示例代码说明问题:(实际问题要复杂得多)

interface I1 {
  public int getData();
}

@Entity
class E1 implements I1 {
  public int id;
  public String someData;

  public int getData() { return someData; }
}

@Entity
class E2 implements I1 {
  public int id;
  public String someOtherData;

  public int getData() { return someOtherData; }
}


@Entity
class Test {
  // we want to handle this as if it was a real entity using the I1 Interface
  // Since it is a oneToMany mapping we cannot use eclipselinks
  // VariableOneToOne mapping
  // something like this should be possible 
  // SELECT t.refToEntities From Test t where t.data == 'Some String'  
  public List<I1> refToEntities;
}

由于历史原因,实体不共享继承关系,但它们具有公共数据,这些数据在公共接口下被抽象出来。 我们现在面临的问题是,我们有另一个实体想要使用一对多关系中的公共接口与这些实体相关联。理想情况下,该关系应该可以使用 JPQL 进行查询,但使用 EclipseLink 特定方法也可以。

总而言之,我们希望为 OneToMany 关系建立一个 VariableOneToOne 映射。

到目前为止,我发现的文档表明可以为接口构建 RelationalDescriptor,但不清楚如何将此描述符放入 JPA中的会话中> 场景以及如何设置 InterfacePolicy 以便将接口用作底层实体的通用类型。

那么有人知道如何做到这一点,或者可以指出我们的工作代码吗?

【问题讨论】:

    标签: java jpa eclipselink


    【解决方案1】:

    您可能最好使用继承而不是接口。

    或者拥有每个实体类型的列表,而不仅仅是接口。

    EclipseLink 仅支持 VariableOneToOne 而不是 VariableOneToMany,请为此记录增强请求。

    您可以使用 SessionCustomizer 为接口定义 RelationalDescriptor,只需对其进行实例化,使用 descriptor.setJavaInterface(Il.class) 设置接口并添加任何常用查询键。

    您可以将此接口用作 OneToManyMapping(在代码中定义)的目标。但是,您将无法在 JPQL 中加入它。

    【讨论】:

      猜你喜欢
      • 2019-02-09
      • 2016-08-09
      • 1970-01-01
      • 2017-04-21
      • 2016-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-05
      相关资源
      最近更新 更多