9.6.4 Default mapping (bi-directional relationship)
This section describes the default mapping of a bi-directional relationship.
- Organization of this subsection
(1) Bi-directional OneToOne relationship
This section describes the default mapping of a bi-directional OneToOne relationship applied in the following conditions:
- Conditions
-
-
Entity A references the stand-alone instance of entity B and sets @OneToOne.
-
Entity B references the stand-alone instance of entity A and sets @OneToOne. The persistence property (or field) name that references entity B using entity A is specified in the mappedBy attribute of @OneToOne.
-
Entity A is the relationship owner.
-
- Applied default mapping
-
-
Entity A is mapped to table A.
-
Entity B is mapped to table B.
-
Table A must have the external key for table B. The name of the external key string is as follows:
Name of the external key string
Name of the relationship property (or field) of entity A_ Name of the primary key string of table B
Note: Italics indicate a variable value.
Also, the external key string has the same type as the primary key of table B and is a unique key constraint.
Figure 9‒6: Default mapping in a bi-directional OneToOne relationship -
(2) Bi-directional ManyToOne/ OneToMany relationship
This section describes the default mapping of a bi-directional ManyToOne/OneToMany relationship applied in the following conditions:
- Conditions
-
-
Entity A references the stand-alone instance of entity B and sets @ManyToOne (or the corresponding XML tags in the O/R mapping file).
-
Entity B references the entity A collection and sets @OneToMany (or the corresponding XML tags in the O/R mapping file). The mappedBy attribute is specified in @OneToMany. The mappedBy attribute specifies the persistent property (or field) name set for referencing entity B with entity A.
-
Entity A is the relationship owner.
-
- Applied default mapping
-
-
Entity A is mapped to table A.
-
Entity B is mapped to table B.
-
Table A must have the external key for table B. The name of the external key string is as follows:
Name of the external key string
Name of the relationship property (or field) of entity A_ Name of the primary key string of table B
Note: Italics indicate a variable value.
The external key string has the same type as the primary key of table B.
Figure 9‒7: Default mapping in a bi-directional ManyToOne/ OneToMany relationship -
(3) Bi-directional ManyToMany relationship
This section describes the default mapping of a bi-directional ManyToMany relationship applied in the following conditions:
- Conditions
-
-
Entity A references the entity B collection. @ManyToMany (or the corresponding XML element in the O/R mapping file) is set in the collection.
-
Entity B references the entity A collection. @ManyToMany (or the corresponding XML tags in the O/R mapping file) is set in the collection and the mappedBy attribute is specified. The mappedBy attribute specifies the persistent property (or field) name set for referencing entity B with entity A.
-
Entity A is the relationship owner.
-
- Applied default mapping
-
-
Entity A is mapped to table A.
-
Entity B is mapped to table B.
-
Apart from table A and B, a junction table named A_B where the name of the owner table appears at the beginning, is necessary. This junction table has two external key strings.
The first external key string references table A and has the same type as the primary key of table A. The name of this external key string is as follows:
Name of the external key string
Name of the relationship property (or field) of entity B_ Name of the primary key of table A
The other external key string references table B and has the same type as the primary key of table B. The name of this external key string is as follows:
Name of the external key string
Name of the relationship property (or field) of entity A_ Name of the primary key of table B
Note: Italics indicate a variable value.
Figure 9‒8: Default mapping in a bi-directional ManyToMany relationship
-