Spring-boot: @Column with name attribute not working property on entities

Created on 11 Dec 2014  Â·  29Comments  Â·  Source: spring-projects/spring-boot

Hi,

I'm using spring-boot-starter-parent and spring-boot-starter-data-jpa (version 1.1.9). I found some strange behaviour on entities. When I specify @Column(name = "myName") then jpa is generating sql query containing "my_name" column (so it changes the name I provide).

Other examples:

  • @Column(name = "aa") -> sql query uses column "aa" -> correct
  • @Column(name = "aA") -> sql query uses column "aa" -> incorrect
  • @Column(name = "aAa") -> sql query uses column "a_aa" -> incorrect

When I put spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy into application.properties, then everything works properly. But I don't think that the initial behaviour is correct.

Greetings,
Kacper86

blocked declined

Most helpful comment

@philwebb it is totally unacceptable that @Column(name = "...") is being ignored, this should always override any naming strategy provided, please re-open this issue

All 29 comments

I faced similar problem where one of our tables is called PurchasedItems. It's an incorrect table name format. However when I specify @Table(name = "PurchasedItems") the generated SQL changed it to _purchased_items_.

As a workaround I specified it as @Table(name = "purchaseditems").

I think the SpringNamingStrategy is doing this. We took the approach so that foreign key columns include the referenced column name. See this question on stackoverflow.com for background.

You can change back to Hibernates ImprovedNamingStrategy by adding this to your application.properties file:

spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy

You could also write your own if needed.

spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy

not working with me on Spring Boot 1.2.2 will check the reason
but why naming-strategy work while there is @Column with name attribute, for me it is bug, as I already mentioned the name of column so the naming-strategy shouldn't apply here, I'll check how to do it and updates you.

I found the issue it is in ImprovedNamingStrategy if you want to solve it without details use DefaultNamingStrategy instead ImprovedNamingStrategy as below

spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.DefaultNamingStrategy

Details:
There are 2 methods in NamingStrategy interface propertyToColumnName and columnName, first one called if you didn't specify name in @Column and 2nd one called if you specify name in @Column.

in ImprovedNamingStrategy both change the name from camel to _ format while in DefaultNamingStrategy it is returning name as it is in 2nd method which is from my point of view the correct behavior.

Thanks for the analysis. The DefaultNamingStrategy doesn't convert to the _ format at all if I remember correctly.

I'm think there will be side-effects if we change the columnName method in SpringNamingStrategy so I'd rather leave this as it is. I think the change should be in the Hibernate ImprovedNamingStrategy class if anywhere, but I imagine that the Hibernate team will also not be keen to change things.

I Already pulled class there and update it waiting for their feedback.

Thanks it worked for me
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.DefaultNamingStrategy

oh, awesome.
spring boot 1.3.3 spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.DefaultNamingStrategy

At spring boot 1.4.1 add those two config will fix this issue:

spring:
  jpa:
    hibernate:
      naming:
        implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

I have some problem with Spring-boot-starter-data-jpa 1.5.2.
None of the solutions,that are written above not working.
Can someone help me with this.

EDIT.
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
helped. problem was gone

@Surendev solution helped (Spring boot 1.5.3)

@philwebb it is totally unacceptable that @Column(name = "...") is being ignored, this should always override any naming strategy provided, please re-open this issue

I'm aware that changing this behaviour now will cause havoc but this issue cost me half an hour till I figured it out what happened to my Column name annotations... totally unexpected

@philwebb and others,

I find there is no key "spring.jpa.hibernate.naming-strategy",and use
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl is OK.

validate on spring boot M2.

For the record, as far as I can tell, there's no way to differentiate between @Column(name="…") and @Column in a naming strategy. Hibernate's Ejb3Column class makes identical calls to the physical naming strategy in both cases:

If anyone who's commented on this issue would like a naming strategy to apply to @Column but not apply to @Column(name="…"), then I would recommend opening a Hibernate issue. If you do so, please comment here with a link to the issue to help others to find it.

Not sure if related or not but I couldn't fix this with the options provided in the Thread.

image

image

@langley-agm the complete solution for Spring Boot in your application configuration:

YAML:

spring:
  jpa:
    hibernate:
      naming:
        implicit-strategy: "org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl"
        physical-strategy: "org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl"

Properties:

spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

@philwebb please re-open this issue as there is strong demand to fix this issue, you can imagine the tons of hours (and money) wasted on this issue alone

@alan-czajkowski Please see my comment above. As far as we know, Hibernate does not support what you are asking for, making it pointless to re-open this issue.

@alan-czajkowski

I did try those but it didn't work, apparently the issue is that Hibernate will only consider either all @Columns in properties or all @Columns in methods, it won't work if you mix them up in a single class.

@alan-czajkowski I'm more than happy to re-open this one if we can find a way to fix it. I've raised HIBERNATE-160 to see if there's anything that the Hibernate team can do for us. Feel free to comment or vote on that issue.

everybody please vote for HIBERNATE-160

Is this issue also affecting @Table(name="...") and @DiscriminatorColumn(name="...") and other naming annotations?

spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

It worked for me, Thankyou

My Problem WITHOUT ImplicitNamingStrategyLegacyJpaImpl property set

  • @Column(name = "aa") -> sql query uses column "aa" -> correct

  • @Column(name = "aA") -> sql query uses column "aa" -> incorrect

  • @Column(name = "aAa") -> sql query uses column "a_aa" -> incorrect

My Problem WITH ImplicitNamingStrategyLegacyJpaImpl property set

  • @Column(name = "aa") -> sql query uses column "aa" -> correct
  • @Column(name = "aA") -> sql query uses column "aa" -> incorrect
  • @Column(name = "aAa") -> sql query uses column "aaa" -> incorrect
  • @Column(name = "[aAa]") -> sql query uses column "aAa" -> correct (my goal)

My solution only worked after this::

set application.properties like this:
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

... and

@Table("[CamelCaseTable]) on entity class

AND

@Column("[camelCaseAttribute]") on entity class attribute

My Conclusions
Only spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl should be sufficient to respect what @Kacper86 expected. I've been debuging ImplicitNamingStrategyLegacyJpaImpl and everything respects the classes and attributes names as it is, without annotation trickies.

Spring Boot 2.2.3.RELEASE
Database: Postgres 12

@langley-agm the complete solution for Spring Boot in your application configuration:

YAML:

spring:
  jpa:
    hibernate:
      naming:
        implicit-strategy: "org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl"
        physical-strategy: "org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl"

Properties:

spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

@philwebb please re-open this issue as there is strong demand to fix this issue, you can imagine the tons of hours (and money) wasted on this issue alone

thanks is work for me

It's a shame this is still a problem in 2020 :(

I ran into this today, trying to map to an existing schema using obvious "incorrect" naming conventions for one of it's columns.
The issue I have with the solutions provided, is that they change the naming strategy at a global level. This now requires me to adjust all my model classes and add @Table/@Column annotations to each and every field to accommodate that change. I just need this one entry to be case sensitive in the way the schema tells me, not the other way around.

To be fair, the whole idea with the @Column name attribute, it that it should be used as an override, and used as is. That's what it is there for, to provide an alternative to any existing naming strategy layer. It is clearly stating: when in doubt, use this.

It's fine if an non-annotated field like "myType" ends up as "my_type". But when I explicitly annotate this entry with @Column("myType"), I am giving clear information on what I really want. Which is not "my_type" obv.

This is why we can't have nice things.

Unfortunately, there's nothing we can do in Boot to fix this without a change in Hibernate. I've added the blocked label to hopefully make that more clear.

If you'd like us to be able to fix this in the future, please take a moment to vote for HIBERNATE-160.

as of this date, there are only 12 votes for this issue: HIBERNATE-160
come on guys and girls, we can do much better than that

voting ensures that issues get top priority and visibility, please take the time to go to that link and vote (create an account on that JIRA if you have to -- it is worth it), please do not be lazy, voting works

spring boot 2.4.0
Hibernate ORM core version 5.4.23.Final
That worked for me

spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
Was this page helpful?
0 / 5 - 0 ratings