Java @ManyToOne 属性上不允许使用 @Column(s)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4121485/
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
@Column(s) not allowed on a @ManyToOne property
提问by newguy
I have a JPA entity with a property set as
我有一个 JPA 实体的属性设置为
@ManyToOne
@Column(name="LicenseeFK")
private Licensee licensee;
But when I deploy on JBoss 6 the application throws an error saying:
但是当我在 JBoss 6 上部署时,应用程序抛出一个错误说:
org.hibernate.AnnotationException: @Column(s) not allowed on a @ManyToOne property
I use Hibernate 3.5 as the JPA 2.0 implementation.
我使用 Hibernate 3.5 作为 JPA 2.0 实现。
What should I use to reference the foreign key column?
我应该使用什么来引用外键列?
采纳答案by kraftan
Use @JoinColumn
instead of @Column
:
使用@JoinColumn
代替@Column
:
@ManyToOne
@JoinColumn(name="LicenseeFK")
private Licensee licensee;
回答by Vaishali Kulkarni
Using @JoinColumn
and @Column
together will result in the same error.
Change it to only use: @JoinColumn
to fix it.
@JoinColumn
和@Column
一起使用会导致同样的错误。将其更改为仅使用:@JoinColumn
修复它。
回答by Vlad Mihalcea
@Column
@Column
The JPA @Column
annotation is for basic entity attributes, like String
, Integer
, Date
.
JPA@Column
注释用于基本实体属性,例如String
、Integer
、Date
。
So, if the entity attribute name differs than the underlying column name, then you need to use the @Column
annotation to specify the column name explicitly, like this:
因此,如果实体属性名称与基础列名称不同,则需要使用@Column
注释显式指定列名称,如下所示:
@Column(name="created_on")
private LocalDate createdOn;
@JoinColumn
@JoinColumn
The @JoinColumn
annotation is used to customize a Foreign Key column name, and it can only be used with an entity association.
该@JoinColumn
注释用于定制外键列名,并且它只能与实体结合使用。
So, in your case, because you are using a @ManyToOne
association, you need to use @JoinColumn
:
因此,在您的情况下,因为您使用的是@ManyToOne
关联,所以需要使用@JoinColumn
:
@ManyToOne(fetch=FetchTYpe.LAZY)
@JoinColumn(name="LicenseeFK")
private Licensee licensee;
Notice that we set the
fetch
attribute toFetchType.LAZY
because, by default,FetchType.EAGER
is used, and that's a terrible strategy. For more details about whyFetchType.LAZY
is a much better default, check out this article.
请注意,我们将
fetch
属性设置为,FetchType.LAZY
因为默认情况下FetchType.EAGER
会使用,这是一个糟糕的策略。有关为什么FetchType.LAZY
是更好的默认设置的更多详细信息,请查看这篇文章。