论坛首页 Java企业应用论坛

hibernate多表查询

浏览 20044 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2006-11-05  
hibernate多表查询时可以采用延迟加载来提高效率,这应该是一种方法吧,不过我还有一个问题,在1:N的情况下,如果是1:10000那延迟加载时是不是会把10000条都加载进来,能不能控时加载数量呀?
0 请登录后投票
   发表时间:2006-11-06  
hibernate多表查询时可以采用延迟加载来提高效率,这应该是一种方法吧,不过我还有一个问题,在1:N的情况下,如果是1:10000那延迟加载时是不是会把10000条都加载进来,能不能控时加载数量呀?
--------
延迟加载加载的是代理类,如果不显示访问是不会加载内容的,
如果允许外连接也要控制一下连接深度
0 请登录后投票
   发表时间:2006-11-09  
[quote=ppm10103 ]hibernate多表查询时可以采用延迟加载来提高效率,这应该是一种方法吧,不过我还有一个问题,在1:N的情况下,如果是1:10000那延迟加载时是不是会把10000条都加载进来,能不能控时加载数量呀?
--------
延迟加载加载的是代理类,如果不显示访问是不会加载内容的,
如果允许外连接也要控制一下连接深度

今天用DWR+Spring做整合时,发现真的跟你说的一样,确实是只要显式访问时才加载,不过通过DWR去迟加载时说session已被关闭.这个问题怎么解决呢
0 请登录后投票
   发表时间:2006-11-09  
wiley 写道
[quote=ppm10103 ]hibernate多表查询时可以采用延迟加载来提高效率,这应该是一种方法吧,不过我还有一个问题,在1:N的情况下,如果是1:10000那延迟加载时是不是会把10000条都加载进来,能不能控时加载数量呀?
--------
延迟加载加载的是代理类,如果不显示访问是不会加载内容的,
如果允许外连接也要控制一下连接深度

今天用DWR+Spring做整合时,发现真的跟你说的一样,确实是只要显式访问时才加载,不过通过DWR去迟加载时说session已被关闭.这个问题怎么解决呢


Hibernate文档里面有说明,应该多看看文档

19.1.1. Working with lazy associations
By default, Hibernate3 uses lazy select fetching for collections and lazy proxy fetching for single-valued associations. These defaults make sense for almost all associations in almost all applications.

Note: if you set hibernate.default_batch_fetch_size, Hibernate will use the batch fetch optimization for lazy fetching (this optimization may also be enabled at a more granular level).

However, lazy fetching poses one problem that you must be aware of. Access to a lazy association outside of the context of an open Hibernate session will result in an exception. For example:
s = sessions.openSession();
Transaction tx = s.beginTransaction();
            
User u = (User) s.createQuery("from User u where u.name=:userName")
    .setString("userName", userName).uniqueResult();
Map permissions = u.getPermissions();

tx.commit();
s.close();

Integer accessLevel = (Integer) permissions.get("accounts");  // Error!

Since the permissions collection was not initialized when the Session was closed, the collection will not be able to load its state. Hibernate does not support lazy initialization for detached objects. The fix is to move the code that reads from the collection to just before the transaction is committed.

Alternatively, we could use a non-lazy collection or association, by specifying lazy="false" for the association mapping. However, it is intended that lazy initialization be used for almost all collections and associations. If you define too many non-lazy associations in your object model, Hibernate will end up needing to fetch the entire database into memory in every transaction!

On the other hand, we often want to choose join fetching (which is non-lazy by nature) instead of select fetching in a particular transaction. We'll now see how to customize the fetching strategy. In Hibernate3, the mechanisms for choosing a fetch strategy are identical for single-valued associations and collections.
0 请登录后投票
   发表时间:2006-11-11  
又一注意点:该查询结果为存放对象数组的list集,可以用以下代码测试

请问list集中存放的是什么,使相关的user对象和note对象,对不对?
0 请登录后投票
   发表时间:2006-11-11  
sun113 写道
又一注意点:该查询结果为存放对象数组的list集,可以用以下代码测试

请问list集中存放的是什么,使相关的user对象和note对象,对不对?

是的,list中存放你所查询对po的集合,多个对象以数组构建list
0 请登录后投票
   发表时间:2006-11-12  
有收获,写得不赖!
0 请登录后投票
   发表时间:2007-09-11  
请问:延迟加载能支持多表查询吗?支持的话,怎么去掉重复数据(set吗?),那要是排序呢?
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics