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

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

问题描述

在hibernate映射中,我已经设置了属性 lazy =false,并且获取父级的所有子记录。

整个应用程序都使用这个函数。

这会在我的应用程序的特定模块中产生性能问题,我只想提取它父记录。



我不能将懒惰属性更改为 true 它正在许多其他地方使用。有没有办法来解决这个问题?



如果需要更多信息,请告诉我们。 >这些在hibernate中没有这样的功能,因为它尊重你的 lazy =false。所以,我可以建议什么来解决你的要求是扩展你的查询类与另一个虚拟的具体类,并定义该类的映射,没有它的儿童关联。



let say你有父类和子类映射在其中

  class父类{

private List< Child>孩子;


$ / code $ / pre
$ b $ p

b
$ b

 < class name =Parenttable =PARENT> 
//其他属性
//子映射
< key column =parent_id/>
<一对多班=小孩/>
< / set>
< / class>

然后,您可以创建另一个扩展Parent类的类

  class MinimalParent extends Parent {
//将实现保留为空白
}

然后将其映射为

 < class name =MinimalParent表= 父 > 
//其他属性
//不映射此
中的子元素< / class>

在需要的地方使用这个 MinimalParent 类只是父对象。希望你懂了!


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.

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.

解决方案

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.

let say you have class Parent with Child mapping in it

class Parent{

     private List<Child> kids;

}

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
}

Then map it as bellow

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

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

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

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