spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

@Column with name attribute not working property on entities

Open Kacper86 opened this issue 11 years ago • 34 comments

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

Kacper86 avatar Dec 11 '14 21:12 Kacper86

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").

rashidi avatar Dec 12 '14 01:12 rashidi

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.

philwebb avatar Dec 12 '14 05:12 philwebb

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.

bassemZohdy avatar Mar 10 '15 18:03 bassemZohdy

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.

bassemZohdy avatar Mar 10 '15 18:03 bassemZohdy

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.

philwebb avatar Mar 17 '15 05:03 philwebb

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

bassemZohdy avatar Mar 17 '15 10:03 bassemZohdy

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

mtkuyucu avatar May 21 '15 21:05 mtkuyucu

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

zmskco avatar Apr 21 '16 08:04 zmskco

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

jacks808 avatar Jan 20 '17 06:01 jacks808

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 avatar Apr 12 '17 17:04 Surendev

@Surendev solution helped (Spring boot 1.5.3)

orrymr avatar May 10 '17 07:05 orrymr

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

alan-czajkowski avatar Aug 21 '17 22:08 alan-czajkowski

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

semteXKG avatar Sep 25 '17 20:09 semteXKG

@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.

alaxcc avatar Nov 22 '17 03:11 alaxcc

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.

wilkinsona avatar Nov 22 '17 10:11 wilkinsona

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

image

image

langley-agm avatar Apr 03 '18 00:04 langley-agm

@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 avatar Apr 03 '18 02:04 alan-czajkowski

@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.

wilkinsona avatar Apr 03 '18 06:04 wilkinsona

@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.

langley-agm avatar Apr 03 '18 20:04 langley-agm

@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.

philwebb avatar Apr 03 '18 23:04 philwebb