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
JPA: default column name mapping for @ManyToOne relations
提问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 JoinColumn
annotation:
以下是 JPA 1.0 规范关于JoinColumn
注释的内容:
9.1.6 JoinColumn Annotation
...
The
name
annotation element defines the name of the foreign key column. The remaining annotation elements (other thanreferencedColumnName
) refer to this column and have the same semantics as for the Column annotation.If there is a single join column, and if the
name
annotation 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;