EclipseLink custom table and column naming strategy

后端 未结 1 351
离开以前
离开以前 2021-01-13 18:18

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

For instance, class MyEntity => select * from MY_ENTITY

相关标签:
1条回答
  • 2021-01-13 18:42

    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" />
    
    0 讨论(0)
提交回复
热议问题