【问题标题】:How to refer to inherited attributes in Hibernate JPA @NamedEntityGraph?如何在 Hibernate JPA @NamedEntityGraph 中引用继承的属性?
【发布时间】:2014-03-23 12:11:15
【问题描述】:

我的项目中有以下映射:

@Embeddable
class LineItem {
    ...
}

@Entity
abstract class Invoice {
    ...
    @ElementCollection @OrderColumn @NotEmpty
    List<LineItem> lineItems = []
    ...
}

@Entity
class PurchaseInvoice extends Invoice {
    ...
    @OneToOne(cascade=CascadeType.ALL, orphanRemoval=true)
    Payment payment
    ...
}

@Entity
class Payment {
    ...
    @ElementCollection @OrderColumn
    List<PaymentTerm> paymentTerms = []
    ...
}

@Embeddable
class PaymentTerm {
    ...
}

默认情况下,所有集合关联都是惰性的。我的目标是创建一个实体图,可用于热切加载PurchaseInvoice.lineItemsPurchaseInvoice.payment.paymentTerms

如果我定义以下实体图:

@NamedEntityGraph(name='PurchaseInvoiceWithDetail', attributeNodes = [
     @NamedAttributeNode(value='payment', subgraph='payment'),
     @NamedAttributeNode(value='lineItems')
], subgraphs = [
     @NamedSubgraph(name='payment', type=Payment, attributeNodes = [
         @NamedAttributeNode(value='paymentTerms')
     ])
])
@Entity
class PurchaseInvoice extends Invoice

我收到以下无法构建实体管理器工厂错误:

java.lang.IllegalArgumentException: Unable to locate Attribute  with the given name [lineItems] on this ManagedType [PurchaseInvoice]

在 JPA 2.1 实体图中引用超类(或子类)中的属性的正确方法是什么?

【问题讨论】:

    标签: java hibernate jpa jpa-2.1


    【解决方案1】:

    从 hibernate-entitymanager 4.3.4 开始不支持此功能。这是我目前发现的:

    1. @NamedAttributeNode 不能用于引用超类的属性。
    2. includeAllAttributes=true 将包括所有属性,包括超类的属性。
    3. 子图中的@NamedAttributeNode 可以引用超类的属性。

    【讨论】:

      【解决方案2】:

      也许这是图形定义中的一个简单语法问题?在我看来,该定义不包含方括号。 我测试了你的代码,没问题。

      见:Developerblog

      【讨论】:

      • 感谢您的反馈。所以代码没问题。您在测试中使用的 JPA 提供程序是什么?我无法在 hibernate-entitymanager 4.3.4.Final 中完成这项工作。方括号是 Groovy 语法,应该兼容 Java。
      • 我使用了休眠提供程序。版本是 4.3.0。奇怪的。我看了很多书。应该没问题
      • 您是否为位于超类中的属性定义了@NamedAttributeNode?似乎 Hibernate 找不到未在 PurchaseInvoice 中声明但在其父类 Invoice 中声明的 lineItems
      【解决方案3】:

      根据link,这是一个 Hibernate 错误,并声称在 4.3.6 版本中修复

      【讨论】:

      • 当我使用版本 4.3.10.Final 时问题已解决
      猜你喜欢
      • 2021-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多