wrschneider
wrschneider

Reputation: 18770

EclipseLink custom table and column naming strategy

Is there any way to get EclipseLink to translate camel case to underscores?

For instance, class MyEntity => select * from MY_ENTITY

Ideally something pre-packaged I can just put as a property in persistence.xml.

Upvotes: 4

Views: 2845

Answers (1)

tahagh
tahagh

Reputation: 807

You can write a session customizer to do that. First, create a class like this:

public class MySessionCustomizer implements SessionCustomizer {
    @Override
    public void customize(Session session) throws SQLException {
        for (ClassDescriptor descriptor : session.getDescriptors().values()) {
            //Only change the table name for non-embedable entities with no @Table already
            if (!descriptor.getTables().isEmpty() && descriptor.getAlias().equalsIgnoreCase(descriptor.getTableName())) {
                String tableName = convertToUnderscore(descriptor.getTableName());
                descriptor.setTableName(tableName);
                for (IndexDefinition index : descriptor.getTables().get(0).getIndexes()) {
                    index.setTargetTable(tableName);
                }
            }
        }
   }
}

Then, you need to register this customizer. Add this line in your persistence.xml in <properties> section:

<property name="eclipselink.session.customizer" value="foo.bar.MySessionCustomizer" />

Upvotes: 8

Related Questions