First, the principle of the use of composition relations :
Minimizing the number of database tables and foreign key references between tables, without causing data redundancy, is a time-consuming operation to establish a connection to multiple tables.
Example: The address attribute in the customer class can be mapped by component, avoiding an additional address table and achieving object-based operations.
Configuration file:
?
1 2 3 4 5 6 7 |
<component name=
"homeAddress" class
=
"mypack.Address"
> <parent name=
"customer" /> <property name=
"province" type=
"string" column=
"HOME_PROVINCE"
/> <property name=
"city" type=
"string" column=
"HOME_CITY"
/> <property name=
"street" type=
"string" column=
"HOME_STREET"
/> <property name=
"zipcode" type=
"string" column=
"HOME_ZIPCODE"
/> </component>
|
Description
?
1 2 3 |
name:设定被映射的持久化类的属性名。 class :设定homeAddress属性的类型。 <parent>:指定Address类所属的整体类。 |
Note: Differentiate whether there are constituent relationships. Hibernate component address has no OID.
1. differentiate between value types and entity types :
Hibernate component address does not have an OID, so you cannot save, update, delete, or load an address object individually through the session. When you use the following methods:
?
1 2 3 4 |
session.load (Address. class new long ( 1 session.save (address); session.update (address); session.delete (address); |
The
throws Org.hibernate.MappingException:Unknow entity class:xx.xxx.Address. The
value type has no OID and cannot be persisted independently, and its life cycle depends on the life cycle of the object that owns the persisted class.
Two, map composite composition Relationship :
A hibernate component can contain other hibernate components and get other entity class associations. The
configuration file is just the same as the one component nested in component.
Three,