Using auto naming policies
As you know, ORMAN can generate your tables and columns automatically using your @Entity
class structure. While generating physical table/column names, framework follows some naming policies for
- Table name policy
- Column name policy
- Index name policy
A naming policy is holded in PhysicalNamingPolicy
object. Let's say you want to make your table names pluralized and uppercased. You need to create a naming policy and set it to the mapping configuration as follows:
PhysicalNamingPolicy tableNamePolicy = new PhysicalNamingPolicy().pluralize(true).uppercase(true);
MappingSession.getConfiguration().setTableNamePolicy(tableNamePolicy);
Let's say you want your column names without underscores and camel-cased:
PhysicalNamingPolicy columnNamePolicy = new PhysicalNamingPolicy().camelCase(true).underscore(false);
MappingSession.getConfiguration().setColumnNamePolicy(columnNamePolicy);
Most of the time, DBMSes are case-insensitive in terms of database schema. However underscore policy can be useful for readability.
If you want to set a policy for table, column and index name generation, you can use MappingConfiguration#setNamePolicy(PhysicalNamingPolicy)
method.
As said before, you don't have to set naming policies manually, because the framework has default as follows:
binding | uppercase? | pluralized? | underscore? |
table name | false | false | true |
column name | false | false | true |
index name | false | false | true |
Please note that
camelCase(boolean)
method overridesuppercase()
andlowercase()
inPhysicalNamingPolicy
.
Here are some examples for a field named dateOfBirth
:
- Underscore:
date_of_birth
- Underscore, uppercase:
DATE_OF_BIRTH
- Camel-case, underscore:
Date_Of_Birth
- Camel-case:
DateOfBirth
- Underscore, pluralized:
date_of_births