Let's consider the following:
@Entity
@Table(name="person")
public class Person implements Serializable {
@Id
@GeneratedValue
@Column(name="id")
private int id;
@Column(name="firstName")
private String firstName;
@Column(name="lastName")
private String lastName;
...getters/setters...
}
and
@Entity
@Table(name="car")
public class Car implements Serializable {
@Id
@GeneratedValue
@Column(name="id")
private int id;
@Column(name="brand")
private String brand;
@Column(name="model")
private String model;
@Column(name="personId")
private String personId;
...getters/setters...
}
Let's imagine that a user is going to subscribe and enter his personal info, like first name, last name, the brand of his car, as well as the model of the car.
I do not want the personal info of the person to be stored in the same table than the car info.
I also would like to be able to retrieve the car information with the personId, this is why I have personId in the Car class.
Which annotations should I use to be able to accomplish this? Obviously I will need a constraint on the Car table and make personId a foreign key, right? What is the best way?
I have seen different things, what is the best?