@Column(s) not allowed on a @ManyToOne property

JavaHibernateJpaJbossAnnotations

Java Problem Overview


I have a JPA entity with a property set as

@ManyToOne
@Column(name="LicenseeFK")
private Licensee licensee;

But when I deploy on JBoss 6 the application throws an error saying:

org.hibernate.AnnotationException: @Column(s) not allowed on a @ManyToOne property

I use Hibernate 3.5 as the JPA 2.0 implementation.

What should I use to reference the foreign key column?

Java Solutions


Solution 1 - Java

Use @JoinColumn instead of @Column:

@ManyToOne
@JoinColumn(name="LicenseeFK")
private Licensee licensee;

Solution 2 - Java

Using @JoinColumn and @Column together will result in the same error. Change it to only use: @JoinColumn to fix it.

Solution 3 - Java

@Column

The JPA @Column annotation is for basic entity attributes, like String, Integer, Date.

So, if the entity attribute name differs from the underlying column name, then you need to use the @Column annotation to specify the column name explicitly, like this:

@Column(name="created_on")
private LocalDate createdOn;

@JoinColumn

The @JoinColumn annotation is used to customize a Foreign Key column name, and it can only be used with an entity association.

So, in your case, because you are using a @ManyToOne association, you need to use @JoinColumn:

@ManyToOne(fetch=FetchTYpe.LAZY)
@JoinColumn(name="LicenseeFK")
private Licensee licensee;

Notice that we set the fetch attribute to FetchType.LAZY because, by default, FetchType.EAGER is used, and that's a terrible strategy.

Solution 4 - Java

In my case @VaishaliKulkarni's answer was helpful to identify the problem.

I missed to write field for @Column annotion and it effected on next field.

@Column(name = "account_id")
// I forgot to write field here

@ManyToOne
@JoinColumn(name = "customer_id")
private Customer customer;

So, I was getting exception at "customer" field.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnewguyView Question on Stackoverflow
Solution 1 - JavakraftanView Answer on Stackoverflow
Solution 2 - JavaVaishali KulkarniView Answer on Stackoverflow
Solution 3 - JavaVlad MihalceaView Answer on Stackoverflow
Solution 4 - JavaSaibaba GunturiView Answer on Stackoverflow