hibernate column name issues
Here is a possible workaround: if you name it dateofbirth
the column in the DB would be named like that, but the attribute name should be the same.
Hibernate takes the camel case format to create/read database columns.
I've had this problem before. I worked with a legacy columns where there was no space in the column names "employeename", "employeerole", "departmentlocation". I hate it because all my beans properties had to be without camel case.
Database columns separated by "_" will be used to properly camelCase as you have just seen.
Hibernate uses wrong column name despite all anotations
There is a student0_
, it's the table alias declared in from student student0_
. It's the column that's not being found since it wasn't escaped properly, so student0_.ID
really is treated as student0_.id
.
Using case sensitive names isn't recommended in Postgres, whereas it's common with other databases. So instead of "First_name"
, just use first_name
. Otherwise you need to escape the names everywhere e.g.
@Column(name = "\"First_Name\"")
and that's not pretty, and all other Postgres users will frown upon your database schema.
HibernateException: Missing column: Wrong name
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
How to prevent Spring Boot/Hibernate from converting entity column names from PascalCase to snake_case?
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
Spring Boot (5) JPA : Column name annotation fails (with set naming convention)
Turns out the context of my application was being bypassed as I was not using all of spring boot and partially creating my own context. Rebuilding the context to work more in line with spring boot made it pick up the proper configuration from applicatiom.yml.
Related Topics
Using Scala Traits with Implemented Methods in Java
Spring Qualifier and Property Placeholder
Exception Thrown Inside Catch Block - Will It Be Caught Again
Java: Finding the Highest Value in an Array
When Does the JPA Set a @Generatedvalue @Id
How to Read Request Body Multiple Times in Spring 'Handlermethodargumentresolver'
How to Use Mockito When We Cannot Pass a Mock Object to an Instance of a Class
Java Interfaces Methodology: Should Every Class Implement an Interface
Cannot Find Firefox Binary in Path. Make Sure Firefox Is Installed
How to Identify End of Inputstream in Java
Getting an Attribute Value in Xml Element
How to Pass Parameters to Anonymous Class