java - @joinColumn name 属性对于 ManyToOne 关系是必需的吗?

标签 java hibernate jpa orm

正在经历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/

相关文章:

java - 以编程方式配置 p2 更新存储库

java - 查找已排序的一维数组中元素的最近/最接近的较低值

java - 我什么时候应该使用 EntityManager 分离方法?

java - Hibernate JPA 注解 1 到 0..1 关系

java - 如何从java异常中获取唯一原因

Java JDBC MySQL异常: “Operation not allowed after ResultSet closed” with Web Page read

java - Hibernate错误org.hibernate.exception.SQLGrammarException : Table . ..不存在

java - 如何通过加入来持久化实体?

java - 将 hibernate-validator 与 wicket 集成

java - AWS (Elastic Beanstalk) + Java (Spring) = 原因 : Error reading from remote server