正在经历this tutorial on how to implement equals and hashcode对于 JPA 实体,它在 Product
中注释 Company
引用,如下所示:
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "company_id",
nullable = false, updatable = false)
private Company company;
name = "company_id"
部分是必需的吗?
最佳答案
仅定义架构时需要列name
。这不是必需的,因为 JPA API 分配了一个默认名称
(Default only applies if a single join column is used.) The concatenation of the following: the name of the referencing relationship property or field of the referencing entity or embeddable class; ""; the name of the referenced primary key column. If there is no such referencing relationship property or field in the entity, or if the join is for an element collection, the join column name is formed as the concatenation of the following: the name of the entity; ""; the name of the referenced primary key column.
此外,可以说,无论如何在注释中硬编码模式信息都不是一个好习惯,以防它们在部署时需要更改。
关于java - @joinColumn name 属性对于 ManyToOne 关系是必需的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46530122/