Hibernate lazy loading in the load method and the principle of

sponsored links
When using the session method in load record in the database query, we return a proxy object, not really need that object; for example, there is a Student table in the database, we have cn.binyulan.doman.Student areas object, if the query Id is "200626313" in Student stu = (Student) session.load (Student.class, "200626313"), then we print System.out.println (stu.getClass); the results obtained for the cn. binyulan.domain.Student $ $ EnhancerByCGLIB $ $ 5a7cc325, very strange name it, this object is generated by Hibernate to help us, you can see it from the name is an enhanced Student class class object, in fact, this is a proxy object, this object in the Student did not need the data, so if you closed the session to obtain the use of stu information, such as stu.getId (); will appear the following exception: org.hibernate.LazyInitializationException: could not initialize proxy - no Session, which shows the stu not we need data.

stu object is now in the end is what it is actually a proxy class that has the ability to query the database when the session is not closed when the call if we stu.getId () or stu.getName and other methods; then the class will go check database and returns the corresponding data. Then we go after the closing session there will be no exception to use this class, and yet if we call stu.getId () or stu.getName () just to make the proxy class to search the database to see if other people feel that when our code 2 is simply not logical to use, people can easily comment out a comment on the unusual turn, so hibernate provides a method Hibernate.initialize (stu); so that you can initialize the proxy object of the .

I do not know notes the absence of the above phrase code Student stu = (Student) session.load (Student.class, "200626313") forced into the stu Student type, one may ask: is not a return to the proxy object Mody, That how they cast it into the Student of the fact the proxy class is a subclass of Student, which has a greater capacity than the parent class (database queries), this class is how to generate it? In fact, using the asm.jar hibernate and cglig-2.1.3.jar, modify the Student class in memory, the byte code, the modified class file byte code that they 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 you can not inherit, of course they can not generate proxy objects, lazy loading also can not be achieved, and if you do not lazy load , then the domain object is also designed fianl possible.
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Hibernate lazy loading in the load method and the principle of

  • REST on Rails

    Series, the previous article said that Ruby on Rails is a sudden pop up a framework for the Ruby programming language to serve as a catalyst. With the continued success of the experience of Ruby, developers began to seek their Ruby applications and u ...

  • Ext common problem of summing up

    scripts / ext / resources / css / ext-all.css / / EXT generic CSS, contains all the style (must) scripts / ext / resources / css / icon.css / / custom menu item or other storage page icon scripts / utils / HiTRUST-CMS.css / / old version of the payment sy

  • Hibernate's cache management

    Carried ] [http ://www.cnblogs.com/eflylab/archive/2007/01/11/617276.html Cache is the cache, it is often to improve system performance are the most important means to serve as a reservoir of data and the role of a buffer. Cache for the substantial r ...

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

  • JS menu

    JS menu

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

  • Choose Hibernate or iBATIS has its reasons

    Hibernate features: Hibernate powerful database has nothing to do with good, O / R mapping ability, and if you are very proficient in Hibernate, but also for Hibernate to conduct an appropriate package, then your project will be the entire persistence lay

  • JDBC example of a long time do not have JDBC forgot

    A back-up here to stay. The first: The second:

  • 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