Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom JDBC table names naming strategy #80

Closed
dmanix opened this issue Dec 16, 2022 · 3 comments
Closed

Custom JDBC table names naming strategy #80

dmanix opened this issue Dec 16, 2022 · 3 comments

Comments

@dmanix
Copy link

dmanix commented Dec 16, 2022

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
Copy link
Member

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
Copy link
Author

dmanix commented Dec 23, 2022

@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
Copy link
Member

lpandzic commented Jan 2, 2023

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

@lpandzic lpandzic closed this as completed Jan 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants