具有主键但也是外键的 JpaRepository 的正确数据类型是什么?

What is the correct data type for JpaRepository that has Primary Key but it is also a Foreign Key?

我有以下表格,

User
int id PK

Contact
int id PK, FK to User.id

所以我在Contact.java中的代码如下,

@Getter(value = AccessLevel.NONE)
@Setter(value = AccessLevel.NONE)
@Id
@Column(name = "id", insertable = false, updatable = false)
private Integer id;

@OneToOne(fetch = FetchType.LAZY)
@MapsId
@JoinColumn(name = "id", nullable = false)
private User userId;

在我的 User.java 它有,

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

所以根据上面的代码,我有一些疑问。

  1. 表示 Contact 的 id(主键)也是 User 的外键的正确方法吗?

  2. 我需要使用 @PrimaryKeyJoinColumn 还是 @JoinColumn

  3. 根据上面的代码,我的仓库界面应该是怎样的?

     public interface ContactRepository extends JpaRepository<Contact, User> {}
    

     public interface ContactRepository extends JpaRepository<Contact, Integer> {}
    
  1. 不要使用外键作为主键,如果使用可以
  2. 加入列
  3. 合约有整数 id,所以 <Contact, Integer>