Mapping column and table names in EclipseLink

I used ImprovedNamingStrategy in hibernate to map the name of a Java field to the name of a MySQL column.

ex) field BirthDate -> column_date column, class AccountRole -> table account_role

I am doing a test migrating hibernate code to eclipselink code.

What is equivalent in EclipseLink for hibernamte ImprovedNamingStrategy?

+3
source share
3 answers

I do not think there is an equivalent.

Where do you use this? For auto-mapping? You can use a JPA tool (like Eclipse Dali) that will generate JPA orm.xml or annotations from the object model, they can give more control over how the data model is created.

JPA, , . , , , @Column xml.

+2
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" />
+4
0

Source: https://habr.com/ru/post/1768034/


All Articles