Hibernate:在渴望的领域上强制进行延迟加载 [英] Hibernate : Force lazy-loadding on eager field

查看:40
本文介绍了Hibernate:在渴望的领域上强制进行延迟加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们应用程序中的一个模型对象具有许多配置为可以像这样急切地获取的字段:

One of our model object in our application has many fields configured to be eagerly fetched like so:

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "field")
public Field getField() {
    return this.field;
}

但是,有时我不需要这些信息,这会使我的查询变慢.我无法更改行为,而是使用FetchType.LAZY,因为我不知道会对整个应用程序产生什么影响(旧版...).有没有一种方法可以简单地告诉hibernate什么都不获取,除了(如果在查询中指定了)?

However I sometime do not need these information, which slow down my queries for nothing. I cannot change the behaviour and use FetchType.LAZY instead as I've no idea what will be the impact on the whole application (legacy...). Is there a way to simply tell hibernate to fetch nothing, except if it is specified in the query?

推荐答案

如果可以切换到该查询的条件,则可以对字段属性使用FetchMode.SELECT

If its possible to switch to Criteria for this query, you could use FetchMode.SELECT for the field property

crit.setFetchMode("field", FetchMode.SELECT); 

这篇关于Hibernate:在渴望的领域上强制进行延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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