Adding relationships between tables
Next, we create a new table called owner
that has a one-to-many relationship with the car
table. In this case, a one-to-many relationship means that the owner can own multiple cars, but a car can only have one owner. The following Unified Modeling Language (UML) diagram shows the relationship between the tables:
The following are the steps to create a new table:
- First, we must create the
Owner
entity and repository classes in thecom.packt.cardatabase.domain
package. TheOwner
entity and repository are created in a similar way to theCar
class.
The following is the source code of the Owner
entity class:
// Owner.java
package com.packt.cardatabase.domain;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class Owner {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long ownerid...