infobip / infobip-spring-data-querydsl

Infobip Spring Data Querydsl provides new functionality that enables the user to leverage the full power of Querydsl API on top of Spring Data repository infrastructure.
Apache License 2.0
278 stars 57 forks source link

Custom JDBC table names naming strategy #80

Closed dmanix closed 1 year ago

dmanix commented 1 year ago

In my Spring Data JDBC project I have custom NamingStrategy (predefined prefix is added to entity name):

private static final String TABLE_NAME_PREFIX = "foo_";

@Bean
    public NamingStrategy namingStrategy() {
        return new NamingStrategy() {

            @Override
            public String getTableName(Class<?> type) {

                Assert.notNull(type, "Type must not be null");
                return TABLE_NAME_PREFIX + ParsingUtils.reconcatenateCamelCase(type.getSimpleName(), "_");
            }
        };
    }

I want to have the same table names generated by AnnotationProcessor in Q-classes. What is recommended way to adjust it in AnnotationProcessor?

It seems that enough should be: 1) writing my own ExtendedTypeFactory implementation which extends CustomExtendedTypeFactory and overrides one method: getTableNameEntityType model). 2) overriding createTypeFactory method of SpringDataJdbcAnnotationProcessorBase and use implementation of ExtendedTypeFactory (created in first step) instead of CustomExtendedTypeFactory

But it turns out it is impossible due to CustomExtendedTypeFactory class visibility issue. What is the reason this class is not an public class?

lpandzic commented 1 year ago

Have you tried steps described here? By creating a custom NamingStrategy in steps described above and creating a custom annotation processor with the naming strategy you should be able to modify this behavior.

dmanix commented 1 year ago

@lpandzic yes I was trying this, but I can't do what I need. As I understand NamingStrategy interface allows to customize generated Q-classes (e.g. change default alias table name, etc.). But I need to change table real name (from database). If I understand correctly annotaion processor code, defining table name is done in function getTableName from CustomExtendedTypeFactory class:

    protected String getTableName(EntityType model) {
        var simpleName = model.getSimpleName();
        var className = model.getPackageName() + "." + simpleName;
        var tableName = CaseFormat.UPPER_CAMEL.to(tableCaseFormat, simpleName);
        return Optional.ofNullable(elements.getTypeElement(className)
                                           .getAnnotation(Table.class))
                       .map(this::getTableName)
                       .orElse(tableName);
    }

NamingStrategy class is not used here, so I'd like to redefine this getTableName method in my subclass, but I encounter problems described in my earlier comment.

lpandzic commented 1 year ago

You are right, I've reverted to original proposal to make CustomExtendedTypeFactory public. Try it out with 8.1.0