在 hibernate 映射中,我设置了属性lazy="false"
,这会获取父级的所有子记录。
整个应用程序都在使用它。
这会在我的应用程序的特定模块中产生性能问题,其中我只想获取父记录。
我无法将 lazy
属性更改为 true
,因为它已在许多其他地方使用。有没有办法来解决这个问题?
如果需要更多信息,请告诉我。
最佳答案
这些在 hibernate 中没有这样的功能,因为它尊重您的lazy="false"
。因此,为了满足您的要求,我建议用另一个虚拟具体类扩展您的查询类,并为该类定义映射,而无需在其中添加子关联。
假设您有一个包含 Child 映射的 Parent 类
class Parent{
private List<Child> kids;
}
您拥有的 Parent 的映射是
<class name="Parent" table="PARENT">
// other properties
// child mapping
<set name="kids" table="KIDS" lazy="false">
<key column="parent_id"/>
<one-to-many class="Child"/>
</set>
</class>
然后您可以创建另一个扩展父类的类
class MinimalParent extends Parent{
// leave implementation as blank
}
然后将其映射如下
<class name="MinimalParent" table="PARENT">
// other properties
// do not map child in this
</class>
只要您只需要父对象,就可以使用这个 MinimalParent
类。希望你明白了!
关于Hibernate映射设置lazy = 'false',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8815209/