Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
  1. Legen Sie ein neues Entity Extra an und fügen Sie ein Attribut extra vom Typ String hinzu.
  2. Schließen Sie das Tab Extra und klicken Sie im folgenden Dialog auf Yes, um das Entity Extra zu speichern.
  3. Legen Sie ein neues Entity Car an und fügen Sie folgende Attribute hinzu:
    1. registration vom Typ Date 
    2. mileage vom Typ Integer
    3. kw vom Typ Integer
    4. price vom Typ Double
  4. Klicken Sie im Project Manager bei Entities auf Extra und fügen Sie das Entity Extra in den Entity-Editor bei Attributes ein.
  5. Wählen Sie im folgenden Dialog die Option Many to Many (n:m).
  6. Ü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.
  7. Klicken Sie auf OK.
  8. Klicken Sie auf Speichern.
    Image Removed
Ergebnis:

...

  1. Create a new Extra entity and add an extra attribute of type String.
  2. Close the Extra tab and click Yes in the following dialog to save the Extra entity.
  3. Create a new Car entity and add the following attributes:
    1. registration of type Date 
    2. mileage of type Integer
    3. kw of type Integer
    4. price of type Double
  4. Under Entities in the Project Manager click Extra and add the Extra entity to the entity editor under Attributes.
  5. In the following dialog choose the Many to Many (n:m) option.
  6. 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.
  7. Click OK.
  8. Click Save.
    Image Added
Result:
  • Entity Car - The Car entity is enhanced with the extras attribute of type Set.



    Code Block
    languagejava
    themeConfluence
    @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
    languagejava
    themeConfluence
    @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.