Java JPA:@ManyToOne 关系的默认列名映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3964059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 07:41:01  来源:igfitidea点击:

JPA: default column name mapping for @ManyToOne relations

javahibernatejpaopenjpa

提问by Kdeveloper

When we have a class:

当我们有一堂课时:

@Entity
Order implements Serializable {
    @Id
    private Integer id;
    ...
}

and:

和:

@Entity
OrderLine implements Serializable {
    @Id
    private Integer id;

    @ManyToOne
    Order order;
    ...
}

What row namewill the property ordermap to?

属性顺序将映射到哪个行名称

order_id, ORDER_ID or Order_id?

order_id, ORDER_ID or Order_id?

(ommiting the @JoinColumn(name='order_id') is deliberate)

(省略 @JoinColumn(name='order_id') 是故意的)

采纳答案by Pascal Thivent

Here is what the JPA 1.0 specification writes about the JoinColumnannotation:

以下是 JPA 1.0 规范关于JoinColumn注释的内容:

9.1.6 JoinColumn Annotation

...

The nameannotation element defines the name of the foreign key column. The remaining annotation elements (other than referencedColumnName) refer to this column and have the same semantics as for the Column annotation.

If there is a single join column, and if the nameannotation member is missing, the join column name is formed as the concatenation of the following: the name of the referencing relationship property or field of the referencing entity; "_"; the name of the referenced primary key column. If there is no such referencing relationship property or field in the entity (i.e., a join table is used), 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.

...

9.1.6 JoinColumn 注解

...

name注释元素定义了外键列的名称。其余的注释元素(除了referencedColumnName)引用此列并且具有与 Column 注释相同的语义。

如果只有一个连接列,并且name缺少注释成员,则连接列名称由以下内容的串联构成: 引用关系属性或引用实体的字段的名称;"_"; 引用的主键列的名称。如果实体中没有这样的引用关系属性或字段(即使用连接表),则连接列名称由以下内容串联而成: 实体名称;"_"; 引用的主键列的名称。

...

So in your example, the default name of the foreign key column would be order_id.

因此,在您的示例中,外键列的默认名称为order_id.

References

参考

  • JPA 1.0 specification
    • Section 9.1.6 "JoinColumn Annotation"
  • JPA 1.0 规范
    • 第 9.1.6 节“JoinColumn 注释”

回答by Oscar Chan

I might not understand your question. However, don't you need something like below?

我可能不明白你的问题。但是,您不需要像下面这样的东西吗?

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="order_id", nullable=false)
Order order;

here are some examples

这里有些例子