- Legen Sie ein neues Entity Extra an und fügen Sie ein Attribut extra vom Typ String hinzu.
- Schließen Sie das Tab Extra und klicken Sie im folgenden Dialog auf Yes, um das Entity Extra zu speichern.
- Legen Sie ein neues Entity Car an und fügen Sie folgende Attribute hinzu:
- registration vom Typ Date
- mileage vom Typ Integer
- kw vom Typ Integer
- price vom Typ Double
- Klicken Sie im Project Manager bei Entities auf Extra und fügen Sie das Entity Extra in den Entity-Editor bei Attributes ein.
- Wählen Sie im folgenden Dialog die Option Many to Many (n:m).
- Übernehmen Sie die Einstellung Bidirectional und den vorgeschlagenen Namen carextra für die Join Table, die in der Datenbank für die Abbildung der n:m Relation benötigt wird.
- Klicken Sie auf OK.
- Klicken Sie auf Speichern.
Ergebnis:
...
- Create a new Extra entity and add an extra attribute of type String.
- Close the Extra tab and click Yes in the following dialog to save the Extra entity.
- Create a new Car entity and add the following attributes:
- registration of type Date
- mileage of type Integer
- kw of type Integer
- price of type Double
- Under Entities in the Project Manager click Extra and add the Extra entity to the entity editor under Attributes.
- In the following dialog choose the Many to Many (n:m) option.
- Apply the Bidirectional setting and the proposed name carextra for the join table, which is required in the database for mapping the n: m relation.
- Click OK.
- Click Save.
Result:
Entity Car - The Car entity is enhanced with the extras attribute of type Set.
Code Block language java theme Confluence @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "carextra", joinColumns = @JoinColumn(name = "car_id", referencedColumnName = "id", nullable = false, updatable = false), inverseJoinColumns = @JoinColumn(name = "extra_id", referencedColumnName = "id", nullable = false, updatable = false)) public Set<Extra> getExtras() { return extras; } public void setExtras(Set<Extra> extras) { this.extras = extras; }
Entity Extra - Das Entity Extra wird um das Attribut cars vom Typ Set erweitert The Extra entity is enhanced with the cars entity of type Set.
Code Block language java theme Confluence @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "carextra", joinColumns = @JoinColumn(name = "extra_id", referencedColumnName = "id", nullable = false, updatable = false), inverseJoinColumns = @JoinColumn(name = "car_id", referencedColumnName = "id", nullable = false, updatable = false)) public Set<Car> getCars() { return cars; } public void setCars(Set<Car> cars) { this.cars = cars; }
HinweisNote:
- Relation im Code per Annotation - Im Code wird die Relation in beiden Entities mit Hilfe der Annotation @ManyToMany definiertin the code via annotation - In the code, the relation is defined in both entities with the annotation @ManyToMany.
- Plural - Bei For n:m Relationen erzeugt RapidClipse den Namen des neuen Attributes wegen der besseren Lesbarkeit automatisch in Plural, z.B. cars.Relation löschen - Um eine n:m Relation korrekt zu löschen, müssen Sie die entsprechenden Attribute in beiden Entities löschenrelations, RapidClipse automatically generates a plural name for the new attribute, e.g. Cars, to improve the readability.
- Delete relation - To correctly delete an n:m relation, you have to delete the corresponding attributes in both entities.