Hibernate Lazy loading not fully realized due to OO

Transfer from: http://www.sulong.info/archives/335

Object and relational database fundamental difference exists between, ORM (object / relational mapping) tried to object mapping to relational database table structure, which simplifies storage object to the database and to recover objects from the database complexity for operation database is as simple as operating targets. Hibernate ORM framework of the estimated number of the most mature, but in dealing with the inheritance relations between classes or have some problems.

Lazy look at the following code and inheritance, presentation of this mapping:

@Entity

@Table(
name =
 "a"
)

@Inheritance(
strategy =
 InheritanceType.SINGLE_TABLE
)

@DiscriminatorColumn(

                name =
 "disc"
,
                columnDefinition =
 "varchar(4)"
,
                discriminatorType =
 DiscriminatorType.STRING
)

public
 abstract
 class
 A {

 @Id
 private
 int
 oid;

 private
 String
 name;

}

@Entity

@DiscriminatorValue(
"b"
)

public
 class
 B extends
 A {

private
 String
 age;

}

@Entity

@DiscriminatorValue(
"c"
)

public
 class
 C extends
 B {

private
 String
 sex;

}

@Entity

@Table(
name =
 "d"
)

public
 class
 D {

  @Id
  private
 int
 oid;

  @ManyToOne(
fetch =
 FetchType.LAZY
)

  private
 A a;

  public
 A getA(
)
 {
return
 a}

}

When the query D class should be loaded for the Da is the delay, there is no check-off a table, naturally do not know the record why the value of the corresponding disc and do not know what kind of subclass A specific type. But check out the d object, if da! = Null da must have a value. Hibernate will generate a subclass of A, and use it as an example of da da if you really want to access the method or domain, hibernate only true to query a table. This is actually hibernate lazy loading to achieve the principle. But in this case, if you execute the following code will encounter ClassCastException,

D d =
 session.load
(
D.class
, 1
)
;

C c =
 (
C)
 d.getA
(
)
;
 // Even if  d.getA() Are you sure you are the type C  , Will  ClassCastException

hibernate generated is a subclass of A, not B or C sub-class, so when the wrong type conversion. To solve this problem, or do not use delay loading, but the performance of the above is likely to go wrong; or not to use inheritance to the following:

@Entity

@Table(
name =
 "a"
)

public
 abstract
 class
 A {

 @Id
 private
 int
 oid;

 private
 String
 name;

 private
 String
 age;

 private
 String
 sex;

}

@Entity

@Table(
name =
 "d"
)

public
 class
 D {

  @Id
  private
 int
 oid;

  @ManyToOne(
fetch =
 FetchType.LAZY
)

  private
 A a;

  public
 A getA(
)
 {
return
 a}

}

This effect is to limit the ability of OO; or re-check each time to change the object displayed to this:

D d =
 session.load
(
D.class
, 1
)
;

C c =
 session.load
(
C.class
, d.getA
(
)
.getOid
(
)
)
 // Back up, type specified

This is neither convenient, not OO.

It seems to want to hibernate mapping classes to maintain the characteristics of an object, or some difficulty. In many cases, these mapping classes eventually became the only responsible for mapping function in a database table objects

分类:Java 时间:2010-06-02 人气:267
分享到:
blog comments powered by Disqus

相关文章

  • 2009-12-07 Chuan Chi podcast database - orcale table joins and subqueries (Reprinted) 2010-04-09

    Database of more boring, I mean do not use the database database. Always use the SQL statement then run around, trying to explain the database very interesting, really is not an easy task. Only in PLSQL in the preparation of SQL statements, then call

  • About how to achieve Oracle database partition table 2010-04-15

    About how to achieve Oracle database partition table Oracle database Oracle database partition is a performance optimization as an important means and methods to do the project before hand, just listen too zone name, I feel especially mysterious, see

  • (Transfer) Oracle database operation methods partition table (for a single table or a large amount of data T level database) 2010-05-22

    Abstract: In a large number of business data processing projects, consider using the partition table to improve the performance of application systems and facilitate data management, This paper describes the use of the partition table. In the large-s

  • How Oracle Database partition table 2010-06-09

    Article Transfer from: http://xu20cn.blog.51cto.com/274020/90342 Abstract: In a large number of business data processing projects, consider using the system partition table to improve application performance and facilitate data management, This paper

  • DB2 create database and table space 2010-07-22

    Physical data independence: DB2 using a logical storage model and a physical storage model to process data, user data stored in the table, but it did not know that the physical data. Table is placed in table space, table space physics on it correspon

  • Query the database and table structure (sql server) 2010-11-02

    - Displays the name of all databases select name from master.dbo.sysdatabases; If you want to view the user to create a database sql statement in the above followed where dbid> (greater than) 6; - Displays the current name of all tables in the databa

  • [SQL Server] database size, table size and number of rows Statistics 2011-01-04

    Quote - A table space statistics, the number of rows of information EXEC Your_DB.dbo.sp_spaceused 'dbo.t_log'; - Statistics more than one table space than the size of the database, the number of rows of information create table tmp_table_space (table

  • Oracle Database Notes - Table space 2011-01-07

    In the previous study and Oracle database infrastructure, it is already understand Oracle's memory structure, logic, Oracle's data stored in the tablespaces, the physically stored in the datafiles in. A tablespace can only belong to a database (a dat

  • oracle database create table space, user, authorization, import and export dmp 2011-07-29

    / / Create a temporary table space create temporary tablespace zfmi_temp tempfile 'D: \ oracle \ oradata \ zfmi \ zfmi_temp.dbf' size 32m autoextend on next 32m maxsize 2048m extent management local; / / Tempfile parameters must be / / Create a data

  • Relational database dictionary table Select queries 2011-03-15

    insert into employee (id, position, xueli, emplyee_name) values ​​(1,1,4, 'Yang Zhongyu'); That the employee is a high school education is the project manager position How can he find out personal information? select a.emplyee_name, b.item_value, c.i

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

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

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

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