Skip to content

Query derivation not working for simple domain primitive with Reading/WritingConverter #2059

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

Open
manosbatsis opened this issue May 16, 2025 · 1 comment
Assignees
Labels
type: bug A general bug

Comments

@manosbatsis
Copy link

manosbatsis commented May 16, 2025

It seems data-jdbc query derivation does not work for simple domain primitives accompanied by Reading/WritingConverters, e.g. something like

public record CustomerRef(String value) {

    /** A Spring Data reading converter for {@link CustomerRef} */
    @ReadingConverter
    public class StringToCustomerRefConverter implements Converter<String, CustomerRef> {
        @Override
        public CustomerRef convert(String source) {
            return new CustomerRef(source);
        }
    }

    /** A Spring Data writing converter for {@link String} */
    @WritingConverter
    public class CustomerRefToStringConverter implements Converter<CustomerRef, String> {
        @Override
        public String convert(CustomerRef source) {
            return source.value();
        }
    }
}

Trying to use the above like:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Customer {
    @Id
    private UUID id;
    @Valid
    private CustomerRef ref;
    private String name;
}

@Repository
public interface CustomerRepository extends CrudRepository<Customer, UUID> {

    Optional<Customer> findOneByRef(CustomerRef ref);
}

results to

    Caused by: org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.Optional com.github.manosbatsis.primitive4j.sample.mvcjdbc.customer.CustomerRepository.findOneByRef(com.github.manosbatsis.primitive4j.sample.mvcjdbc.customer.CustomerRef); Reason: Cannot query by nested entity: ref
        at org.springframework.data.repository.query.QueryCreationException.create(QueryCreationException.java:101) ~[spring-data-commons-3.4.3.jar:3.4.3]
        at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:120) ~[spring-data-commons-3.4.3.jar:3.4.3]
        at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.mapMethodsToQuery(QueryExecutorMethodInterceptor.java:104) ~[spring-data-commons-3.4.3.jar:3.4.3]
        at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lambda$new$0(QueryExecutorMethodInterceptor.java:92) ~[spring-data-commons-3.4.3.jar:3.4.3]
        at java.base/java.util.Optional.map(Optional.java:260) ~[na:na]
        at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.<init>(QueryExecutorMethodInterceptor.java:92) ~[spring-data-commons-3.4.3.jar:3.4.3]
        at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:440) ~[spring-data-commons-3.4.3.jar:3.4.3]
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$4(RepositoryFactoryBeanSupport.java:350) ~[spring-data-commons-3.4.3.jar:3.4.3]
        at org.springframework.data.util.Lazy.getNullable(Lazy.java:135) ~[spring-data-commons-3.4.3.jar:3.4.3]
        at org.springframework.data.util.Lazy.get(Lazy.java:113) ~[spring-data-commons-3.4.3.jar:3.4.3]
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:356) ~[spring-data-commons-3.4.3.jar:3.4.3]
        at org.springframework.data.jdbc.repository.support.JdbcRepositoryFactoryBean.afterPropertiesSet(JdbcRepositoryFactoryBean.java:198) ~[spring-data-jdbc-3.4.3.jar:3.4.3]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1859) ~[spring-beans-6.2.3.jar:6.2.3]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1808) ~[spring-beans-6.2.3.jar:6.2.3]
        ... 125 common frames omitted
    Caused by: java.lang.IllegalArgumentException: Cannot query by nested entity: ref
        at org.springframework.data.jdbc.repository.query.JdbcQueryCreator.validateProperty(JdbcQueryCreator.java:151) ~[spring-data-jdbc-3.4.3.jar:3.4.3]
        at java.base/java.lang.Iterable.forEach(Iterable.java:75) ~[na:na]
        at org.springframework.data.jdbc.repository.query.JdbcQueryCreator.validate(JdbcQueryCreator.java:130) ~[spring-data-jdbc-3.4.3.jar:3.4.3]
        at org.springframework.data.jdbc.repository.query.PartTreeJdbcQuery.<init>(PartTreeJdbcQuery.java:114) ~[spring-data-jdbc-3.4.3.jar:3.4.3]
        at org.springframework.data.jdbc.repository.support.JdbcQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JdbcQueryLookupStrategy.java:127) ~[spring-data-jdbc-3.4.3.jar:3.4.3]
        at org.springframework.data.jdbc.repository.support.JdbcQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JdbcQueryLookupStrategy.java:257) ~[spring-data-jdbc-3.4.3.jar:3.4.3]
        at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:116) ~[spring-data-commons-3.4.3.jar:3.4.3]
        ... 137 common frames omitted
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 16, 2025
@manosbatsis manosbatsis changed the title Query derivation not working for simple value object using Reading/WritingConverter Query derivation not working for simple domain primitive with Reading/WritingConverter May 16, 2025
@schauder schauder self-assigned this May 19, 2025
@schauder schauder added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels May 19, 2025
@schauder
Copy link
Contributor

schauder commented May 19, 2025

A first test indicate that there is a two fold error here: the arguments converter doesn't get considered when validating the argument and also not when constructing the select.

This might be related to #1828 or at least its fix, which involves some refactoring of the conversion mechanics, which turn out to be duplicated between derived and other queries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants