Hibernate Lazy loading not fully realized due to OO

2010-06-02  来源:本站原创  分类:Java  人气:268 

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

相关文章
  • Hibernate Lazy loading not fully realized due to OO 2010-06-02

    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

  • hibernate lazy loading principles reproduced great ~ ~ 2008-11-12

    hibernate lazy loading of the principle of When using the load method of query session records in the database, we return a proxy object, rather than the needs of that object; for example, there is a Student table in the database, we have cn.binyulan

  • Discussion hibernate lazy loading 2010-10-01

    Hibernate lazy initialization error is the use of development projects in the most common mistakes. If the configuration of a class or collection of search strategy of delay, it must be when the agent or agent class instance is a collection of persis

  • json-lib hibernate lazy loading problem 2010-10-07

    json-lib hibernate lazy loading problem ------ Question: Using json-lib to hibernate lazy loading of objects into a json when session closed packet error ------ Address: Can use json-lib's JsonConfig filter out unwanted properties, using the apply me

  • hibernate lazy loading issue a temporary solution 2011-05-06

    hibernate lazy loading problems often plague us today, hibernate itself found to provide some solutions Hibernate.initialize (company.getUsers ()); This collection is loaded users But does not support recursive loading it is not guaranteed that the e

  • Hibernate lazy loading mechanism 2009-09-18

    Lazy loading: Lazy loading mechanism is in order to avoid unnecessary performance overhead and put forward the so-called lazy loading is that when data in the real needs when we come to the real implementation of the data load operation. Provided in

  • hibernate lazy loading failed 2010-02-03

    Concerned about the javaeye has been more than a year, but had no opportunity to write a log. Today, they wanted big cow jokes are not afraid of you wrote a log. Want to beginners strtus, hibernate, spring who have some help. The lazy loading, I no l

  • BlazeDS & Hibernate lazy loading in n-tier arhitecture 2010-04-07

    Original link: http://forum.springsource.org/showthread.php?t=71622 Hi, We have faced an issue on lazy loading Domain Objects with Hibernate in n-tier architecture with Spring BlazeDS. Our architecture is just a basic Spring architecture model: - Fle

  • Hibernate lazy loading FOR Class 2010-05-09

    lazy Policy can be used in : * <class> The label : Can the value true/false * <property> The label, you can value true/false, This feature requires class enhancement * <set>/<list> And so the collection, you can evaluate to true/fa

  • Hibernate lazy loading FOR Connection 2010-05-10

    Hibernate Collection property of lazy loading policy : In the collection properties, you can configure the lazy loading policy , A value of :true/false/extra true: The default value, which means that only the collection gets inside the element object

  • Hibernate lazy loading single-ended correlation FOR 2010-05-10

    Hibernate To load policy-Terminal associated lazy : That is, in <one-to-one>/<many-to-one> You can configure on the label Lazy loading policy . Can a value :false/proxy/no-proxy false: Cancel the lazy loading policy, that is, while loading the

  • Hibernate lazy loading 2010-05-10

    What is hibernate lazy load? The so-called lazy loading (lazy) is to delay loading, lazy loading. When to use lazy loading it? Can only be answered when to use lazy loading with lazy loading. Why should lazy load it? When we want to access the data t

  • Hibernate lazy loading in the load method and the principle of 2010-08-29

    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 "2006

  • Detailed hibernate lazy loading problem 2010-10-03

    Lazy loading: Lazy loading mechanism is to avoid unnecessary performance overhead of their argument, the so-called lazy loading is when the really need the data before it will actually perform the data loading operation. Provided in the Hibernate obj

  • Hibernate lazy loading simple solution 2010-10-03

    Recently doing a project with a piece of data persistence layer hibernate. After the previous study used no how, has been in use IBATIS. When the integration of spring security encountered "no session, or session was closed", so look for good so

  • spring + hibernate lazy loading errors integrated solution 2010-11-25

    org.springframework.dao.InvalidDataAccessApiUsageException error org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER / MANUAL): Turn your Session into FlushMode.COMMIT / AUTO

  • The hibernate lazy loading (Lazy Loading) 2011-01-07

    Avoid some cases, association brings unnecessary overhead, Hibernate introduced the concept of lazy loading; Hibernate3, can be recorded for the delay: Entity object Collection (Collection) Lazy property Through the Load method, the target entity obj

  • Spring + Hibernate lazy loading test base class 2009-08-13

    package cn.com.oneslife; import junit.framework.TestCase; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.junit.AfterClass; import org.springframework.context.ApplicationContext; import org.springframework.context.suppor

  • hibernate lazy loading Principle and Realization 2010-03-29

    Can read: http://superleo.javaeye.com/blog/243322

  • hibernate in the pessimistic and optimistic locking lock and lazy loading 2009-04-02

    hibernate frequently used when many individuals of the same data at the same time when changes will happen dirty data, resulting in data inconsistency, the solution can be adopted by the pessimistic and optimistic locking to achieve lock. Pessimistic