I am making a REST api by Spring to retrieve data from database. However I would like to understand how exactly Hibernate maps columns to the POJO?
Column names in the database are like 'FIRST_NAME', 'LAST_NAME'. In the POJO, I make property names as 'firstName' and 'lastName'. However, Hibernate can still map correctly. How does that happen?
Normally , we would use @Column
to explicitly define the column name that is mapped to the a property. If no @Column
is used , it will then use the configured naming strategies to determine which column should a property mapped to.
I am guessing you are using a customised PhysicalNamingStrategy
which map a lowerCamelCase property to the HIGHER_UNDERSCORE column.
You can refer to the docs for more information about the naming strategy behaviour.
As you mention in the comment you are using spring, I believe you are using spring boot too which by default which configure the physical naming strategy to SpringPhysicalNamingStrategy
(docs) :
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.
For example, a TelephoneNumber entity is mapped to the telephone_number table.