hibernate lazy loading principles reproduced great ~ ~

hibernate lazy loading of the principle of

When using the load method of query session records in the database, we return a proxy object, rather than the needs of that object; for example, there is a Student table in the database, we have cn.binyulan.doman.Student areas object, if the query Id value of "200,626,313" and Student stu = (Student) session.load (Student.class, "200626313"), and then we print System.out.println (stu.getClass); get the results cn. binyulan.domain.Student $ $ EnhancerByCGLIB $ $ 5a7cc325, very strange name it, this object is generated by Hibernate to help us, from the name you can see that it is the Student class to enhance the class object, in fact, this is a proxy object, this the object where the Student does not need the data, so if you shut down the session using the stu to obtain information, such as stu.getName (); will appear the following exception: org.hibernate.LazyInitializationException: could not initialize proxy - no Session, which illustrates the stu not we need the data.

stu object is now in the end what is it, it is actually a proxy class that has the ability to query the database when the session does not shut down when the call if we stu.getName () method; then the class would go to query the database and returns the corresponding data. Then we close the session after the Zaiqu use this class there will be no exception now, but if we call stu.getName () just to make the proxy class should go to the database, if people look at our code when two logically think this is a fundamental there would be no use, people can easily comment out a comment on the abnormal again, so hibernate provides a method Hibernate.initialize (stu); so that you can initialize the proxy object.

Do not know if It was noted that not, the above phrase code Student stu = (Student) session.load (Student.class, "200626313") to stu forced into a Student type, someone will ask: is the proxy object is not returned Mody, That how they cast it into the Student had, in fact, the proxy class is a subclass of Student, which has a greater capacity than the parent class (database queries), this class is how did it? In fact, hibernate using asm.jar and cglig-2.1.3.jar, modify the Student class in memory, the byte code, the modified byte-code class files that meet the rules, you can create a proxy object.

Domain objects should not be final, and we should now understand why the bar, if it is final, then not be able to inherit, of course, also can not create proxy object, also can not be achieved lazy loading, and if you do not lazy load , then the domain object design into fianl is also possible.

分类:Java 时间:2008-11-12 人气:258
分享到:
blog comments powered by Disqus

相关文章

  • A session cookie and persistent cookie mechanism to achieve the session (reproduced) 2009-05-22

    A session cookie and persistent cookie mechanism to achieve the session When you first visit a site when the web server will be inserted in response to the first Set-Cookie: PHPSESSID = nj1tvkclp3jh83olcn3191sjq3 (php server), or Set-Cookie JSESSIONI

  • Memcache存储session,修改tomcat源码,实现全站二级域名session共享 2011-05-13

    上篇文章中的方案,在外部显式的使用memcache来替代session,虽然可以达到各个服务器session共享的目的,但是改变了开发人员获取session的方式. 本篇介绍的方法可以在不重构原来代码,不改变代码习惯的情况下,实现session共享的目的. 两种方法各有利弊,第一种方法编码方式改变比较大,开发人员可能不习惯,但是不受服务器类型的影响;第二种方法,针对tomcat服务器,需要修改tomcat源码,但是不需要重构代码,代码中对session的操作还和以前一样. 第二种方式主要做两处改

  • What is a reverse proxy, how to use reverse proxy to improve site performance 2010-10-21

    Often see these days talking about in the blog kangzj agent, the article notes also mentioned reverse proxy, and some students use the absolute path of the accelerated play around ghs not quite understand, then to summarize the knowledge of the rever

  • proxy: Max-Forwards has reached zero - proxy loop? 2010-04-06

    Ssi to include a page directive often error, its source code is like this: <!--# Include virtual = "/ backend / res? ResId = r1" -> <!--# Include virtual = "/ backend / res? ResId = r2" -> ... <!--# Include virtual = &qu

  • Java design patterns with beginner in mind - agent (Proxy) mode of the dynamic proxy (Dynamic Proxy) 2010-12-28

    What is a dynamic proxy? Proxied object (reality theme (RealSubject)) can be dynamically changed at runtime, need to control the interface (abstract topics (Subject)) can be changed at runtime, the control can also dynamically change the way in order

  • Nginx Proxy Cache using Nginx reverse proxy caching 2010-10-15

    If you do not want to lose LAMP platform and want to improve server performance, use Nginx reverse proxy feature on it. Apache as a back-end server processes the PHP process by Nginx front-end processing. Proxy on performance alone does not help, ser

  • Interceptor, was the custom Session object 2010-09-13

    When a custom interceptor is divided into three steps 1. Define interceptor class is important to override the inherited AbstractInterceptro class inside intercept (ActionInvocation actionInvocation) method Also note that the methods which have sessi

  • 100 interview questions (a) 2010-09-29

    1 What are the characteristics of object-oriented aspects of (1). Abstract: Abstract is to ignore the topic has nothing to do with those aspects of the current target in order to more fully attention-related aspects of the current target. Abstract do

  • struts2 + hibernate3 + spring2 study notes 12 (manipulating physical objects) 2010-06-21

    Chapter 13, Hibernate entity object manipulation <br /> Introduction to Language in this chapter, the database operation, the common operation in addition to queries, there are add, modify and delete records. This chapter first object into the hiber

  • java written test questions (change) 2010-08-02

    First, talk about the final, finally, finalize the difference. final statement for the properties, methods and classes, respectively immutable properties, methods can not be covered, the class can not inherit. finally exception handling statement is

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.240 (s). 12 q(s)