- Legen Sie ein Entity Car an und fügen Sie folgende Attribute hinzuCreate a new Car entity and add the following attributes:
- registration vom Typ of type Date
- mileage vom Typ of type Integer
- kw vom Typ of type Integer
- price vom Typ of type Double
- Schließen Sie das Tab Car und klicken Sie im folgenden Dialog auf Yes, um das Entity Car zu speichern.
- Legen Sie ein Entity Logbook an.
- Klicken Sie im Project Manager bei Entities auf Car.java und fügen Sie das Entity Car in den Entity-Editor bei Attributes ein.
- Wählen Sie im folgenden Dialog die Option Close the Car tab and click Yes in the following dialog to save the Car entity
- Create a Logbook entity.
- Under Entities in the Project Manager, click Car.java and add the Car entity to the entity editor under Attributes.
- In the following dialog, choose the One to One (1:1) option.
- Übernehmen Sie die Einstellung Apply the setting Bidirectional.
- Klicken Sie auf OKClick OK.Klicken Sie
- auf SpeichernClick Save.
...
Result:
Entity Car - Das Entity Car wird um das Attribut logbook vom Typ Logbook erweitertThe Car entity is enhanced by the logbook attribute of type Logbook.
Code Block language java theme Confluence @OneToOne(mappedBy = "car") public Logbook getLogbook() { return logbook; } public void setLogbook(Logbook logbook) { this.logbook = logbook; }
Entity Logbook - Das Entity Logbook wird um das Attribut car vom Typ Car erweitertThe Logbook entity is enhanced by the car attribute of type Car.
Code Block language java theme Confluence @OneToOne(fetch = FetchType.EAGER) @JoinColumn(name = "car_id") public Car getCar() { return car; } public void setCar(Car car) { this.car = car; }
...
Note:
- Relation im Code per Annotation - Im Code wird die Relation in beiden Entities mit Hilfe der Annotation @OneToOne definiert.Um eine 1:1 Relation korrekt zu löschen, müssen Sie die entsprechenden Attribute in beiden Entities löschenIn the code, the relation is defined in both entities with the annotation @OneToOne.
- To correctly delete a 1:1 relation, you have to delete the corresponding attributes in both entities.