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 人气:314
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

  • 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

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

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



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

CodeWeblog.com 版权所有 闽ICP备15018612号

processed in 0.043 (s). 13 q(s)