Naxi
Naxi

Reputation: 2062

@JoinColumn gives error "column with logical name ID not found" in Hibernate 4

I am having below annotation on column in my entity class.

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "INQUIRYID", referencedColumnName="ID", updatable=false, insertable=false)
private MyTable myInquiry; 

It was giving below error on runtime.

column with logical name ID not found in entity class

As the referenced column is a primary key, I removed the referencedColumnName attribute and updated to below

 @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "INQUIRYID", updatable=false, insertable=false)
    private MyTable myInquiry;

This works perfectly with Hibernate 5.3 but as soon as I go to hibernate 4 , I see some anomalies.

In hibernate 5 I get this issue only with columns which are referring some ID(PK) of another class. However, in hibernate 4 I see this error for non-pk columns as well.

I start to get the same error for columns which are referring non primary keys.

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "AB", referencedColumnName = "AB")
private MyTable someData;

Above code gives me error in hibernate 4

Column with logical name AB not found in entity class. 

Here, AB is a non primary key column .So I can't remove referencedColumnName attribute.

Is it purely because of hibernate version or there is a different reason for such behavior ?

Referred this : Similar Issue

Upvotes: 0

Views: 1787

Answers (1)

Vikas
Vikas

Reputation: 7205

There is a bug for hibernate 4.1.7

A workaround for this issue is surrounding the column name with gave accents.

Unable to find column with logical name: id in org.hibernate.mapping.Table(template) and its related supertables and secondary tables

@ManyToOne @JoinColumnsOrFormulas({ @JoinColumnOrFormula(column = @JoinColumn(name = "template", referencedColumnName = "id")),
@JoinColumnOrFormula(formula = @JoinFormula(value = "'custom'", referencedColumnName = "type")) }) This is caused by identifying the column names within the logicalToPhysical map of TableColumnNameBinding. Within this map the column names are surrounded by grave accents (`id`) while the check do a lookup to the map with plain column name (id).

A workaround for this issue is surrounding the column name with gave accents.

@ManyToOne @JoinColumnsOrFormulas({ @JoinColumnOrFormula(column = @JoinColumn(name = "template", referencedColumnName = "`id`")),
@JoinColumnOrFormula(formula = @JoinFormula(value = "'custom'", referencedColumnName = "`type`")) })

Upvotes: 1

Related Questions