I want make shared primary key in Spring data jpa, everything is fine untill i`m using @ManyToOne whats wrong?
my entities:
@Entity
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
@OneToOne(mappedBy = "person", cascade = CascadeType.ALL)
private IDCard idCard;
}
@Entity
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
public class IDCard {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@OneToOne(cascade = CascadeType.ALL)
private Person person;
private Long inn;
@JsonIgnore
public Person getPerson() {
return person;
}
}
result (if i`m using @OneToOne it works):
when im switching @OneToMany there
s diffrent id:
@ManyToOne result
CodePudding user response:
Considering only the mapping of the association, the fields should look something like:
@Entity
public class Person {
// This field is optional, it's useful only if you want a bidirectional association
@OneToMany(mappedBy = "person", cascade = CascadeType.ALL)
private List<IDCard> idCards;
}
@Entity
public class IDCard {
@ManyToOne
private Person person;
}
Please, check many-to-one mapping in the Hibernate ORM guide for more details.