4

In my Java code, I have a field named isNegative with a similar column existing in database. But Hibernate insists the name should be is_negative, even with forcing the name with @Column.

@Column(name="isNegative")
private boolean isNegative;

Error:

Caused by: org.hibernate.HibernateException: Missing column: is_negative in datasource.item

Application.properties:

#JPA
spring.data.jpa.repositories.enabled=false
spring.jpa.database=mysql
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.generate-ddl=true
spring.jpa.open-in-view=true
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.hibernate.use-new-id-generator-mappings=false
spring.jpa.properties.hibernate.event.merge.entity_copy_observer=allow
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
3
  • Can you show file hibernate.cfg.xml or application.properties in case you use Hibernate and Spring Framework together, or persistence.xml in case you use Hibernate as a JPA implement? Commented Aug 17, 2018 at 12:57
  • 3
    Possible duplicate of hibernate column name issues Commented Aug 17, 2018 at 12:58
  • @Arnaud Yes, totally; but the thing is the mitigation doesn't work. I set the naming strategy to physicalNamingStrategyStandard; but it doesn't work. The workaround (writing @column(name="isnegative")) works; but it is too much to do so for all my column names. Commented Aug 17, 2018 at 13:20

3 Answers 3

3

That's due to your configuration, because you are setting spring.jpa.hibernate.naming.physical-strategy to PhysicalNamingStrategyStandardImpl which will use underscores for the names.

If you check the Configure Hibernate Naming Strategy section of Spring Docs, you can see that:

Hibernate uses two different naming strategies to map names from the object model to the corresponding database names. The fully qualified class name of the physical and the implicit strategy implementations can be configured by setting the spring.jpa.hibernate.naming.physical-strategy and spring.jpa.hibernate.naming.implicit-strategy properties, respectively. Alternatively, if ImplicitNamingStrategy or PhysicalNamingStrategy beans are available in the application context, Hibernate will be automatically configured to use them.

By default, Spring Boot configures the physical naming strategy with SpringPhysicalNamingStrategy. This implementation provides the same table structure as Hibernate 4: all dots are replaced by underscores and camel casing is replaced by underscores as well. By default, all table names are generated in lower case, but it is possible to override that flag if your schema requires it.

To solve that you need to remove this property and use the default naming strategy instead:

spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.DefaultNamingStrategy
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, what is the solution?
No use spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.DefaultNamingStrategy instead check my Edit.
0

You would need spring.jpa.hibernate.naming.physical-strategy and spring.jpa.hibernate.naming.implicit-strategy

Adding following

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

to application.properties could help. This solution would work from hibernate 5.

Hope it helps.

Comments

0

Please find below my analysis:

If you don't want your naming strategy to add an underscore to the column name or class name, then the strategy that you need to use would look like: spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl. The things that you provide in annotations @Table and @Column’s name attribute would remain as it is. E.g. firstName attribute in entity will get a column name as firstName i.e. No change.

If you don't want to provide annotations and want to manually handle the table name and column names, you should extend the class org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl and override the required methods. If you still use annotations for some of the cases here, remember the overridden methods will apply on the names written in those annotations. spring.jpa.hibernate.naming.physical-strategy=example.CustomStrategy

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.