One-to-one fields are defined by the tag <one-to-one> :
<entity name="Engine">
...
</entity>
<entity name="Car">
...
<one-to-one name="engine" ref="Engine"/>
</entity>
In this case, the object because a relation 1: 1 with an object Engine . This relationship is unidirectional in the sense that one can know the engine of a car, but nothing in the engine class allows us to know the car he belongs.
The entity Car contains a reference to Engine :
public class Car extends AuditableModel {
...
@OneToOne(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
private Engine engine;
public Engine getEngine() {
return engine;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
...
}
To create a bidirectional relationship, the slave entity must specify a return (one-to-one) and a field mappedBy attribute, which must reference the field that bears the master side relationship.
<entity name="Engine">
...
<one-to-one name="car" ref="Car" mappedBy="engine"/>
</entity>
<entity name="Car">
...
<one-to-one name="engine" ref="Engine"/>
</entity>