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 ...

  • 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 reverse pr

  • 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 does no

  • 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 part

  • Java basic knowledge of 30 classic Q & A 2010-03-29

    1, object-oriented features of Abstraction: Or slightly abstract is a topic unrelated to the current goal of factors, while giving full consideration to the content. Abstract and can not solve all the problems objectives can only choose one part of the ex

  • java study notes an interview subject 2010-07-17

    JAVA side 1 What are the characteristics of object-oriented aspects of 2 String is the most basic data type? 3 int and Integer What is the difference 4 String and StringBuffer difference 5 run-time exception and the similarities and differences betwe

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



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

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

processed in 0.116 (s). 14 q(s)