drypot
drypot

Reputation: 667

Mapping column and table names in EclipseLink

I used ImprovedNamingStrategy in hibernate, to mapping Java field name to MySQL column name.

ex) birthDate field -> birth_date column, AccountRole class -> account_role table

I'm doing test migrating hibernate code to eclipselink code.

What is the equivalent in EclipseLink to hibernamte's ImprovedNamingStrategy ?

Upvotes: 3

Views: 1196

Answers (3)

Flavio Barisi
Flavio Barisi

Reputation: 470

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

Insert into persistence.xml the property:

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

Upvotes: 4

Emmanuel Touzery
Emmanuel Touzery

Reputation: 9173

It appears possible through the use of the SessionCustomizer interface:

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

EDIT: unfortunately after spending a bit of time trying to make this work, it does not seem to work, or at least not easily. Note that this link was from 2008.

Upvotes: 0

James
James

Reputation: 18379

I don't think there is an equivalent.

Where are you using this? For auto-mapping? You could use a JPA tool (such as Eclipse Dali) that will generate the JPA orm.xml or annotations from an object model, they may give more control over how the data model is generated.

In general using JPA but not the standard naming default will not be portable. If you want a mapping to use something different than the default, you should use the @Column annotation or xml.

Upvotes: 2

Related Questions