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 人气:265
分享到:
blog comments powered by Disqus

相关文章

  • Database sub-table deal with design 2010-08-04

    I. Introduction Sub-table is a comparison of speculation at present is relatively popular concept, especially in the case of large loads, sub-table is a good a good way to spread the pressure database. First of all, to understand why the points table, wha

  • Detailed database multi-table join queries 2011-10-06

    From: http://blog.csdn.net/flyingbox/article/details/623189 Operators can achieve by connecting multiple table query. Connections are the main features of the relational database model, is it different from other types of database management system,

  • 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, vie

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

  • Advanced Query Mysql database multi-table query, together query 2010-06-08

    Recently to be a PHP + MYSQL project, to be honest, for the first time it Continue to find information, think about some of the information collection should be Mysql Database Advanced Search The query in the EMP table ,emp_name All the information f

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

  • Database sub-table Opinions 2010-06-13

    Opinions from the database sub-table talk about building a house When I was young, my father, uncle, uncle all lived in the ancestral house, and then my dad and uncle built the house in the village have come out. This is a very natural thing, because the

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

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

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

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

processed in 0.395 (s). 14 q(s)