gpt4 book ai didi

java - 提交实体对象后检索子对象

转载 作者:行者123 更新时间:2023-12-01 16:00:34 26 4
gpt4 key购买 nike

假设我检索了一个如下所示的实体对象:

@Entity
public class Mother {
...

@OneToMany(mappedBy = "mother",
targetEntity = Child.class,
fetch = FetchType.LAZY)
public List<Child> getChildren() {
return children;
}
}

检索上述对象时,我提交事务(并关闭与该对象关联的 session )。稍后在代码中,需要检索子级。如果我想将获取类型保持为 LAZY,有没有办法使用 Mother 对象,并且仍然能够调用 getChildren() 来检索子对象?或者我必须硬着头皮通过母亲的 key 询问 children ?

最佳答案

If I wanted to keep the fetch type as LAZY, is there a way to use the Mother object and still be able to call getChildren() to retrieve the children?

如果 EntityManager 已关闭,则不会。

Or would I have to just bite the bullet and query the children via the key of the Mother?

当使用 FETCH JOIN 获取母亲时,您可以检索 child :

SELECT m
FROM Mother m LEFT JOIN FETCH m.children
WHERE m.id = :id

其他选项包括:

  • 使用在 View 中打开实体管理器模式(如果您使用的是网络应用)
  • 使用扩展的持久性上下文。

关于java - 提交实体对象后检索子对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4047966/

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