gpt4 book ai didi

java - OpenJPA 1.2.1 : fine-grained fetch plan

转载 作者:行者123 更新时间:2023-12-02 08:04:24 26 4
gpt4 key购买 nike

假设我有以下内容:

@Entity
class Product {
@OneToOne(Lazy)
ProductType type;
}

@Entity
class ProductType {
@ManyToOne(Lazy)
ProductFamily pf;
}

@Entity
class ProductFamily {
String name;
// ...
}

默认情况下,OpenJPA 的获取深度为-1,如果我查询产品:

select p from Product p

它最终会查询如下内容:

select p from product p
left join product_type
left join product_family

我已经阅读了有关获取深度的内容,并且我可以将获取深度限制为1,这意味着查询将仅获取直接关系:

select p from product p
left join product_type

但是我如何自定义查询以便能够为嵌套关系指定获取连接?我尝试了 left fetch join 并将字段添加到提取计划,但它不起作用。

<小时/>

明确一点:我希望所有 *-to-One 关系 默认情况下是惰性的,然后通过 fetch 连接微调关系,但 OpenJPA 似乎忽略了惰性配置。

最佳答案

不确定我是否理解。如果您希望能够指定必须获取的内容,则需要将关联标记为惰性关联,并使用查询加载对象。默认情况下,*ToOne 关联是急切的,而 *ToMany 关联是惰性的。一旦标记为惰性,就可以进行以下查询:

select p from Product p // load only the products

select p from Product p
left join fetch p.type // load products with their type

select p from Product p
left join fetch p.type t
left join fetch t.pf // load products with their type and the family of their type

如果关联是急切的,无论用于加载实体的代码如何,都将获取其关联。

关于java - OpenJPA 1.2.1 : fine-grained fetch plan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8390020/

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