覆盖休眠映射文件中相关实体的延迟加载设置
我有一个映射文件,它为一些相关实体设置lazy=false
。这对于很多用例来说都是有意义的,但也有一些例外。问题是我不想在这些情况的查询时获取相关关联,这在时间上非常昂贵。
实体的映射示例。
<class name="Category" table="category">
<id name="id" type="string">
<column length="50" name="id"/>
<generator class="uuid"/>
</id>
<property name="name" type="string">
<column length="100" name="name" not-null="true"/>
</property>
<set inverse="true" lazy="false" name="categorySourcesList">
<key>
<column length="50" name="categoryid" not-null="true"/>
</key>
<one-to-many class="CategorySource"/>
</set>
</class>
我的问题是,有没有一种方法可以覆盖映射文件中设置的 lazy
值,无论是在我自定义编写的 sql-query
中还是启用延迟加载DAO 中的参数?或者通过一些注释?
I have a mapping file which sets lazy=false
for some related entities. This makes sense for a lot of use cases but there are some exceptions. The problem is that I dont want to fetch the related associations at query time for these cases, which are very expensive time-wise.
Example of the mapping for the entity.
<class name="Category" table="category">
<id name="id" type="string">
<column length="50" name="id"/>
<generator class="uuid"/>
</id>
<property name="name" type="string">
<column length="100" name="name" not-null="true"/>
</property>
<set inverse="true" lazy="false" name="categorySourcesList">
<key>
<column length="50" name="categoryid" not-null="true"/>
</key>
<one-to-many class="CategorySource"/>
</set>
</class>
My question is, is there a way to override the lazy
value which is set in the mapping file, either in the sql-query
I custom write or enabling lazy load as one of the parameters in the DAO? or through some annotations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您可以覆盖带注释或 xml 映射的关联获取策略。
Hibernate 文档
这将返回您有一个
MyClass
实例,并且急切地获取了它的子级。Yes, you can override the annotated or xml mapped association fetching strategy.
Hibernate Documentation
This will return you an instance of
MyClass
with its children eagerly fetched.AFAIK 你不能用 LAZY 覆盖 EAGER 加载,但只能反过来。
因此,您需要将关联定义为 LAZY 并在使用联接的查询中覆盖该关联。可能还有其他方法可以做到这一点,但恐怕这就是我现在所知道的。
AFAIK you can't override EAGER loading with LAZY but only the other way round.
Thus, you'd need to define the association the be LAZY and override that in the queries using joins. There might be other ways to do that but I'm afraid that's all I know right now.