Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 796
wrschneider Avatar asked Feb 18 '23 03:02

wrschneider


1 Answers

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" />
like image 125
tahagh Avatar answered Feb 24 '23 03:02

tahagh