Reputation: 450
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
Upvotes: 4
Views: 1109
Reputation: 123
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
Upvotes: 0
Reputation: 72
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.
Upvotes: 0
Reputation: 32165
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
andspring.jpa.hibernate.naming.implicit-strategy
properties, respectively. Alternatively, ifImplicitNamingStrategy
orPhysicalNamingStrategy
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
Upvotes: 3