【发布时间】:2022-01-23 07:10:57
【问题描述】:
我的 springboot 应用程序中有一对一的映射 JPA 表,它工作正常。
Users 是父表,在account_no 列中,它存储外键。也就是说,孩子的主键。孩子是Account table。
但是,当应用程序启动时,我可以看到在 H2 DB 中创建了一个额外的列 (user_id)。我认为我的 JPA 映射有问题。请帮助弄清楚。下面是两个类。
@Entity
public class User extends AbstractEntity {
// Other fields related to user entity go here ..
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "account_no", referencedColumnName = "account_num")
private Account account;
}
@Entity
public class Account extends AbstractEntity{
// fields like account#, balance etc goes here..
@Column(name="account_num", unique = true)
@NotNull
private long accountNo;
@OneToOne (fetch = FetchType.LAZY)
private User user;
}
启动日志。
create table account (id bigint not null, account_num bigint not null, bal float not null, user_id bigint, primary key (id))
2021-12-22 00:09:28.765 DEBUG 25380 --- [ main] org.hibernate.SQL :
【问题讨论】:
标签: java spring-boot hibernate spring-data-jpa jpa-2.0