Say we have 2 classes Driver and Car with the Driver having a many-to-one relationship with the Car as follows.
@Entity
@Table(name = "driver")
public class Driver {
@Id @GeneratedValue
private Long id;
@ManyToOne
private Car car;
...
// getter setter ignored for brevity
}
Is there a way to set the value of car
via post request for example by referencing car
by its id
by just JPA/Hibernate annotations? I'm still sort of new to Spring boot, so I was actually thinking of creating a new attribute Long carId
and then apply @JsonIgnore
to car
, according to https://stackoverflow.com/a/42633336/9324939. Or is there any other suggestion or approach to get what I'm trying to achieve?
PS: In the database, they are already connected by reference.
-- in postgres
...
driver_id BIGINTEGER REFERENCES car (id)
...