- 浏览: 251941 次
- 性别:
- 来自: 上海
最新评论
-
lazyox_2008:
多谢楼主配对那一段,解决了我的大问题啊
android蓝牙主动发起配对实例 -
yandol:
这个反射的createBond用法很有意思 这个代码能像非an ...
android蓝牙主动发起配对实例 -
hzxlb910:
例子还是短小看着一目了然。不错
初识AOP -
lteb2002:
在WEB中,是使用WebApplicationContext的 ...
[转]Spring中WebApplicationContext的研究 -
Mark_dev:
请问一下楼主,这个UUID是不是和设备有关啊,我使用你的 co ...
android蓝牙主动发起配对实例
百事通信息网
我的demo小程序随着我学习的过程、体积也越来越庞大起来了、开始只是用来测试hibernate、渐渐的Spring,struts,dwr(我在用来做验证的过程因为异步,效果反而不好,所以后来拿掉了)的身影也出现了,到如今他已经有注册、登陆、发贴、查看主题、查看跟贴,回贴的能耐了,呵呵,为他而高兴。在前面学习的过程中先避开一些难点,比如多表查询、AOP切面编程等等,不过为了满足需求,今天开始将多表应用上去了,确实遇到了不少麻烦,花了不少的时间,当然在遇到问题,解决问题的过程中让我回顾了很多要点、盲点,也值了。
现在将关键点记录下来,以免日后再有相同问题出现,以便查阅。
二张表分别为用户表(users)和贴子表(notes),其中users中的userid是notes表的外键,users跟notes是一对多,多对一关系,由myEclpise自动生成映射文件
Users.hbm.xml
Notes.hbm.xml
注意:如果多表查询,在写HQL语句时取notes的userid字段不能写notes.userid(这个问题下午我查了好久,才找到原因的)
HQL语句:From Notes notes,Users users WHERE notes.users=users.userid AND notes.issubject=1
又一注意点:该查询结果为存放对象数组的list集,可以用以下代码测试
在jsp页面显示结果的时候我觉得用EL表达式会简单的多,如:
是的,list中存放你所查询对po的集合,多个对象以数组构建list
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:
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.
这种设计使用hibernate无疑是鸡肋表现,无法解决。
那什么样的设计才使用hibernate呢?
这种设计使用hibernate无疑是鸡肋表现,无法解决。
Users(POJO)中没有userid成员
这样做很好啊,不如用排序组合算法算了。拿出数据集合还需新组装成页面需求集合,几乎说是严重浪费人力物力表现。真不知道你是如何想得。
我的demo小程序随着我学习的过程、体积也越来越庞大起来了、开始只是用来测试hibernate、渐渐的Spring,struts,dwr(我在用来做验证的过程因为异步,效果反而不好,所以后来拿掉了)的身影也出现了,到如今他已经有注册、登陆、发贴、查看主题、查看跟贴,回贴的能耐了,呵呵,为他而高兴。在前面学习的过程中先避开一些难点,比如多表查询、AOP切面编程等等,不过为了满足需求,今天开始将多表应用上去了,确实遇到了不少麻烦,花了不少的时间,当然在遇到问题,解决问题的过程中让我回顾了很多要点、盲点,也值了。
现在将关键点记录下来,以免日后再有相同问题出现,以便查阅。
二张表分别为用户表(users)和贴子表(notes),其中users中的userid是notes表的外键,users跟notes是一对多,多对一关系,由myEclpise自动生成映射文件
Users.hbm.xml
...... <set name="noteses" inverse="true"> <key> <column name="userid" not-null="true" /> </key> <one-to-many class="com.note.model.Notes" /> </set> ......
Notes.hbm.xml
...... <many-to-one name="users" class="com.note.model.Users" fetch="select"> <column name="userid" not-null="true" /> </many-to-one> ......
注意:如果多表查询,在写HQL语句时取notes的userid字段不能写notes.userid(这个问题下午我查了好久,才找到原因的)
HQL语句:From Notes notes,Users users WHERE notes.users=users.userid AND notes.issubject=1
又一注意点:该查询结果为存放对象数组的list集,可以用以下代码测试
page=ins.listSubjects(page);//page是我的分页类,并存放了查询后的返回结果 List list=page.getResult(); System.out.println(page.getPageToolBar()); for(int i=0;i<list.size();i++){ Object[] obj=(Object[])list.get(i); for(int j=0;j<obj.length;j++){ if(obj[j] instanceof Notes){ Notes note=(Notes)obj[j]; System.out.print(note.getTitle()); }else if(obj[j] instanceof Users){ Users user=(Users)obj[j]; System.out.print(user.getUsername()); } } System.out.println(); }
在jsp页面显示结果的时候我觉得用EL表达式会简单的多,如:
<logic:present name="subjects"> <table width="100%" border="1" rules="rows" frame="below" cellpadding="5" cellspacing="0" bordercolorlight="#6C7BA6" bordercolordark="#ffffff" bgcolor="#DEEFFF" class="DoubleColorTable"> <logic:present name="subjects" property="result"> <logic:iterate id="objs" name="subjects" indexId="number" property="result"> <tr> <td> <html:link page="/listFollow.do?method=listFollow&subjectId=${objs[0].noteid}" styleId="link001"> 主题:${objs[0].title} </html:link> </td> <td> 作者:${objs[1].username} </td> <td> 发贴时间:${objs[0].addtime} </td> </tr> </logic:iterate> </logic:present> </table> <bean:write name="subjects" property="pageToolBar" filter="false" /> </logic:present>
评论
18 楼
379548695
2007-09-19
好东西欧
17 楼
magic.chen
2007-09-11
请问:延迟加载能支持多表查询吗?支持的话,怎么去掉重复数据(set吗?),那要是排序呢?
16 楼
sun113
2006-11-12
有收获,写得不赖!
15 楼
wiley
2006-11-11
sun113 写道
又一注意点:该查询结果为存放对象数组的list集,可以用以下代码测试
请问list集中存放的是什么,使相关的user对象和note对象,对不对?
请问list集中存放的是什么,使相关的user对象和note对象,对不对?
是的,list中存放你所查询对po的集合,多个对象以数组构建list
14 楼
sun113
2006-11-11
又一注意点:该查询结果为存放对象数组的list集,可以用以下代码测试
请问list集中存放的是什么,使相关的user对象和note对象,对不对?
请问list集中存放的是什么,使相关的user对象和note对象,对不对?
13 楼
andyao
2006-11-09
wiley 写道
[quote=ppm10103 ]hibernate多表查询时可以采用延迟加载来提高效率,这应该是一种方法吧,不过我还有一个问题,在1:N的情况下,如果是1:10000那延迟加载时是不是会把10000条都加载进来,能不能控时加载数量呀?
--------
延迟加载加载的是代理类,如果不显示访问是不会加载内容的,
如果允许外连接也要控制一下连接深度
今天用DWR+Spring做整合时,发现真的跟你说的一样,确实是只要显式访问时才加载,不过通过DWR去迟加载时说session已被关闭.这个问题怎么解决呢
--------
延迟加载加载的是代理类,如果不显示访问是不会加载内容的,
如果允许外连接也要控制一下连接深度
今天用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.
12 楼
wiley
2006-11-09
[quote=ppm10103 ]hibernate多表查询时可以采用延迟加载来提高效率,这应该是一种方法吧,不过我还有一个问题,在1:N的情况下,如果是1:10000那延迟加载时是不是会把10000条都加载进来,能不能控时加载数量呀?
--------
延迟加载加载的是代理类,如果不显示访问是不会加载内容的,
如果允许外连接也要控制一下连接深度
今天用DWR+Spring做整合时,发现真的跟你说的一样,确实是只要显式访问时才加载,不过通过DWR去迟加载时说session已被关闭.这个问题怎么解决呢
--------
延迟加载加载的是代理类,如果不显示访问是不会加载内容的,
如果允许外连接也要控制一下连接深度
今天用DWR+Spring做整合时,发现真的跟你说的一样,确实是只要显式访问时才加载,不过通过DWR去迟加载时说session已被关闭.这个问题怎么解决呢
11 楼
ppm10103
2006-11-06
hibernate多表查询时可以采用延迟加载来提高效率,这应该是一种方法吧,不过我还有一个问题,在1:N的情况下,如果是1:10000那延迟加载时是不是会把10000条都加载进来,能不能控时加载数量呀?
--------
延迟加载加载的是代理类,如果不显示访问是不会加载内容的,
如果允许外连接也要控制一下连接深度
--------
延迟加载加载的是代理类,如果不显示访问是不会加载内容的,
如果允许外连接也要控制一下连接深度
10 楼
wiley
2006-11-05
hibernate多表查询时可以采用延迟加载来提高效率,这应该是一种方法吧,不过我还有一个问题,在1:N的情况下,如果是1:10000那延迟加载时是不是会把10000条都加载进来,能不能控时加载数量呀?
9 楼
wiley
2006-11-02
santafeng 写道
wiley 写道
to:santafeng
我也是初学hibernate,并不是很深入,你能详述你在多表查询时的做法吗?谢谢
引用
这样做很好啊,不如用排序组合算法算了。拿出数据集合还需新组装成页面需求集合,几乎说是严重浪费人力物力表现。真不知道你是如何想得。
我也是初学hibernate,并不是很深入,你能详述你在多表查询时的做法吗?谢谢
这种设计使用hibernate无疑是鸡肋表现,无法解决。
那什么样的设计才使用hibernate呢?
8 楼
santafeng
2006-11-02
wiley 写道
to:santafeng
我也是初学hibernate,并不是很深入,你能详述你在多表查询时的做法吗?谢谢
引用
这样做很好啊,不如用排序组合算法算了。拿出数据集合还需新组装成页面需求集合,几乎说是严重浪费人力物力表现。真不知道你是如何想得。
我也是初学hibernate,并不是很深入,你能详述你在多表查询时的做法吗?谢谢
这种设计使用hibernate无疑是鸡肋表现,无法解决。
7 楼
wiley
2006-11-02
to:santafeng
我也是初学hibernate,并不是很深入,你能详述你在多表查询时的做法吗?谢谢
引用
这样做很好啊,不如用排序组合算法算了。拿出数据集合还需新组装成页面需求集合,几乎说是严重浪费人力物力表现。真不知道你是如何想得。
我也是初学hibernate,并不是很深入,你能详述你在多表查询时的做法吗?谢谢
6 楼
wiley
2006-11-02
引用
注意:如果多表查询,在写HQL语句时取notes的userid字段不能写notes.userid(这个问题下午我查了好久,才找到原因的)
HQL语句:From Notes notes,Users users WHERE notes.users=users.userid AND notes.issubject=1
问下 为何不能写成notes.userid 而要notes.users?
HQL语句:From Notes notes,Users users WHERE notes.users=users.userid AND notes.issubject=1
问下 为何不能写成notes.userid 而要notes.users?
Users(POJO)中没有userid成员
5 楼
jd2bs
2006-11-02
注意:如果多表查询,在写HQL语句时取notes的userid字段不能写notes.userid(这个问题下午我查了好久,才找到原因的)
HQL语句:From Notes notes,Users users WHERE notes.users=users.userid AND notes.issubject=1
问下 为何不能写成notes.userid 而要notes.users?
HQL语句:From Notes notes,Users users WHERE notes.users=users.userid AND notes.issubject=1
问下 为何不能写成notes.userid 而要notes.users?
4 楼
santafeng
2006-11-02
这样做很好啊,不如用排序组合算法算了。拿出数据集合还需新组装成页面需求集合,几乎说是严重浪费人力物力表现。真不知道你是如何想得。
3 楼
cncomkyle
2006-11-01
我把你的查选考虑成外连接了,如果是外连接,会有出现null的情况
2 楼
wiley
2006-11-01
obj[j]应该不会出现null,只有list才有可能出现null所以在循环外加if判断就可以了
1 楼
cncomkyle
2006-11-01
if(obj[j] instanceof Notes){
Notes note=(Notes)obj[j];
System.out.print(note.getTitle());
}else if(obj[j] instanceof Users){
Users user=(Users)obj[j];
System.out.print(user.getUsername());
}
当obj[j]为null时的情况,怎么样处理呢
Notes note=(Notes)obj[j];
System.out.print(note.getTitle());
}else if(obj[j] instanceof Users){
Users user=(Users)obj[j];
System.out.print(user.getUsername());
}
当obj[j]为null时的情况,怎么样处理呢
发表评论
-
android数据库操作(二)
2011-11-12 00:45 1453package cn.madfinger.android.co ... -
android数据库操作(一)
2011-10-31 13:18 1742为了以后的项目使用Android数据库更方便,自已写了一个小的 ... -
android蓝牙主动发起配对实例
2011-09-24 08:39 25431package cn.madfinger.core; i ... -
最给力的办公平台
2011-02-24 01:02 1553SUCANG协同采用AJAX、J2EE、MV ... -
DWR应用总结
2006-11-11 10:47 9998百事通信息网 配置调试: DWR的配置说明,网上已经是N多 ... -
[求教]关于DWR+Spring延迟加载的问题
2006-11-09 12:10 7080百事通信息网 今天用DWR+Spring做整合时,发现不能通 ... -
[转]Hibernate应用中如何处理批量更新和批量删除?
2006-11-02 10:36 10168百事通信息网 在Hibernate应用中如何处理批量更新和 ... -
初识AOP
2006-11-02 00:07 4332百事通信息网 学习Spring AOP之前,先来了 ... -
AOP的应用领域
2006-11-01 11:46 2514百事通信息网 AOP的应用领域: Authentica ... -
spring+hibernate之applicationContext.xml配置
2006-10-27 20:31 39653百事通信息网 最近一段时间潜心自学struts,spring ... -
Spring用回调HibernateCallBack方法
2006-10-27 20:28 7632百事通信息网 Spring用回调HibernateCall ... -
[转]Spring中WebApplicationContext的研究
2006-10-27 20:27 5884百事通信息网 作者:laoer 引自:http://b ... -
struts+spring整合测试
2006-10-27 20:24 6770百事通信息网 struts+spr ... -
将action得到的值,通过request显示到页面中
2006-10-27 20:13 2469百事通信息网 今天又遇到一个问题,将action ... -
WEB分页技术
2006-10-27 19:49 3899百事通信息网 web应用开发中,分页是一项很重要的技术,分 ... -
DWR+SPRING整合
2006-10-27 19:41 3490百事通信息网 Ajax,它 ... -
按步就班使用struts验证框架
2006-10-26 22:19 4055使用动态FormBean与struts验证框架 第一步,安装 ... -
[转]Log4J 最佳实践之全能配置文件
2006-10-26 16:39 1504LOG4J的配置之简单使它遍及于越来越多的应用中了:Log4J ... -
[转]Log4J 最佳实践之全能配置文件
2006-10-26 16:38 1474LOG4J的配置之简单使它遍及于越来越多的应用中了:Log4J ... -
[转]关于hibernate中的锁机制
2006-10-26 14:57 1601(转载)关于hibernate中的 ...
相关推荐
hibernate 多表查询 hibernate 多表不同于一般的sql嵌套查询,嵌套查询是不支持的,所以必须要配置好才能实现多表!只要配置好了,还是特别好用的
hibernate多表联合查询实例,很经典的查询语句和总结.
该源代码配了了解释,针对hibernate中多表查询的多种形式进行了总结,很有价值,有Object[]形式的查询,也有对象方式的查询 也有集合list形式的查询,
Hibernate连表查询 Hibernate连表查询
Hibernate多表查询.txt
hibernate多表查询[定义].pdf
Hibernate 多表连接分页查询示范项目 Hibernate Criteria 多表连接 分页
Hibernate 多表映射关系配置 如,一对一 多对一 多对多等
NULL 博文链接:https://08284008.iteye.com/blog/1456476
Hibernate多对多关联添加及查询示例
NULL 博文链接:https://gaoquanyang.iteye.com/blog/1213765
使用Struts2+Hibernate实现对部门表、员工表两张表的增删改查。
Hibernate中使用Criteria Query各种QBC查询
hibernate实现动态表查询的多种解决方案.自行整理的动太实现方案,总有一个适合你.
Hibernate_query查询数据表中的一个字段.
Hibernate_QBC查询
Hibernate中的关联查询实际上生成的是数据库表连接查询的SQL语句
利用MYSQL数据库提供的样例数据库world建立的查询小例子。已经建立好了数据库对应的...本项目主要是为了练习使用Hibernate查询数据库。如果有什么问题可以发站内信或者邮件asddf14@hotmail.com。我会尽量解答一下~~~
struts2+hibernate+spring多表操作
博客对应代码