- Legen Sie ein Entity Automaker an und fügen Sie ein Attribut company vom Typ String hinzu.
- Schließen Sie das Tab Automaker und klicken Sie im folgenden Dialog auf Yes, um das Entity Automaker zu speichern.
- Legen Sie ein Entity Carmodel an und fügen Sie ein Attribut model vom Typ String hinzu.
- Klicken Sie im Project Manager bei Entities auf Automaker.java und fügen Sie das Entity Automaker in den Entity-Editor bei Attributes ein.
- Wählen Sie im folgenden Dialog die Option Create an Automaker entity and add a company attribute of type String.
- Close the Automaker tab and click Yes in the following dialog to save the Automaker entity.
- Create a Carmodel entity and add a model attribute of type String.
- Under Entities in the Project Manager, click Automaker.java and add the Automaker entity to the entity editor under Attributes.
- In the following dialog, choose the Many to One (n:1) option.
- Übernehmen Sie die Einstellung BidirectionalApply the Bidirectional setting.
- Klicken Sie auf Click OK.Klicken Sie auf
- SpeichernClick Save.
...
Result:
Entity Carmodel - Das Entity Carmodel wird um das Attribut automaker vom Typ Automaker erweitertThe Carmodel entity is enhanced by the automaker entity of type Automaker.
Code Block language java theme Confluence @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "automaker_id") public Automaker getAutomaker() { return automaker; } public void setAutomaker(Automaker automaker) { this.automaker = automaker; }
Entity Automaker - Durch die übernommene Einstellung Bidirectional wird gleichzeitig das Entity Automaker um eine Liste mit allen Carmodels erweitert By applying the setting Bidirectional, the Automaker entity, too, is enhanced with a list of all car models.
Code Block language java theme Confluence @OneToMany(fetch = FetchType.LAZY, mappedBy = "automaker") public List<Carmodel> getCarmodels() { return carmodels; } public void setCarmodels(List<Carmodel> carmodels) { this.carmodels = carmodels; } public Carmodel addCarmodel(Carmodel carmodel) { getCarmodels().add(carmodel); carmodel.setAutomaker(this); return carmodel; } public Carmodel removeCarmodel(Carmodel carmodel) { getCarmodels().remove(carmodel); carmodel.setAutomaker(null); return carmodel; }
Hinweis:
- Relation im Code per Annotation - Im Code wird die Relation mit Hilfe der Annotationen @ManyToOne, bzw. in the code via annotation - In the code, the relation is defined with the annotation @ManyToOne or @OneToMany(mappedBy = "automaker") definiert.
- Plural - Bei For 1:n Relationen wandelt RapidClipse den Namen des Entities auf der n-Seite wegen der besseren Lesbarkeit automatisch in Plural um, z.B. carmodels.Relation löschen - Um eine 1:n Relation korrekt zu löschen, müssen Sie die entsprechenden Attribute in beiden Entities löschenrelations, RapidClipse automatically generates a plural name for the entity on the n-side, e.g. carmodels, to improve the readability.
- Delete relation - To correctly delete a 1:n relation, you have to delete the corresponding attributes in both entities.