hibernate lazy loading principles reproduced great ~ ~

sponsored links
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.
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of hibernate lazy loading principles reproduced great ~ ~

  • rails plugins to do an e-commerce systems

    Phase I: Put the following three plug-ins running rails. spree active_merchant OpenID_authentication_plugin: gem update spree Open source e-commerce applications. checkout down, we need to localize, the adjustment under the css and added so that a sm ...

  • Express instance to use AJAX

    Summary Introduction: AJAX-wide known as "Asynchronous JavaScript and XML" (Asynchronous JavaScript and XML), its core only JavaScript, XMLHTTPRequest and DOM. XMLHTTP is the first application of Microsoft, IE (IE5 and above) by allowing develop

  • hibernate in the get method and the difference between load method

    hibernate in the get method and the load method of the fundamental difference is: If you use the load method, hibernate consider the corresponding object id (database records) are in the database must exist, so it can be assured that the use, it can be as

  • Great Design

    Understand the principle with the practice, details the relationship between thought, we'll go have selectively study the details, we must work on the details because the details are important to you to use the technical details of implementation ...

  • hibernate study of the fifth chapter

    1, oracle database, user is the keyword, if a user table named user will give rise to conflicts. The approach taken in the main have two kinds: ① If you can modify the table name, it is best to change the table name tuser or other name, to avoid causing t

  • Hibernate doubts

    Hibernate has been used recently, encountered in the implementation of inquiry A "Session is closed!" Troublesome, is as follows: org.hibernate.SessionException: Session is closed! at org.hibernate.impl.AbstractSessionImpl.errorIfClosed (Ab ...

  • Object-oriented design principles 11

    What is object-oriented design? Contains what? The benefits of it are what? Required to pay what you do? In today's age, ask these questions seem to be foolish, because this year almost every software developer knows how to use some sort of objec ...

  • Hibernate pessimistic locking mechanism for locking and optimistic locking

    hibernate lock mechanism 1. Pessimistic lock It refers to the modification of data by outsiders hold a conservative attitude. The assumption that at any time access to data, may also have another client to access the same data, in order to maintain t ...

  • Application of spring struts2.0 hibernate HQL

    Therefore, in the development of statistical inquiry system, as far as possible through the use of select statement to write the required query property way back relational data, and avoid using the first query return persistent object (in this way are in

  • hibernate to use the principle of

    The use of hibernate, implementation of data persistence. Has the following several processes. One configuration database connection information. Hibernate.config 2 configuration mapping. 3 use: the use of the process are the following steps: 3.1: Ge ...

blog comments powered by Disqus
Recent Entries
Tag Cloud
Random Entries