java - 具有主键但也是外键的 JpaRepository 的正确数据类型是什么?
问题描述
我有以下表格,
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;
所以根据上面的代码,我有一些疑问。
这是指示联系人的 id(主键)也是用户的外键的正确方法吗?
我需要使用
@PrimaryKeyJoinColumn
or@JoinColumn
吗?根据上面的代码,我的存储库接口应该是怎样的?
public interface ContactRepository extends JpaRepository<Contact, User> {}
或者
public interface ContactRepository extends JpaRepository<Contact, Integer> {}
解决方案
- 不要使用外键作为主键,如果使用可以
- 加入专栏
- 合同有整数 id,所以
<Contact, Integer>
推荐阅读
- python - 如何进行条件循环和除以零是无效的
- php - Laravel 自定义 Artisan 命令在完成 50% 时停止
- python - Python 在“if”中识别 9 大于 27
从字面上看,我已经进入学习之旅两天了,所以放轻松!我正在尝试组合一个基本的温度应用程序,除了输入个位数外,一切运行良好且没有错误。如果我输入两位数,它会正确记录正确的响应,例如:“是的,太热了”或“是的,太冷了”,但它似乎将任何低于 10 的值识别为大于 32 且不低于 27从而给出“太热情”的回应。
temper
- php - 币安 PHP API
- python - 使用虚拟环境时如何在 Windows cmd 中并行化 python 进程
- php - php & 似乎和 mysql & 不一样
- python - 如何访问python中字符串指定的变量?
- c++ - Boost::multi_index_container 具有不同的键和元素类型
- css - 如何使一个区域线显示在同一图表上的另一个区域之上?
- oracle-sqldeveloper - LISTAGG 函数:“字符串连接的结果太长”