对于以下域:
\nclass Parent {\n @OneToMany(fetch = FetchType.LAZY, mappedBy = "parent")\n Set <Child> children;\n)\n\nclass Child {\n @ManyToOne\n Parent parent;\n ... // setters, getters, other properties \n}\n
Run Code Online (Sandbox Code Playgroud)\n我正在寻找一种添加子实体的方法,而无需在父对象中延迟初始化子实体。
\n前任:
\nchild.set(parent) // is fine, since Parent is pretty lightweight.\n\nparent.getChildren().add(child) // No good, since in this case it will attempt to \n//load all the children of a Parent object.\n
Run Code Online (Sandbox Code Playgroud)\n乍一看,这感觉应该是很容易实现的事情,因为为了添加子项,您所需要的只是父项 ID 和对子表的 SQL 插入语句。\n但是,当涉及到休眠时,没有什么是简单的(事实上,对于所有使用缓存的东西来说,\xe2\x80\x99 都是如此):-)
\n我们正在尝试扩展一个遗留应用程序,该应用程序最初是为支持数十个孩子而编写的,但随着时间的推移,现在已经发展到可以与数千个孩子一起运行,并且正如您可以想象的那样,\xe2\x80\x99t 无法很好地扩展,因为每次我们需要要添加子项,我们需要首先将整个组加载到内存中,这会导致 O(N) 时间复杂度和 O(N) 空间复杂度(其中 N 是子项总数)。
\n有人遇到过类似的问题吗?有没有办法在 hibernate 中解决这个问题,或者 hibernate …