`

Session.find()方法

阅读更多

Session.find()方法

 我被Session.find()的方法困扰了好几天,今天才看到新的Hibernate里没有了Session.find()方法。
现在转载在此,方便你我。

查询性能往往是系统性能表现的一个重要方面,查询机制的优劣很大程度上决定了系统的整体性能。这个领域往往也存在最大的性能调整空间。

hibernate2中Session.find()对应于3中的session.createQuery().list();
hibernate2中Session.iterate()对应于3中的session.createQuery().iterate();
find和iterate区别:
find方法通过一条Select SQL实现了查询操作,而iterate方法要执行多条Select SQL.
iterate第一次查询获取所有符合条件的记录的id,然后再根据各个id从库表中读取对应的记录,这是一个典型的N+1次的查询问题,如果符合条件记录有10000条,就需要执行10001条Select SQL,可想性能会如何的差。

那为什么要提供iterator方法,而不只是提供高效率的find方法?

原因1.与hibernate缓存机制密切相关
find方法实际上是无法利用缓存的,它对缓存只写不读。
find方法只执行一次SQL查询,它无法判断缓存中什么样的数据是符合条件的,也无法保证查询结果的完整性。而iterate方法,会首先查询所有符合条件记录的id,然后根据id去缓存中找,如果缓存中有该id,就返回,没有可以根据id再去数据库查询。
String hql = "from TUser where age > ?";
List userList = session.find(hql, new Integer(18), Hibernate.INTEGER);
Iterator it = session.iterate(hql, new Integer(18), Hibernate.INTEGER);
顺序执行,iterate方法只会执行一次SQL查询,就是查找id,然后根据id就可以从缓存中获得数据。

String hql = "from TUser where age > ?";
List userList = session.find(hql, new Integer(18), Hibernate.INTEGER);
userList = session.find(hql, new Integer(18), Hibernate.INTEGER);
缓存是不起作用的。
如果目标数据读取相对较为频繁,通过iterate这种机制,会减少性能损耗。

原因2.内存使用上的考虑
find方法将一次获得的所有记录并将其读入内存。如果数据量太大,可能会触发OutOfMemoryError,从而导致系统异常。解决方案之一就是结合iterate方法和evict方法逐条对记录进行处理,将内存消化保持在一个可以接受的范围之内。如:
String hql = "from TUser where age > ?";
Iterator it = session.iterate(hql, new Integer(18), Hibernate.INTEGER);
while(it.hasNext()) {
    TUser user = (TUser)it.next();
   
    //将对象从一级缓存中删除
    session.evict(user);

    //二级缓存可以设定最大缓存量,达到后自动对较老数据进行废除,但也可以通过编
    //码移除,这样有助于保持数据有效性。
    sessionFactory.evict(TUser.class, user.getID());

分享到:
评论

相关推荐

    Hibernate持久层方法汇总

    Hibernate持久层方法汇总 session.load, session.find, session.iterator, session.save, session.update, session.saveorupdate

    tomcat8用redis实现session共享.rar

    ... 3、将redisson-all-3.11.2.jar和redisson-tomcat-8-3.11.2.jar 拷贝到${catalina.base}\lib下 4、在原有tomcat\context.xml下 添加 以下代码(参考 <Manager className="org.redisson.tomcat....

    java小项目

    List<Message> msgList = mDao.findAll(); if(null == u.getUname()){ response.sendRedirect("jspPages/login.jsp"); }else{ HttpSession session = request.getSession(); session.setAttribute("userName",...

    struts2 session 解读

    例: public String findAll(){ HttpSession session = ServletActionContext.getRequest().getSession(); list = userService.find(); session.setAttribute(“list”, list); return this.SUCCESS;

    jsp Hibernate批量更新和批量删除处理代码

    Iterator customers=session.find(“from Customer c where c.age>0”).iterator();while(customers.hasNext()){Customer customer=(Customer)customers.next();customer.setAge(customer.getAge()+1);} tx.commit()...

    轻量级数据持久层组件Restful.Data.zip

    }SessionHelper的使用:SessionHelper对session对象的方法进行了静态封装,如果你只是需要执行单条语句,并马上关闭连接,你可以使用 SessionHelper 类中提供的一些辅助方法。5、如何定义实体类[Serializable] ...

    php.ini-development

    session.hash_bits_per_character ; Default Value: 4 ; Development Value: 5 ; Production Value: 5 ; short_open_tag ; Default Value: On ; Development Value: Off ; Production Value: Off ; track_errors ...

    pro_apache_third_edition..pdf

    Contents About the Author...............................................................................................xix About the Technical Reviewer and Contributing Author.................xxi ...

    EJB3.0实例教程

    4.3 STATELESS SESSION BEAN与STATEFUL SESSION BEAN的区别...............................................................................22 4.4 如何改变SESSION BEAN的JNDI 名称...............................

    cookie中用字符串方式存储session信息实现的购物车模块

    List<Computer> findAll() save(Computer computer); ComputerDAOJdbcImpl 测试dao: 购物车的设计: CartItem //商品条目 属性: int qty //购买产品的数量 Computer c //购买的产品是什么 方法: get/...

    EJB3.0 实例教程 -- 切片2

    6.5.1 Entity获取find()....43 6.5.2 添加 persist().........43 6.5.3 更新 Merge() .........44 6.5.4 删除 Remove() .......44 6.5.5 执行 EJB3 QL操作createQuery() 44 6.6 关系/对象映射.45 6.6.1 映射的表名或...

    unix power tools

    Table of Contents 1. Introduction....................................................................................................................................................2 ...

    python学习总结day03.txt

    2、正则方法 p = re.compile('....') r_list = p.findall(html) 结果 :[(),(),(),()] 3、贪婪匹配 : .* 4、非贪婪匹配 :.*? 2、抓取步骤 1、找URL 2、写正则表达式 3、定义类,写程序框架 4、补全代码 3...

    Springer.The.Developer’s.Guide.to.Debugging.2008.pdf

    3.8 A Debug Session on a Simple Example . . . . . . 30 4 Fixing Memory Problems . . . . . . . . . . . . 33 4.1 Memory Management in C/C++ – Powerful but Dangerous . . . . 33 4.1.1 Memory Leaks . . ....

    haproxy-1.5.14.tar

    In some cases, a client might be able to cause a buffer alignment issue and retrieve uninitialized memory contents that exhibit data from a past request or session. I want to address sincere ...

    xmnt 2002 program not find的解决方法(分区魔术师的用法)

    解决出现上述错误的方法很简单,修改注册表中的键值即可(windows XP中): 1.开始菜单――运行 ,输入regedit打开注册表 2.在注册表中找到以下位置: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session ...

    vld-2.5.1-setup.exe

    When you run your program under the Visual Studio debugger, Visual Leak Detector will output a memory leak report at the end of your debugging session. The leak report includes the full call stack ...

    数据库测试test.sql

    // //根据method属性的值调用相应的方法 // if("login".equals(methodName)){ // this.login(request,response); // }else if("register".equals(methodName)){ // this.register(request,response); // }else if(...

    VereORM至简持久层微架构

    Object obj=session.findOne(User.class, "select * from user where id=1");//原生SQL if(obj!=null) { User user=(User)obj; System.out.println(user.getId()+" | "+user.getName()+" | "+user.getAge()+...

    Debugging with GDB --2003年6.0

    1 A Sample gdb Session . . . . . . . . . . . . . . . . . . . . . . 7 2 Getting In and Out of gdb . . . . . . . . . . . . . . . . 11 2.1 Invoking gdb . . . . . . . . . . . . . . . . . . . . . . . . . ....

Global site tag (gtag.js) - Google Analytics