EclipseLink自定义表和列命名策略

时间:2013-03-15 20:54:28

标签: jpa eclipselink

有没有办法让EclipseLink将camel case转换为下划线?

例如,class MyEntity => select * from MY_ENTITY

理想情况下,我预先打包的东西可以作为persistence.xml中的属性。

1 个答案:

答案 0 :(得分:9)

您可以编写会话自定义程序来执行此操作。首先,创建一个这样的类:

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);
                }
            }
        }
   }
}

然后,您需要注册此自定义程序。在<properties>部分的persistence.xml中添加此行:

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