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 @JoinColumninstead of @Column:
使用@JoinColumn代替@Column:
@ManyToOne
@JoinColumn(name="LicenseeFK")
private Licensee licensee;
回答by Vaishali Kulkarni
Using @JoinColumnand @Columntogether will result in the same error.
Change it to only use: @JoinColumnto fix it.
@JoinColumn和@Column一起使用会导致同样的错误。将其更改为仅使用:@JoinColumn修复它。
回答by Vlad Mihalcea
@Column
@Column
The JPA @Columnannotation 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 @Columnannotation to specify the column name explicitly, like this:
因此,如果实体属性名称与基础列名称不同,则需要使用@Column注释显式指定列名称,如下所示:
@Column(name="created_on")
private LocalDate createdOn;
@JoinColumn
@JoinColumn
The @JoinColumnannotation 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 @ManyToOneassociation, you need to use @JoinColumn:
因此,在您的情况下,因为您使用的是@ManyToOne关联,所以需要使用@JoinColumn:
@ManyToOne(fetch=FetchTYpe.LAZY)
@JoinColumn(name="LicenseeFK")
private Licensee licensee;
Notice that we set the
fetchattribute toFetchType.LAZYbecause, by default,FetchType.EAGERis used, and that's a terrible strategy. For more details about whyFetchType.LAZYis a much better default, check out this article.
请注意,我们将
fetch属性设置为,FetchType.LAZY因为默认情况下FetchType.EAGER会使用,这是一个糟糕的策略。有关为什么FetchType.LAZY是更好的默认设置的更多详细信息,请查看这篇文章。

