Hibernate lazy loading in the load method and the principle of

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.

分类:Java 时间:2010-08-29 人气:191
分享到:
blog comments powered by Disqus

相关文章

  • working principle of the oracle database query 2011-09-29

    http://database.ctocio.com.cn/tips/141/8244141.shtml [IT News Writer If we now use the Select statement to query data from the database, Oracle database, how does it work? From which we can comprehend to what? Here, we combine a simple select stateme

  • Using C # implementing distributed database query 2010-06-05

    In this paper, the . Net environments, a new development language C # ADO.Net data access model to integrate the development of distributed database systems which dramatically simplifies the development process ... ... With the traditional database,

  • Massive SQL server database query optimization and paging algorithm (a) 2010-11-03

    Massive Database query optimization and paging algorithm program (1) With the "Golden Shield Project" gradual and rapid development of public security information technology, public security computer application systems are widely used in variou

  • Oracle database query optimization of large tables 2010-12-23

    Key words: massive data; Oracle database; query optimization; data query 1 Introduction <br /> ground in the helicopter flight data processing platform, the need to query the historical flight data for flight simulation and flight accident analysis

  • Oracle database query optimization in a large study table 2010-12-23

    Keywords: mass data; Oracle database; query optimization; data query 1 Introduction <br /> ground in a helicopter flight data processing platform, the need to query the historical flight data for flight simulation and flight accident analysis to the

  • ORACLE database query language SQL language structure (a) 2010-12-30

    ORACLE database query language SQL language structure (a) First, the concept description: Database DATABASE, table TABLE, column COLUMN, line ROW, keyword PRIMARY KEY, index INDEX Second, the type of the column: the character CHAR and VARCHAR2, value

  • Report Template JasperReports database query sons 2010-03-29

    Report Template JasperReports database query sons Decompression dbreport.rar the file to c: \ test directory Install and open the iReport, and create a report file (see Figure -1) [Fig -1] Select the "c: \ test" directory create a find file (see

  • Database Query Optimization 2010-03-09

    Database system is the core of management information systems, database-based online transaction processing (OLTP) and online analytical processing (OLAP) is a bank, business, government and other departments of the most important computer applicatio

  • High-performance concurrent - Database Query Optimization 2010-03-22

    Database system is the core of management information systems, database-based online transaction processing (OLTP) and online analytical processing (OLAP) is the banks, enterprises, government departments one of the most important computer applicatio

  • Database query optimization techniques, design 2010-04-27

    Application database design analysis of the three major paradigms http://dev.yesky.com/424/2220924.shtml Database Query Optimization http://www.moon-soft.com/download/info/1674.htm On database design skills (I) http://www.moon-soft.com/download/info/

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

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

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

processed in 0.296 (s). 12 q(s)