6.6.4 Default mapping (bi-directional relationship)
(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 6-6 Default mapping in a bi-directional OneToOne relationship
![[Figure]](figure/zu050600.gif)
(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 6-7 Default mapping in a bi-directional ManyToOne/ OneToMany relationship
![[Figure]](figure/zu050700.gif)
(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