Hibernate Lazy loading not fully realized due to OO
Advertisements
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
Related Posts of Hibernate Lazy loading not fully realized due to OO
-
Some interview questions java
The first is the company give you a chance to meet, it is necessary to know to meet from time to equal the interview, and have a lot of companies to see you at the first time will give you a ready point of doing something trivial, these questions, althoug
-
Hibernate Mapping Types
Hibernate mapping types divided into two categories: built-in mapping types and mapping types of customers. Built-in mapping types is responsible for some common Java types are mapped to the corresponding SQL type; In addition, Hibernate also allows users
-
JAVA Class naming convention
1. Entity Layer: Inheritance: All categories inherited from BasicEntity, one of BasicEntity implementation java.io.Serializable interface; Naming rules: Class Name = Object + type suffix, one of type suffix for Bean, such as: SalesOrderBean 2. Form l ...
-
Hibernate configuration parameters hibernate.hbm2ddl.auto
Hibernate in the configuration file: <properties> <property name="hibernate.hbm2ddl.auto" value="create" /> </ properties> Parameter Description: validate load hibernate, the authentication to create a database t ...
-
Hibernate.cfg.xml configuration file (including the primary key generation strategy Introduction)
Hibernate.cfg.xml configuration file: <? xml version = "1.0" encoding = "utf-8"?> <! DOCTYPE hibernate-configuration PUBLIC "- / / Hibernate / Hibernate Configuration DTD / / EN" "hibernate-configuration-2.0.dtd
-
hibernate to use the principle of
The use of hibernate, implementation of data persistence. Has the following several processes. One configuration database connection information. Hibernate.config 2 configuration mapping. 3 use: the use of the process are the following steps: 3.1: Ge ...
-
Hibernate annotation using notebook
These are the basic common @Entity --Declared an entity bean @Table(name="promotion_info") --For the entity bean mapping for the specified table (Table name ="promotion_info) @Id --Declare that the identifying attribute of the entity bean @GeneratedValue
-
The level Hibernate cache
Hibernate cache level: (1) a cache is very short and the session life cycle consistent, also known as session-level cache-level cache or transaction-level cache (2) Ways of Supporting level cache: get (); load (); iterator (); only entity object cach ...
-
Great collection of java interview topics
1, object-oriented features of what has 1. Abstract: Abstract is that it has overlooked a subject has nothing to do with the current goal of those aspects in order to more fully with the current objectives of the attention-related aspects. Abstract does n












