浏览 2010 次
锁定老帖子 主题:hibernate与spring整合的问题
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-09-26
Hibernate: select spring0_.id as col_0_0_ from spring spring0_ where spring0_.username=? Hibernate: select spring0_.id as col_0_0_ from spring spring0_ where spring0_.username=? Hibernate: select spring0_.id as id0_0_, spring0_.username as username0_0_, spring0_.password as password0_0_ from spring spring0_ where spring0_.id=? Hibernate: select spring0_.id as col_0_0_ from spring spring0_ where spring0_.username=? Hibernate: select spring0_.id as col_0_0_ from spring spring0_ where spring0_.username=? Hibernate: select spring0_.id as id0_0_, spring0_.username as username0_0_, spring0_.password as password0_0_ from spring spring0_ where spring0_.id=? Hibernate: select spring0_.id as col_0_0_ from spring spring0_ where spring0_.username=? Hibernate: select spring0_.id as col_0_0_ from spring spring0_ where spring0_.username=? Hibernate: select spring0_.id as id0_0_, spring0_.username as username0_0_, spring0_.password as password0_0_ from spring spring0_ where spring0_.id=? 其中我只用了一次向数据库查询数据怎么这一句 Hibernate: select spring0_.id as col_0_0_ from spring spring0_ where spring0_.username=?要执行两此 其中查询是在spring的validator中中进行的事物管理使用spring的声明式事物管理 引用 Spring DBuser = udao.getUserByName(user.getUsername());
而getUserByName()的定义如下 public class SpringDaoImpl extends HibernateDaoSupport implements SpringDao { public Spring getUserByName(String name) { Session session = this.getSession(); Query q = session.createQuery("from Spring s where s.username=?"); q.setString(0, name); Spring user = null; if(q.iterate().hasNext()) { user = (Spring) q.iterate().next(); } return user; } } 而现在我有试着重新把这个方法再定义下,其改后如下 public Spring getUserByName(String name) { List list = this.getHibernateTemplate().findByNamedParam( "from Spring s where s.username=:name", "name", name); Iterator it = list.iterator(); Spring user = null; if (it.hasNext()) { user = (Spring) it.next(); } return user; } 这时我重新运行这个web程序 看下运行的结果,却发现这时没有hibernate所说的lazy-load(懒集合),而是直接从database里把所有的数据加进来 其运行两次的结果如下: Hibernate: select spring0_.id as id0_, spring0_.username as username0_, spring0_.password as password0_ from spring spring0_ where spring0_.username=? Hibernate: select spring0_.id as id0_, spring0_.username as username0_, spring0_.password as password0_ from spring spring0_ where spring0_.username=? 这是否就意味着没有使用到hibernate提供的性能优化的功能呢?或者这背后还有更多的考虑呢?真是迷惑啊,还请各位指点。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |