Now that we have defined our entities, let's add relationships between them:
- Add the following snippet for relationships into the JDL Studio editor:
relationship OneToOne {
Customer{user} to User
}
The first relationship declared is a unidirectional OneToOne between a Customer entity and the inbuilt User entity:
Customer (1) -----> (1) User
It means the Customer entity knows about the User and is the owner of the relationship but the User doesn't know about the Customer and hence we will not be able to obtain customers from a User. This lets us map customers to the User entity and use that for authorization purposes later ensuring one customer can be mapped only to one system user.
- Add this snippet for relationships into the JDL Studio editor:
relationship ManyToOne {
OrderItem{product} to Product
}
This one declares a unidirectional ManyToOne relationship from OrderItem to Product:
OrderItem (*) -----> (1) Product
It means the OrderItem knows their Product but Product does not know about OrderItem. This keeps the design clean as we don't want to know about orders from products for this use case. In the future, if we want to know the orders made for a product we could make this bi-directional.
- Add the following snippet for relationship into the JDL Studio editor:
relationship OneToMany {
Customer{order} to ProductOrder{customer},
ProductOrder{orderItem} to OrderItem{order},
ProductOrder{invoice} to Invoice{order},
Invoice{shipment} to Shipment{invoice},
ProductCategory{product} to Product{productCategory}
}
This declaration is interesting, as we have multiple OneToMany declarations:
Customer (1) <-----> (*) ProductOrder
ProductOrder (1) <-----> (*) OrderItem
ProductOrder (1) <-----> (*) Invoice
Invoice (1) <-----> (*) Shipment
ProductCategory (1) <-----> (*) Product
They are all bidirectional, meaning both the source entity and destination entity know about each other.
We declare that a Customer can have multiple ProductOrders, ProductOrder can have multiple OrderItems and Invoices, Invoice can have many Shipment, and ProductCategory can have many Products. From the destination entity, the source entities are mapped as ManyToOne.