【问题标题】:Mapping column and table names in EclipseLink在 EclipseLink 中映射列名和表名
【发布时间】:2010-10-05 09:39:20
【问题描述】:

我在 hibernate 中使用了 ImprovementNamingStrategy,将 Java 字段名映射到 MySQL 列名。

ex)birthDate 字段 ->birth_date 列,AccountRole 类 -> account_role 表

我正在测试将 hibernate 代码迁移到 eclipselink 代码。

EclipseLink 中与 hibernamte 的改进命名策略等价的是什么?

【问题讨论】:

    标签: hibernate jpa eclipselink


    【解决方案1】:
    public class MyCustomizer implements SessionCustomizer {
    
        public void customize(Session session) throws Exception {
            Map<Class, ClassDescriptor> descs = session.getDescriptors();
            Collection<ClassDescriptor> descriptors = descs.values();
            for (ClassDescriptor desc : descriptors) {
                updateMappings(desc);
            }
        }
    
        private void updateMappings(ClassDescriptor desc) {
            for (DatabaseMapping mapping : desc.getMappings()) {
                if (mapping.isDirectToFieldMapping()) {
                    DirectToFieldMapping directMapping = (DirectToFieldMapping) mapping;
                    String name = directMapping.getAttributeName();
                    String regex = "([a-z])([A-Z]+)";
                    String replacement = "$1_$2";
                    String newName = name.replaceAll(regex, replacement)
                            .toUpperCase();
                    directMapping.getField().resetQualifiedName(newName);
                }
            }
        }
    }
    

    将属性插入persistence.xml:

      <property name="eclipselink.session.customizer" value="com.test.MyCustomizer" />
    

    【讨论】:

      【解决方案2】:

      我认为没有等价物。

      你在哪里用这个?用于自动映射?您可以使用 JPA 工具(例如 Eclipse Dali)从对象模型生成 JPA orm.xml 或注释,它们可以更好地控制数据模型的生成方式。

      通常使用 JPA 但不是标准命名默认值将不可移植。如果您希望映射使用不同于默认值的映射,则应使用 @Column 注释或 xml。

      【讨论】:

      • 感谢您的回答。目前返回使用 Hibernate。稍后我会再次尝试 EclipseLink。
      【解决方案3】:

      似乎可以通过使用 SessionCustomizer 接口来实现:

      http://dev.eclipse.org/mhonarc/lists/eclipselink-users/msg00094.html

      编辑:不幸的是,在花了一些时间尝试完成这项工作之后,它似乎并不奏效,或者至少不容易。请注意,此链接来自 2008 年。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-22
        • 1970-01-01
        • 1970-01-01
        • 2020-08-10
        相关资源
        最近更新 更多