如何在运行时重写hibernate获取策略 [英] how to override hibernate fetching strategy at runtime

查看:66
本文介绍了如何在运行时重写hibernate获取策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在hibernate in action一书中看到了以下声明。任何人都可以告诉我如何在运行时覆盖策略。你可以做相反的方式,那就是我将策略集lazy设置为false,并且我想将它设置为true?

i saw following statement in book "hibernate in action". anyone can tell me how to override strategy at runtime. and can you do opposite way that is i have fetching strategy set lazy as false and i want to set it true?

懒惰的抓取可以让您决定对象的多少graph被加载到第一个
数据库命中,并且只有在第一个
被访问时才应该加载哪些关联。延迟提取是对象持久化的基本概念,而
是获得可接受性能的第一步。
我们建议,首先,将所有关联配置为在映射文件中获取惰性(或可能是
批量拖动)。 这个策略可能会在运行时被强制提前抓取发生的查询覆盖

"Lazy fetching lets you decide how much of the object graph is loaded in the first database hit and which associations should be loaded only when they’re first accessed. Lazy fetching is a foundational concept in object persistence and the first step to attaining acceptable performance. We recommend that, to start with, all associations be configured for lazy (or perhaps batched lazy) fetching in the mapping file. This strategy may then be overridden at runtime by queries that force eager fetching to occur."

推荐答案

如果您使用HQL进行查询,您可以使用fetch关键字指定您的提前获取,如下所示:

If you're using HQL for your queries, you can specify your eager fetching using the "fetch" keyword, like so:

from Cat as cat
    inner join fetch cat.mate
    left join fetch cat.kittens child
    left join fetch child.kittens

如果您使用Criteria Query API,则可以使用setFetchMode指定获取模式

If you're using the Criteria Query API, you can specify the fetch mode using setFetchMode

List cats = sess.createCriteria(Cat.class)
    .add( Restrictions.like("name", "Fritz%") )
    .setFetchMode("mate", FetchMode.EAGER)
    .setFetchMode("kittens", FetchMode.EAGER)
    .list();

这篇关于如何在运行时重写hibernate获取策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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