11

I stumbled upon a strange error today. One of my Java Persistence Application programming interface (JPA) entities in Spring Boot application is not working. I tracked the problem down to a single column:

@javax.persistence.Column(name = "NameWrittenInPascalCase") java.lang.String c; 

When I checked the Structured Query Language (SQL) query which Spring Boot/Hibernate generates I discovered the problem. It seams that Spring Boot or Hibernate converts the NameWrittenInPascalCase into name_written_in_pascal_case (just written in snake case). (In database, of course, my column name is written in PascalCase).

For gods sake, why?

And how to prevent it from doing so?

If you need aditional info, I use Spring Boot version 2.5.7.

4
  • 1
    Have you tried escaping the string with backticks? Like @javax.persistence.Column(name = "`NameWrittenInPascalCase`") Commented Dec 16, 2021 at 17:22
  • No, but it does not help. In query sent to server there is still name_written_in_pascal_case. :( Commented Dec 16, 2021 at 17:46
  • 1
    depending upon ur hibernate version the property name needs to be adjusted in application.properties. refer to this post: stackoverflow.com/questions/29087626/… Commented Dec 16, 2021 at 18:01
  • Oh, thank you. It looks like this question is possible duplicate of the question which you linked. I did not find it when i searched for it before. Commented Dec 20, 2021 at 14:25

1 Answer 1

11

In your project application.properties file set the naming strategy:

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

Default value is org.springframework.boot.orm.jpa.SpringNamingStrategy


UPDATE:

If previous property does not solved your problem, you can use this one (For newer versions of Hibernate):

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl 
Sign up to request clarification or add additional context in comments.

3 Comments

Unfortunately, hibernate still sends name_written_in_pascal_case instead of NameWrittenInPascalCase even after setting this property.
Try this property spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl @KRISTIJANTOMASINI
This fixed it! Thank you very much. Can you please update your answer so that i can accept it and give you reputation?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.