gpt4 book ai didi

java - 如何在 Hibernate JPA @NamedEntityGraph 中引用继承的属性?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:14:31 24 4
gpt4 key购买 nike

我的项目中有以下映射:

@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 实体图中引用父类(super class)(或子类)中的属性的正确方法是什么?

最佳答案

从 hibernate-entitymanager 4.3.4 开始不支持。这是我到目前为止发现的:

  1. @NamedAttributeNode 不能用于引用父类(super class)的属性。
  2. includeAllAttributes=true 将包含所有属性,包括父类(super class)的属性。
  3. 子图中的
  4. @NamedAttributeNode可以引用父类的属性。

关于java - 如何在 Hibernate JPA @NamedEntityGraph 中引用继承的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22590721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com