管理关系 Managing relationships

当使用JPA时, entity sub-generator 可以创建实体之间的关系。

介绍 Presentation

关系仅在使用JPA时有效。如果您选择使用 CassandraMongoDB, 它们将不可用。

一个关系在两个实体之间工作,JHipster将生成以下代码:

  • 在生成的实体中管理与JPA的这种关系
  • 创建正确的Liquibase更改日志,以便数据库中存在关系
  • 生成AngularJS前端,以便您可以在用户界面中以图形方式管理此关系

JHipster UML和JDL Studio JHipster UML and JDL Studio

本页介绍如何使用标准命令行界面与JHipster建立关系。如果要创建多个实体和关系,则可能更喜欢使用图形工具。

在这种情况下,有两个选项可用:

  • JHipster UML, 它允许您使用UML编辑器。
  • 我们的在线工具JDL Studio, 使用我们的域名语言创建实体和关系。

You can generate entities with relationships from a JDL file using the import-jdl sub-generator, by running jhipster import-jdl your-jdl-file.jh.

可用关系 Available relationships

当我们使用JPA时,可以使用通常的一对多,多对一,多对多和一对一的关系:

  1. 双向一对多关系 A bidirectional one-to-many relationship
  2. 单向多对一关系 A unidirectional many-to-one relationship
  3. 单向一对多关系 A unidirectional one-to-many relationship
  4. 两个实体之间的一对多关系 Two one-to-many relationships on the same two entities
  5. 多对多的关系 A many-to-many relationship
  6. 一对一的关系 A one-to-one relationship
  7. 单向一对一关系 A unidirectional one-to-one relationship

提示: the User 实体

请注意, User 实体是JHipster生成的具体的实体. 你可以:

  • many-to-one 的关系就像这个实体(a Car can have a many-to-one relationship to a User). 这将在您的新实体存储库中生成特定查询,因此您可以在当前的安全用户上过滤您的实体,这是一个常见的要求。在生成的AngularJS客户端UI上,您将有一个下拉列表Car来选择User

  • many-to-many and one-to-one relationships to the User entity, but the other entity must be the owner of the relationship (a Team can have a many-to-many relationship to User, 但只有团队可以添加/删除用户,而且用户不能添加/删除一个团队). 在AngularJS客户端界面上,您还可以User在多选框中选择。

双向一对多关系 A bidirectional one-to-many relationship

我们从两个实体开始, a Owner and a Car. 车主可以有很多车,一辆车只能有一个车主。

所以一方面这是一个简单的 one-to-many 关系 (one owner has many cars) ,另一方面是 many-to-one 关系 (many cars have one owner):

Owner (1) <-----> (*) Car

我们首先将创造Owner, 以下是JHipster的相关问题 Owner:

jhipster entity Owner
...
Generating relationships with other entities
? Do you want to add a relationship to another entity? Yes
? What is the name of the other entity? Car
? What is the name of the relationship? car
? What is the type of the relationship? one-to-many
? What is the name of this relationship in the other entity? owner

请注意,我们选择了关于关系名称的默认选项。

现在我们可以生成 Car:

jhipster entity Car
...
Generating relationships with other entities
? Do you want to add a relationship to another entity? Yes
? What is the name of the other entity? Owner
? What is the name of the relationship? owner
? What is the type of the relationship? many-to-one
? When you display this relationship with AngularJS, which field from 'Owner' do you want to use? id

使用以下JDL也可以实现相同的

entity Owner
entity Car

relationship OneToMany {
  Owner{car} to Car{owner}
}

就这样,你现在在这两个实体之间有一对多的关系!在生成的AngularJS客户端UI上,您将有一个下拉列表Car 来选择Owner

单向多对一关系

在前面的例子中,我们有一个双向关系: 从一个 Car 实例你可以找到它的所有者, 从一个 Owner 实例你可以得到所有他的汽车。

A many-to-one 单向关系意味者汽车知道他们的主人, 但是相反却不是。

Owner (1) <----- (*) Car

你会这样做的关系有两个原因:

  • 从商业角度来看,您只能以这种方式使用您的实体。所以你不想有一个API,允许开发人员做一些没有意义的事情。
  • 使用Owner实体时,您的性能获得了较小的收益(因为它不需要管理汽车的收集)。

In that case, you would still create the Owner first, this time with no relationship:

jhipster entity Owner
...
Generating relationships with other entities
? Do you want to add a relationship to another entity? No

And then the Car entity, as in the previous example:

jhipster entity Car
...
Generating relationships with other entities
? Do you want to add a relationship to another entity? Yes
? What is the name of the other entity? Owner
? What is the name of the relationship? owner
? What is the type of the relationship? many-to-one
? When you display this relationship with AngularJS, which field from 'Owner' do you want to use? id

这将像以前的例子一样工作,但您将无法从Owner实体添加或删除汽车。在生成的AngularJS客户端UI上,您将有一个下拉列表Car来选择Owner。这是相应的JDL:

entity Owner
entity Car

relationship ManyToOne {
  Car{owner} to Owner
}

单向一对多关系 A unidirectional one-to-many relationship

一对多的单向关系意味着该 Owner 实例可以获取其车辆的收集,但不是相反的。与前面的例子相反。

Owner (1) -----> (*) Car

JHipster目前不提供此类关系,详见 #1569 .

你有两个解决方案::

  • 执行双向映射,并使用它,而无需修改:这是我们推荐的方法,因为它更简单
  • 执行双向映射,然后进行修改,将其转换为单向映射:
    • Remove the "mappedBy" attribute on your @OneToMany annotation
    • Generate the required join table: you can do a mvn liquibase:diff to generate that table, see the documentation about using Liquibase diff

这不是JDL支持的,因为它不在JHipster中。

两个实体之间的一对多关系 Two one-to-many relationships on the same two entities

对于这个例子,一个Person可以是许多汽车的拥有者,他也可以是许多汽车的驱动力:

Person (1) <---owns-----> (*) Car
Person (1) <---drives---> (*) Car

为此,我们需要使用关系名称,我们在前面的例子中留下了它们的默认值。

Generate the Person entity, which has two one-to-many relationships to the Car entity:

jhipster entity Person
...
Generating relationships with other entities
? Do you want to add a relationship to another entity? Yes
? What is the name of the other entity? Car
? What is the name of the relationship? ownedCar
? What is the type of the relationship? one-to-many
? What is the name of this relationship in the other entity? owner
...
Generating relationships with other entities
? Do you want to add a relationship to another entity? Yes
? What is the name of the other entity? Car
? What is the name of the relationship? drivedCar
? What is the type of the relationship? one-to-many
? What is the name of this relationship in the other entity? driver

Generate the Car entity, which use the same relationship name has was configured in the Person entity:

jhipster entity Car
...
Generating relationships with other entities
? Do you want to add a relationship to another entity? Yes
? What is the name of the other entity? Person
? What is the name of the relationship? owner
? What is the type of the relationship? many-to-one
? When you display this relationship with AngularJS, which field from 'Person' do you want to use? id
...
Generating relationships with other entities
? Do you want to add a relationship to another entity? Yes
? What is the name of the other entity? Person
? What is the name of the relationship? driver
? What is the type of the relationship? many-to-one
? When you display this relationship with AngularJS, which field from 'Person' do you want to use? id

使用以下JDL也可以实现相同的

entity Person
entity Car

relationship OneToMany {
  Person{ownedCar} to Car{owner}
}

relationship OneToMany {
  Person{drivedCar} to Car{driver}
}

A Car can now have a driver and a owner, which are both Person entities. On the generated AngularJS client UI you will dropdowns in Car to select a Person for owner field and driver field.

多对多的关系 A many-to-many relationship

Driver可以驾驶很多车,但Car也可以有很多司机。

Driver (*) <-----> (*) Car

在数据库级别,这意味着我们将在表Driver和表Car之间建立一个连接表。 At the database level, this means we will have a join table between the Driver and the Car tables.

对于JPA,这两个实体之一将需要管理关系:在我们的情况下,这将是Car实体,它将负责添加或删除驱动程序。 For JPA, one of those two entities will need to manage the relationship: in our case, that would be the Car entity, which will be responsible to add or remove drivers.

Let us generate the non-owning side of the relationship, the Driver, with a many-to-many relationship:

jhipster entity Driver
...
Generating relationships with other entities
? Do you want to add a relationship to another entity? Yes
? What is the name of the other entity? Car
? What is the name of the relationship? car
? What is the type of the relationship? many-to-many
? Is this entity the owner of the relationship? No
? What is the name of this relationship in the other entity? driver

Then generate the Car, with the owning side of the many-to-many relationship:

jhipster entity Car
...
Generating relationships with other entities
? Do you want to add a relationship to another entity? Yes
? What is the name of the other entity? Driver
? What is the name of the relationship? driver
? What is the type of the relationship? many-to-many
? Is this entity the owner of the relationship? Yes
? When you display this relationship with AngularJS, which field from 'Driver' do you want to use? id

The same can be achieved using the below JDL as well

entity Driver
entity Car

relationship ManyToMany {
  Car{driver} to Driver{car}
}

That's it, you now have a many-to-many relationship between those two entities! On the generated AngularJS client UI you will have a multi-select dropdown in Car to select multiple Driver since Car is the owning side.

一对一的关系 A one-to-one relationship

Following our example, a one-to-one relationship would mean that a Driver can drive only one Car, and a Car can only have one Driver.

Driver (1) <-----> (1) Car

Let us create the non-owning side of the relationship, in our case the Driver:

jhipster entity Driver
...
Generating relationships with other entities
? Do you want to add a relationship to another entity? Yes
? What is the name of the other entity? Car
? What is the name of the relationship? car
? What is the type of the relationship? one-to-one
? Is this entity the owner of the relationship? No
? What is the name of this relationship in the other entity? driver

Then generate the Car, which owns the relationship:

jhipster entity Car
...
Generating relationships with other entities
? Do you want to add a relationship to another entity? Yes
? What is the name of the other entity? Driver
? What is the name of the relationship? driver
? What is the type of the relationship? one-to-one
? Is this entity the owner of the relationship? Yes
? What is the name of this relationship in the other entity? car
? When you display this relationship with AngularJS, which field from 'Driver' do you want to use? id

使用以下JDL也可以实现相同的

entity Driver
entity Car

relationship OneToOne {
  Car{driver} to Driver{car}
}

That's it, you now have a one-to-one relationship between those two entities! On the generated AngularJS client UI you will have a dropdown in Car to select a Driver since Car is the owning side.

单向一对一关系 A unidirectional one-to-one relationship

A unidirectional one-to-one relationship means that the citizen instance can get its passport, but the passport instance can't get to its owner.

Citizen (1) -----> (1) Passport

Generate the Passport entity first, without any relationship to its owner:

jhipster entity Passport
...
Generating relationships with other entities
? Do you want to add a relationship to another entity? No

Then, generate the Citizen entity:

jhipster entity Citizen
...
Generating relationships with other entities
? Do you want to add a relationship to another entity? Yes
? What is the name of the other entity? Passport
? What is the name of the relationship? passport
? What is the type of the relationship? one-to-one
? Is this entity the owner of the relationship? Yes
? What is the name of this relationship in the other entity? citizen
? When you display this relationship with AngularJS, which field from 'Passport' do you want to use? id

After doing this, a Citizen possesses a passport, but no Citizen instance is defined in Passport. On the generated AngularJS client UI you will have a dropdown in Citizen to select a Passport since Citizen is the owning side. This is the corresponding JDL:

entity Citizen
entity Passport

relationship OneToOne {
  Citizen{passport} to Passport
}

results matching ""

    No results matching ""