JPA Hibernate 外键存储错误table

JPA Hibernate foreign key is stored in the wrong table

我有这个基本设置,但我不太明白,为什么我会得到我正在得到的行为

用户

@Entity
@Table(name = "USER")
@UserDefinition 
public class User {
@Id
@SequenceGenerator(name = "userSeq", sequenceName = "ZSEQ_USER_ID", allocationSize = 1, initialValue = 1)
@GeneratedValue(generator = "userSeq")

@Column(name = "id", unique = true)
private Long id;

@OneToOne(cascade = CascadeType.ALL)
private Address address;

@OneToOne(cascade = CascadeType.ALL)
private ActivityForum activityForum;

@OneToMany(mappedBy="user",cascade = {CascadeType.ALL},fetch=FetchType.LAZY )
private List<Phone> phones = new ArrayList<>();

//ctor...
//getter/setter...
}

Activity论坛

@Entity
@Table(name = "Actvity_Forum")
public class ActivityForum {

@Id
@SequenceGenerator(name = "afcSeq", sequenceName = "ZSEQ_af_ID",allocationSize = 1,initialValue = 1)
@GeneratedValue(generator = "afSeq")

@Column(name = "id", unique = true)
private Long id;

@OneToOne(mappedBy = "activityForum")
@JoinColumn(name = "user_id", referencedColumnName = "id")
private User user;

//ctor...
//getter/setter...
}

Phone

@Entity
@Table(name ="PHONE")
public class Phone{
@Id
@SequenceGenerator(name = "phoneSeq", sequenceName = "ZSEQ_phone_ID", allocationSize = 1, initialValue = 1)
@GeneratedValue(generator = "phoneSeq")

@Column(name = "id", unique = true)
private Long id;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name ="user_id", referencedColumnName="id")
private User user;

//ctor...
//getter/setter...
}

这是生成的数据库布局。

我在 src 代码中留下了一些列以使 post 更短。

在我问这个问题之前,我做了一些研究,发现外键存储在关系所有者的 table 中。所有者由@JoinColumn 注释确定。它适用于 Phone 多对一关系。

问题:为什么 Key 存储在用户 table 中。而不是在 Activity table.

我在做什么。 Java 11 Quarkus 长期支持版 Gradle 6.71 MySQL数据库

通过@vincendep 和@crizzis 的评论,我很快发现了我的错误。

Explanation: 定义实体间关系的方向对数据库映射没有影响。它仅定义我们在领域模型中使用该关系的方向。

对于双向关系,我们通常定义:

拥有方 反向或参考侧 @JoinColumn 注释帮助我们指定我们将用于加入实体关联或元素集合的列。另一方面,mappedBy 属性用于定义关系的引用方(非拥有方)

代码更改:

用户

@OneToOne(cascade = CascadeType.ALL, mappedBy = "user")
private ActivityForum activityForum;

活动论坛

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "user_id", referencedColumnName = "id")
private User user;

生成的数据库布局: