i have a spring application but hibernate makes queries to database with table name in…
- User
i have a spring application but hibernate makes queries to database with table name in lowercase which naming strategy should i use to avoid that and that hibernate would use names like specified in @Table annotation?
- Assistant
You can use the following naming strategy to ensure that Hibernate uses the table names as specified in the @Table annotation:
-
Create a new class that implements the NamingStrategy interface provided by Hibernate.
-
Override the two methods in the interface:
a) public String classToTableName(String className) - This method is invoked when Hibernate needs to convert a Java class name to a table name.
b) public String propertyToColumnName(String propertyName) - This method is invoked when Hibernate needs to convert a Java property name to a column name.
-
- User
explain what should i write in application.yaml
- Assistant
If you're using Spring Boot with YAML configuration, you can configure the Hibernate naming strategy by adding the following property to your
application.yamlfile:spring: jpa: hibernate: naming: physical-strategy: <fully-qualified-class-name-of-custom-naming-strategy>Replace "" with the fully qualified class name of your custom naming strategy.
For example, if your class is in the package
com.example.myapp.namingand is namedCustomNamingStrategy, you would replace<fully-qualified-class-name-of-custom-naming-strategy>withcom.example.myapp.naming.CustomNamingStrategy.If you prefer the XML configuration, you can add the following property to your
persistence.xmlorhibernate.cfg.xmlfile:<property name="hibernate.physical_naming_strategy" value="<fully-qualified-class-name-of-custom-naming-strategy>"/>Note that you still need to define the other Hibernate properties for managing your database connection.