休眠映射设置lazy = 'false' [英] Hibernate mapping setting lazy = 'false'

查看:39
本文介绍了休眠映射设置lazy = 'false'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在休眠映射中,我设置了属性lazy="false",这会获取父级的所有子记录.

In hibernate mapping, I have set the property lazy="false", and this fetches all the child records of the parent.

这将在整个应用程序中使用.
这会在我的应用程序的特定模块中产生性能问题,其中我只想获取父记录.

This is being used throughout the application.
This creates a performance issue at a particular module of my application, wherein I would like to fetch only the parent record.

我无法将 lazy 属性更改为 true,因为它已在许多其他地方使用.有没有办法解决这个问题?

I cant change the lazy property to true since it's being used at many other places. Is there a way to fix this?

如果需要更多信息,请告诉我.

Do let me know if any more info is required.

推荐答案

hibernate 中没有这些功能,因为它尊重您的 lazy="false".所以,我可以建议解决您的要求是使用另一个虚拟具体类扩展您的查询类,并为该类定义映射,而其中没有该子关联.

These is no such feature in hibernate as it respects your lazy="false". So, what can I suggest to address your requirement is extends your querying class with another dummy concrete class and define mapping for that class without that child association in it.

假设你有类 Parent 和 Child 映射

let say you have class Parent with Child mapping in it

class Parent{

     private List<Child> kids;

}

您拥有的 Parent 的映射是

and mapping for Parent you have is

<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>

然后你可以创建另一个扩展父类的类

Then you can create another class which extends Parent class

class MinimalParent extends Parent{
   // leave implementation as blank
}

然后将其映射如下

<class name="MinimalParent" table="PARENT">
    // other properties
    // do not map child in this
</class>

并在您只需要父对象的任何地方使用此 MinimalParent 类.希望你明白了!

And use this MinimalParent class wherever you require just parent object. hope you got it!

这篇关于休眠映射设置lazy = 'false'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆