user1880957
user1880957

Reputation: 1236

Spring Snake case conversion not working on foreign key column

I have my entity definition like this:

@ManyToOne
private DomainObject domainObject;

I get this error while running the code:

2017-10-30 14:58:52,517  WARN rnate.engine.jdbc.spi.SqlExceptionHelper: 127 - SQL Error: 1054, SQLState: 42S22
2017-10-30 14:58:52,520 ERROR rnate.engine.jdbc.spi.SqlExceptionHelper: 129 - Unknown column 'domainObject_id' in 'field list'

My table FK column is named domain_object_id.

I have this in my application.yml:

properties:
  hibernate:
    naming:
        strategy: org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy

I was expecting it to generate the SQL as domain_object_id, but it is converting as domainObject_id. What am I doing wrong?

Other non Foreign Key columns are getting converted fine. The issue is only with Foreign Key columns.

Upvotes: 0

Views: 302

Answers (2)

Try using javax.persistence.JoinColumn

@ManyToOne
JoinColumn(name="domain_object_id")
private DomainObject domainObject;

That way you are sure that the column will be named exactly as you want

Upvotes: 1

Rohit Gaikwad
Rohit Gaikwad

Reputation: 3904

It is generating domainObject_id because of this attribute private DomainObject domainObject;

If you are expecting domain_object_id then use, private DomainObject domain_object;. Since the _id will be appended at the end of parameter name.

Upvotes: 0

Related Questions