休眠,要懒惰还是不要懒惰? [英] hibernate, to be lazy or not to be lazy?

查看:163
本文介绍了休眠,要懒惰还是不要懒惰?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有实体A ,与实体B 有多对多的关系。



所以表格布局是: A,AB(映射表),B



要获取实体A的对象:
我调用 A.getById()其中 getHibernateTemplate( ).get(A.class,id)使用spring和hibernate。



问题是,有时接下来的代码只需要A,有时接下来的代码将会继续访问关联的B,所以我们想使用延迟加载在某些情况下, 渴望在某些情况下。但问题是所有的数据库访问是通过相同的单一 ADao.java 提供的,所以只有一种方法 getById()

我应该创建两个版本的方法getById()?



但是对于更复杂的情况,如果A也通过多对多连接到C,那么可能会有懒加载C和加载C的变体,所以需要的



你对这个选择有什么看法?



谢谢

解决方案

有关一般注意事项,请查看Hibernate 3.6文档关于提取策略。在映射注释或hbm.xml文件中定义了默认提取策略。有三种方法可以从默认的延迟加载策略动态切换到热切加载策略。前两个需要单独实现DAO方法,用于延迟加载和加载用例:


  1. 标准。

  2. HQL查询中的FETCH 关键字中的setFetchMode()

  3. 由于Hibernate 3.5(现在不太确定,也许是3.6),第三个选项是使用 fetch profiles 从动态切换到延迟加载。

在会话范围内启用/禁用提取配置文件。因此,如果在当前会话中设置了所需的抓取配置文件,则可以使用相同的DAO方法进行延迟加载以及加载用例。



这里需要注意的一点是,您只能从注释或hbm.xml文件中定义的延迟加载策略转换为热切加载策略强>而不是反之亦然。该限制与用于切换提取策略的方法无关。


I have entity A, which has a many-to-many relation to entity B.

So the table layout is : A, AB(mapping table), B

To get an object of entity A: I call A.getById() which does getHibernateTemplate().get(A.class, id) using spring and hibernate.

The problem is, sometimes ensuing code will just need A, sometimes ensuing code will continue to access the associated B's, so we'd like to use lazy loading in some cases and eager in some other cases. but the problem is that all database access is provided through the same single ADao.java, so there is only one method getById().

Should I create two versions of method getById()?

But then for more complex cases, if A is also attached to C through many-to-many, then there could be variants of lazy-loading-C and eager-loading-C,so the required getById() variants quickly grows exponentially.

what is your opinion on this choice?

Thanks

解决方案

For general considerations take a look at the Hibernate 3.6 docs about fetching strategies. The default fetching strategy is defined in mapping annotations or in a hbm.xml file. There are three ways to dynamically switch from a default lazy-loading strategy to an eager-loading strategy. The first two require separate implementations of DAO methods for lazy-loading and eager-loading use cases:

  1. Criteria.setFetchMode() in a Hibernate Criteria query
  2. FETCH keyword in a HQL query
  3. Since Hibernate 3.5 (not quite sure now, maybe 3.6) there is the third option of using fetch profiles to dynamically switch from lazy-loading to eager-loading.

A fetch profile is enabled/disabled on a session scope. So, provided the desired fetch profile is set in the current session, you can use the same DAO method for a lazy-loading as well as for an eager-loading use case.

The important thing to be aware of here is that you can only switch from a lazy-loading strategy defined in annotations or in a hbm.xml file to an eager-loading strategy and not vice versa. This restriction is independent of the method used for switching the fetching strategy.

这篇关于休眠,要懒惰还是不要懒惰?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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