为什么我的复合键在@ManyToOne 中不起作用?

Why my composite key is not working in @ManyToOne?

我想在 Spring 数据 jpa 中创建共享主键,一切都很好,直到我使用 @ManyToOne 怎么了?

我的实体:

@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;
}
}

结果(如果我使用@OneToOne 就可以):

@OneToOne result

当我m switching @OneToMany theres 不同的 id: @ManyToOne result

仅考虑关联的映射,字段应如下所示:

@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;
  

}

请查看 Hibernate ORM 指南中的 many-to-one mapping 了解更多详细信息。