I've an entity like this:
@Entity
public class Person {
@Id
private Long id;
private String firstName;
// Getters and setters
}
The id
is not autogenerated, but decided by the user.
I have a second entity like this:
@Entity
public class PersonDetail {
@Id
@OneToOne
private Long id; // should be referred to id of Person entity
// or maybe private Person person; ???
private String language;
private Integer age;
// Getters and setters
}
Also in the second entity, the id
is not autogenerated.
I would like to make a @OneToOne
relationship, and I would like that the id
of PersonDetail
is the primary key of PersonDetail
but at the same time it must be a foreign key to the Person
entity (id
field).
Is it possible with Spring JPA / Hibernate annotations ?
Thanks a lot, Andrea
You can follow this wiki: Primary Keys through OneToOne and ManyToOne Relationships
Re-write your PersonDetail
entity to this:
@Entity
public class PersonDetail {
@Id
private Long id;
@OneToOne
@PrimaryKeyJoinColumn
private Person person;
private String language;
private Integer age;
// Getters and setters
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With