Hibernate延迟加载

延迟加载
            1.立即加载:Hibernate查询Dept对象时,立即查询并加载与之关联的Emp对象
                立即加载的缺点:1.select语句的数目太多,需要频繁的访问数据库,会影响查询性能
                            2.在应用程序只需要访问Dept对象,不需要访问Emp对象大的场合,加载Emp完全是多余
                                的操作,浪费了许多的内存空间
                                
            2.延迟加载:能避免加载应用程序不需访问的关联对象
            3.lazy:用于设定加载策略
                属性:  类级别:<class>元素中lazy属性的可选值为: false(立即加载)  默认为true(延迟加载)
                
                        一对多的关联级别:<set>元素中lazy属性的可选值为:true(延迟加载)
                            false(立即加载)、extra(增强延迟加载) <set>元素的lazy属性默认值为true
                            
                        多对一关联级别:<many-to-one>元素中国lazy属性的可选值为:proxy(延迟加载)
                                            no-proxy(无代理延迟加载)和false(立即加载) 默认值为proxy
                    
                    注:session的get()方法及Query对象的list()方法在Dept类级别总是使用立即加载策略
                        get()方法永远不会返回dept代理类实例

                
            4.Open Session In View模式:在用户的每一次请求过程中始终保持一个Session打开
            5.OpenSessionInView模式的具体实现由以下三个步骤
                第一步:把Session绑定到当前线程上,要保证在异常请求只有一个Session对象
                第二步:用Filter过滤器在请求到达之前打开Session,在响应返回前关闭Session
                第三步:编写Dao层的代码
                
                
                eg:
                    OpenSessionInViewFilter.java
                    
                        public class OpenSessionInViewFilter implements Filter{
                            @Override
                            public vodi doFilter(ServletRequest request,ServletResponse response,FilterChain arg2)
                                    throws IOException,ServletException{
                                    
                                //请求到达时,打开Session并启动事务
                                Session session = null;
                                Tranaction tx = null;
                                try{
                                    session = HibernateUtil.currentSession();
                                    tx = session.beginTransaction();
                                    //执行请求处理链
                                    arg2.doFilter(request,response);
                                    //返回响应时,提交事务
                                    tx.commit();
                                }catch(HibernateException e){
                                
                                    e.printStackTrace();
                                    tx.rollback();
                                }finally{
                                    HibernateUtil.closeSession();
                                }
                            }
                            public void destroy(){}
                            public void init(FilterConfig config) throws ServletException{}
                        }
                        
                    web.xml中Filter 配置:
                        
                        <filter>
                            <filter-name>openSessionInview</filter-name>
                            <filter-class>cn.jbit.hibernatedemo.web.OpenSessionInViewFilter</filter-class>
                        </filter>
                        <filter-mapping>
                            <filter-name>openSessionInview</filter-name>
                            <url-pattern>/*</url-pattern>
                        </filter-mapping>
                    
                    DeptDaoImpl.java
                        
                        public class DeptDaoImpl implements IDeptDao{
                            public Dept selectDeptById(Byte id)  throws Exception{
                                Session session = HibernateUtil.currentSession();
                                return (Dept)session.load(Dept.class,id);
                            }
                        }

作者:JamelAr
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!

本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/JamelAr/p/6489920.html