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
Comment From: rashidi
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").
Comment From: philwebb
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.
Comment From: bassemZohdy
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.
Comment From: 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.
Comment From: philwebb
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.
Comment From: bassemZohdy
I Already pulled class there and update it waiting for their feedback.
Comment From: mtkuyucu
Thanks it worked for me spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.DefaultNamingStrategy
Comment From: zmskco
oh, awesome.
spring boot 1.3.3 spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.DefaultNamingStrategy
Comment From: jacks808
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
Comment From: Surendev
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
Comment From: orrymr
@Surendev solution helped (Spring boot 1.5.3)
Comment From: alan-czajkowski
@philwebb it is totally unacceptable that @Column(name = "...") is being ignored, this should always override any naming strategy provided, please re-open this issue
Comment From: semteXKG
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
Comment From: alaxcc
@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.
Comment From: wilkinsona
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.
Comment From: langley-agm
Not sure if related or not but I couldn't fix this with the options provided in the Thread.
Comment From: alan-czajkowski
@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
Comment From: wilkinsona
@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.
Comment From: langley-agm
@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.
Comment From: philwebb
@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.
Comment From: alan-czajkowski
everybody please vote for HIBERNATE-160
Comment From: end-user
Is this issue also affecting @Table(name="...") and @DiscriminatorColumn(name="...") and other naming annotations?
Comment From: prakashtjp
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
Comment From: garrydias
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
Comment From: anilkumar-ak
@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
Comment From: djBo
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.
Comment From: wilkinsona
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.
Comment From: alan-czajkowski
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
Comment From: hussienalbared
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
Comment From: LuisMCunha
This too made me lose a lot of hours or scouring the internet, the resolution mentioned in this thread solved the issue, but it's crazy that it happens in the first place.
Comment From: alan-czajkowski
because this issue tracker has been shut down: HIBERNATE-160 I have re-created the issue here: HHH-14584
please go ahead and vote for this issue in the new issue tracker
Comment From: MirosmarOliveira
spring boot V.5.3.23 the same problem, and the same "solution" spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
Comment From: wilkinsona
@MirosmarOliveira That's to be expected. Please vote for https://hibernate.atlassian.net/browse/HHH-14584 is you'd like the situation to change.
Comment From: helderhernandez
Using spring boot and sql server I got the following error:
SQL Error: 0, SQLState: S1093 The Document parameter was not defined for the USP_UpdateDocument stored procedure.
And I had my application.properties configured as follows:
spring.jpa.database-platform=org.hibernate.dialect.SQLServer2012Dialect
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
Solution: Assign EXECUTE permission to the User (with which to connect to the DB) on the stored procedure