Skip to content

Mapping collections to SQL arrays does not work with repositories #3356

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

Closed
markusheiden opened this issue Feb 8, 2024 · 3 comments
Closed
Assignees
Labels
type: bug A general bug

Comments

@markusheiden
Copy link

When mapping collections to SQL arrays like this with Hibernate 6.4

class MyEnttiy {
  @Column(name = "ids")
  private SortedSet<Long> ids;
  ...
}

a repository with queries against that column cannot be created

interface MyRepository extends Repository<MyEntity, Long> {
  MyEntity findByIds(SortedSet<Long> ids);
}

results in

Operator SIMPLE_PROPERTY on ids requires a scalar argument, found interface java.util.SortedSet in method public abstract MyEntity MyRepository.findByIds(java.util.SortedSet)
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 8, 2024
@christophstrobl
Copy link
Member

Thank you for getting in touch. Which version of data-jpa are you using? Can you please provide the full stacktrace,
ideally take the time to provide a complete minimal sample (something that we can unzip or git clone, build, and deploy) that reproduces the problem.
Have you tried if the problem is reproducible using plain JPA/hibernate?

@christophstrobl christophstrobl added the status: waiting-for-feedback We need additional information before we can continue label Feb 8, 2024
@markusheiden
Copy link
Author

markusheiden commented Feb 8, 2024

I used Spring Boot 3.2.2.

To reproduce, simply startup an application with these two classes: demo.zip

Entity:

package com.adsoul;

import java.util.SortedSet;
import java.util.TreeSet;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Entity
@Table(name = "my_entity")
public class MyEntity {
    @Id
    @Column(name = "id", nullable = false)
    @GeneratedValue
    private Long id;

    // This automatically maps to a SQL array.
    @Column(name = "ids")
    private SortedSet<Long> ids = new TreeSet<>();
}

Repository:

package com.adsoul;

import java.util.SortedSet;

import org.springframework.data.repository.Repository;

public interface MyRepository extends Repository<MyEntity, Long> {
    MyEntity findByIds(SortedSet<Long> ids);
}

Stacktrace:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myRepository' defined in com.adsoul.MyRepository defined in @EnableJpaRepositories declared on PersistenceConfig: Could not create query for public abstract com.adsoul.MyEntity com.adsoul.MyRepository.findByIds(java.util.SortedSet); Reason: Failed to create query for method public abstract com.adsoul.MyEntity com.adsoul.MyRepository.findByIds(java.util.SortedSet); Operator SIMPLE_PROPERTY on ids requires a scalar argument, found interface java.util.SortedSet in method public abstract com.adsoul.MyEntity com.adsoul.MyRepository.findByIds(java.util.SortedSet)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1773)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:599)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:521)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:325)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:323)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:969)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:959)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:624)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:753)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:455)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:323)
	at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:149)
	at com.adsoul.web.WebApplication.main(WebApplication.java:43)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:50)
Caused by: org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract com.adsoul.MyEntity com.adsoul.MyRepository.findByIds(java.util.SortedSet); Reason: Failed to create query for method public abstract com.adsoul.MyEntity com.adsoul.MyRepository.findByIds(java.util.SortedSet); Operator SIMPLE_PROPERTY on ids requires a scalar argument, found interface java.util.SortedSet in method public abstract com.adsoul.MyEntity com.adsoul.MyRepository.findByIds(java.util.SortedSet)
	at org.springframework.data.repository.query.QueryCreationException.create(QueryCreationException.java:101)
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:115)
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.mapMethodsToQuery(QueryExecutorMethodInterceptor.java:99)
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lambda$new$0(QueryExecutorMethodInterceptor.java:88)
	at java.base/java.util.Optional.map(Optional.java:260)
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.<init>(QueryExecutorMethodInterceptor.java:88)
	at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:357)
	at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:279)
	at org.springframework.data.util.Lazy.getNullable(Lazy.java:135)
	at org.springframework.data.util.Lazy.get(Lazy.java:113)
	at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:285)
	at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:132)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1820)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1769)
	... 18 common frames omitted
Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract com.adsoul.MyEntity com.adsoul.MyRepository.findByIds(java.util.SortedSet); Operator SIMPLE_PROPERTY on ids requires a scalar argument, found interface java.util.SortedSet in method public abstract com.adsoul.MyEntity com.adsoul.MyRepository.findByIds(java.util.SortedSet)
	at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:107)
	at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:124)
	at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:258)
	at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:95)
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:111)
	... 30 common frames omitted
Caused by: java.lang.IllegalStateException: Operator SIMPLE_PROPERTY on ids requires a scalar argument, found interface java.util.SortedSet in method public abstract com.adsoul.MyEntity com.adsoul.MyRepository.findByIds(java.util.SortedSet)
	at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.throwExceptionOnArgumentMismatch(PartTreeJpaQuery.java:172)
	at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.validate(PartTreeJpaQuery.java:148)
	at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:101)
	... 34 common frames omitted

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Feb 8, 2024
@christophstrobl christophstrobl added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided labels Feb 9, 2024
@christophstrobl christophstrobl self-assigned this Feb 9, 2024
@christophstrobl
Copy link
Member

Thank you @markusheiden - seems to be an unintended side effect of #1478. We'll get this fixed.

mp911de pushed a commit that referenced this issue Feb 13, 2024
This change allows to use collection/array arguments with scalar operators when targeting a collection like property.

Closes #3356
Original pull request: #3359
mp911de pushed a commit that referenced this issue Feb 13, 2024
This change allows to use collection/array arguments with scalar operators when targeting a collection like property.

Closes #3356
Original pull request: #3359
@mp911de mp911de added this to the 3.1.9 (2023.0.9) milestone Feb 13, 2024
mp911de pushed a commit that referenced this issue Feb 14, 2024
This change allows to use collection/array arguments with scalar operators when targeting a collection like property.

Closes #3356
Original pull request: #3359
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

Successfully merging a pull request may close this issue.

4 participants