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.