- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想是否有一种方法可以微调 JPA/Hibernate 以管理以下用例。我有以下对象模型:
class Parent {
...
@OneToMany(mappedBy = "definizione", fetch = FetchType.LAZY)
private List<Child> childs;
...
}
class Child {
...
@ManyToOne(fetch = FetchType.LAZY)
private GrandChild grandChild;
...
}
class GrandChild {
...
}
然后我执行下面的代码:
Parent parent = entityManager.find(id, Parent.class); // (1)
List<Child> childs = parent.getChilds(); // (2)
GrandChild grandChild = null;
for(Child child : childs) {
grandChild = child.getGrandChild(); // (3)
//do somthing with childs
}
我想实现的是:
让 hibernate 执行两个查询:
select parent0_.* -- all Parent columns
from PARENTS parent0_
where parent0_.ID=?
SELECT
childs0_.* -- all Child columns
grandchild1_.* -- all GrandChild columns
FROM childs childs0_
LEFT OUTER JOIN grand_childs grandchild1_ ON childs0_.grand_child_id = grandchild1_.id
WHERE childs0_.parent_id =?
上面带有 Child - GrandChild 延迟获取的代码片段的基本行为是:
Parent
实体的查询; Parent
实体的所有Child
实体执行一次查询; GrandChild
实体一个。 通过阅读Hibernate Fetching ,我找到了以下解决方案,但都没有达到我想要的效果:
class Child {
...
@ManyToOne(fetch = FetchType.EAGER)
private GrandChild grandChild;
...
}
优点:执行的查询次数符合要求;
缺点:此解决方案会影响其他用例,出于某些原因我不想在实体类级别更改获取策略。
这种情况对 jpql 和 Crieria 查询都有效。
final Parent parent = entityManager.createQuery(
"SELECT p FROM Parent p LEFT JOIN FETCH p.childs c JOIN FETCH c.grandChild WHERE p.id = :id",
Parent.class
)
.setParameter("id", id)
.getSingleResult();
List<Child> childs = parent.getChilds();
GrandChild grandChild = null;
for (Child child : childs) {
grandChild = child.getGrandChild();
//do somthing with childs
}
执行的查询是:
SELECT parent0_.*, -- all Parent fields
childs1_.*, -- all Child fields
grandchild2_.* -- all GrandChild fields
FROM parents parent0_
LEFT OUTER JOIN childs childs1_ ON parent0_.id = childs1_.parent_id
LEFT JOIN grand_childs grandchild2_ ON childs1_.grand_child_id = grandchild2_.id
WHERE parent0_.id =?
优点:只执行了一个查询。
缺点:从数据库中加载了很多重复的数据,我不想多次加载父实体。
@Entity
@NamedEntityGraph(
name = "parent.childs.grandchild",
attributeNodes = {
@NamedAttributeNode(value = "childs", subgraph = "childs.grandchild")
},
subgraphs = {
@NamedSubgraph(
name = "childs.grandchild",
attributeNodes = {
@NamedAttributeNode(value = "grandChild")
}
)
}
)
public class Parent extends BaseEntity{
...
}
以及要加载的代码:
final Parent parent = entityManager.find(
Parent.class,
id,
Collections.singletonMap(
"javax.persistence.fetchgraph",
entityManager.getEntityGraph( "parent.childs.grandchild" )
)
);
List<Child> childs = parent.getChilds();
GrandChild grandChild = null;
for (Child child : childs) {
grandChild = child.getGrandChild();
//do somthing with childs
}
执行的查询与通过查询动态抓取相同,所以优缺点相同。
最佳答案
您可以使用 JOIN FETCH 一次执行查询和提取。
String hql = "SELECT parent FROM Parent parent " +
"LEFT JOIN FETCH parent.child child " +
"JOIN FETCH child.grandChild " +
"WHERE parent.id = :parentId";
Parent parent = entityManager.createQuery(hql, Parent.class).getSingleResult()
.setInteger("parentId", parentId);
List<Child> childs = parent.getChilds();
for(Child child : childs) {
GrandChild grandChild = child.getGrandChild();
//more code...
}
查看此文档:https://docs.jboss.org/hibernate/orm/5.1/userguide/html_single/chapters/fetching/Fetching.html
关于hibernate - JPA/Hibernate 加载惰性子实体并获取孙实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48448307/
我先说我正在学习 Haskel,所以不要太苛刻。 Haskell 的惰性求值可能有用也可能危险,这取决于计算的瓶颈是时间复杂度还是堆栈的大小。 出于这个原因,我想更好地了解 Haskell 中求值的工
我正在开发一款玩具 RTS 游戏,我依赖 A* 寻找路径,问题是很多单位四处移动导致计算的路径变得无效,这导致 CPU 周期浪费,我必须为那些重新计算路径代理商。 所以我想为什么不懒惰地计算路径而不是
我正在尝试非贪婪地解析出 TD 标签。我从这样的事情开始: stuffMore stuffOther stuffthingsmore things 我使用以下作为我的正则表达式: Regex.Spli
我正在学习 http://learnyouahaskell.com/starting-out 上的(优秀的)Haskell 教程。并且正在尝试直角三角形示例: > let triangles = [(
我编写了一个小型 Haskell 程序来打印当前目录中所有文件的 MD5 校验和(递归搜索)。基本上是 md5deep 的 Haskell 版本.一切都很好,除非当前目录有大量文件,在这种情况下我会收
我通常听说生产代码应该避免使用惰性 I/O。我的问题是,为什么?除了闲逛之外,还可以使用 Lazy I/O 吗?是什么让替代方案(例如枚举器)更好? 最佳答案 惰性 IO 存在的问题是,释放您所获取的
我注意到 Scala 提供了lazy vals。但我不明白他们在做什么。 scala> val x = 15 x: Int = 15 scala> lazy val y = 13 y: Int =
我目前正在尝试将 XML 文件的内容读入 Map Int (Map Int String) 并且它工作得很好(使用 HaXml)。但是,我对程序的内存消耗不满意,问题似乎出在垃圾回收上。 这是我用来读
lazy val seq: Unit = { println("a") seq } 我们可以尾递归调用上面的表达式吗? 最佳答案 我想你可以从这个意义上说,是的 - 评估时,seq将递归评估自
在以下示例中: def maybeTwice2(b: Boolean, i: => Int) = { lazy val j = i if (b) j+j else 0 } 为什么当我这样调用它
我的一个项目使用了混合的Scala功能,这些功能似乎不能很好地融合在一起: 类型类和无形自动类型类实例派生 隐式转换(向具有类型类实例的类型添加有用的语法) 默认参数,因为即使它们通常是一件坏事,但在
我有一个应用程序,涉及一个数组集合,这些数组可能非常大(索引最大为 int 的最大值),但它们是惰性 - 它们内容是动态计算的,并且在请求之前实际上是不知道的。数组也是不可变的 - 每个数组的每个元素
最近我开始使用 spring 中的惰性初始化功能很多。所以我一直在徘徊——懒惰地初始化你的 bean 有什么实际的缺点吗?如果不是 - 为什么不是懒惰的默认行为? 最佳答案 主要的“缺点”是不能立即发
我有一个通过信息亭向访问者显示的网站。人们可以与之互动。但是,由于该网站不是本地托管的,而是使用互联网连接 - 页面加载速度很慢。 我想实现某种惰性缓存机制,以便在人们浏览页面时 - 页面和页面引用的
我是否正确理解声明关系急切加载的方法是使用lazy='joined'或lazy='subquery'? “lazy”与“eager”相反——在这种情况下使用“lazy”关键字来表示急切加载,这是一个历
我想抓取 对之间任何值的内容标签。 This is one block of text This is another one 我想出的正则表达式是 /(.*)/m 虽然,它看起来很贪心,并
考虑以下几点: z = [{"x" => 5}, 2, 3].lazy.map{ |i| i} #=> #5}, 2, 3]>:map> z.first #=> {"x"=>5} 当我尝试将 z 转换
因此我有一个条件语句: if($boolean && expensiveOperation()){ ...} PHP 是否具有惰性 bool 值评估,即它是否会检查 $boolean 并且如果它为 f
我在 Scala 上有问题。我用 @transient lazy val 序列化了一个类的实例 field 。然后我反序列化它,该字段被分配null .我期待反序列化后的惰性评估。我该怎么办? 以下是
我编写了以下函数,我认为该函数应该以原子方式执行 IO(只要其他人都使用相同的 MVar)。 atomicIO :: MVar () -> IO a -> IO a atomicIO mvar io
我是一名优秀的程序员,十分优秀!