ssh lazy机制(懒惰的机制ssh)

上传人:xins****2008 文档编号:117439850 上传时间:2019-12-05 格式:DOC 页数:32 大小:29.09KB
返回 下载 相关 举报
ssh lazy机制(懒惰的机制ssh)_第1页
第1页 / 共32页
ssh lazy机制(懒惰的机制ssh)_第2页
第2页 / 共32页
ssh lazy机制(懒惰的机制ssh)_第3页
第3页 / 共32页
ssh lazy机制(懒惰的机制ssh)_第4页
第4页 / 共32页
ssh lazy机制(懒惰的机制ssh)_第5页
第5页 / 共32页
点击查看更多>>
资源描述

《ssh lazy机制(懒惰的机制ssh)》由会员分享,可在线阅读,更多相关《ssh lazy机制(懒惰的机制ssh)(32页珍藏版)》请在金锄头文库上搜索。

1、ssh lazy机制(懒惰的机制SSH)Query without lazyOne: multi table query of single dataWhen using super.getHibernateTemplate ().Get () queries, you can get the data of the tables associated with himSuch as: petInfo and petDiary tablesApplicationContext CTX = new ClassPathXmlApplicationContext (applicationContex

2、t.xml);PetBiz biz = (PetBiz) ctx.getBean (petBiz);PetInfo p= biz.get (9);Iterator t= p.getPetDiaries ().Iterator ();While (t.hasNext () PetDiary o= (PetDiary) t.next ();System.out.println (o.getDiaryAuthor (); data obtained from PetInfo based on petDiaryTwo: multi table query of multiple dataYou can

3、not use hql= from petInfo to get multiple tables of data, and use connection queriesSuch as:Hql= from PetDiary d inner join fetch d.petInfo-Use the lazy query - delayed retrieval strategyThe lazy attribute of the element of the petInfo.Hbm.xml file is set to true, which represents the use of a delay

4、 retrieval strategySo using the above get () to get a single data will be wrong:Error information for:A delayed search strategy is configured for a class or set,You must initialize the proxy class instance or the set of agents in a persistent state, that is, within the Session range.If it is initial

5、ized in the free state, a delay initialization error will occur.When executing the load () method of Session, Hibernate does not immediately execute the select statement for querying the CUSTOMERS table,Just returning an instance of the proxy class of the Customer class, the proxy class has the foll

6、owing characteristics:(1) dynamically generated by Hibernate at runtimeIt extends the Customer classSo it inherits all the attributes and methods of the Customer class,But its implementation is transparent to the application.(2) when Hibernate creates an instance of a Customer proxy class, only its

7、OID property is initializedAll other attributes are null, so the proxy class instance consumes very little memory.(3) when the application first accesses the instance of the Customer proxy class (for example, calling customer.getXXX () or customer.setXXX () method),Hibernate initializes the instance

8、 of the proxy class and executes the select statement during initialization,Load all data from the Customer object in the database.But theres one exception, that is, when the application accesses the getId () method of the Customer proxy class instanceHibernate does not initialize a proxy class inst

9、anceBecause OID exists when you create a proxy class instance, and you dont have to go to the database to query.Hint: Hibernate uses CGLIB tools to generate persistent classes of proxy classes.CGLIB is a powerful Java bytecode generator,It can dynamically generate extended Java classes or proxy clas

10、ses that implement Java interfaces when the program is running.The following code loads the Customer object through the Sessions load () method, and then accesses its name attribute:TX = session.beginTransaction ();Customer customer= (Customer) session.load (Customer.class, new Long (1);Customer.get

11、Name ();Tmit ();Hibernate does not execute any select statement when running the session.load () method,Just returning an instance of the proxy class of the Customer class, whose OID is 1, which is specified by the second parameter of the load () method.When the application calls the customer.getNam

12、e () method,Hibernate initializes the instance of the Customer proxy class,Load the data from the Customer object from the database and execute the following select statement:Select * from CUSTOMERS where ID=1;Select * from ORDERS where CUSTOMER_ID=1;When the lazy property of the element is true,Var

13、ious runtime behaviors that affect the load () method of Session are illustrated below.1. if the loaded Customer object does not exist in the database, the load () method of Session does not throw an exception,The following exceptions are thrown only when the customer.getName () method is run:ERROR

14、LazyInitializer:63 - Exception initializing proxyNet.sf.hibernate.ObjectNotFoundException: No row with the given identifier exists: 1, of class:Mypack.Customer2. if the application has not visited the Customer object in the entire Session range,So the instances of the Customer proxy class are never

15、initialized,Hibernate doesnt execute any select statement.The following code attempts to access Customer free objects after Session is closed:TX = session.beginTransaction ();Customer customer= (Customer) session.load (Customer.class, new Long (1);Tmit ();Session.close ();Customer.getName ();Because the instance of the Customer proxy class refer

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号