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

Add fluent findBy API to JpaSpecificationExecutor #2274

Closed
quaff opened this issue Aug 9, 2021 · 4 comments
Closed

Add fluent findBy API to JpaSpecificationExecutor #2274

quaff opened this issue Aug 9, 2021 · 4 comments
Assignees
Labels
in: repository Repositories abstraction type: enhancement A general enhancement

Comments

@quaff
Copy link
Contributor

quaff commented Aug 9, 2021

follow up #2273, I need streaming matched data filter by Specification, I tried

public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {

	Stream<User> streamAllBy(@Nullable Specification<User> spec, Sort sort);

}

It failed with:

org.springframework.dao.InvalidDataAccessApiUsageException: At least 1 parameter(s) provided but only 0 parameter(s) present in query.; nested exception is java.lang.IllegalArgumentException: At least 1 parameter(s) provided but only 0 parameter(s) present in query.
	at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:374)
	at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:235)
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:551)
	at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
	at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:152)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:145)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
	at com.sun.proxy.$Proxy94.streamAllBy(Unknown Source)

It seems like Specification method parameter is treated as query parameter.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 9, 2021
@schauder
Copy link
Contributor

@mp911de mp911de added status: duplicate A duplicate of another issue type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged status: duplicate A duplicate of another issue labels Sep 20, 2021
@gregturn
Copy link
Contributor

@quaff Have you checked out the recently added QuerydslJpaPredicateExecutor.findBy(Predicate predicate, Function<FetchableFluentQuery<S>, R> queryFunction) yet?

An example of using it to get a Stream -based response based upon a Query DSL criteria is shown below.

@Test // GH-2294
void findByFluentPredicateStream() {

	Stream<User> userStream = predicateExecutor.findBy(user.firstname.contains("v"),
			q -> q.sortBy(Sort.by("firstname")).stream());

	assertThat(userStream).containsExactly(dave, oliver);
}

There is also support for Query By Example.

@quaff
Copy link
Contributor Author

quaff commented Nov 11, 2021

@gregturn Thanks, but I'm not using Query DSL.

@mp911de
Copy link
Member

mp911de commented Feb 15, 2022

It would make sense to add the fluent findBy API to JpaSpecificationExecutor.

@mp911de mp911de changed the title Add stream return type methods to JpaSpecificationExecutor Add fluent findBy API to JpaSpecificationExecutor Feb 15, 2022
gregturn added a commit that referenced this issue Mar 31, 2022
gregturn added a commit that referenced this issue Apr 1, 2022
Extend fluent findBy support through the usage of Specifications.

See #2274.
@gregturn gregturn added this to the 3.0 M4 (2022.0.0) milestone Apr 1, 2022
@gregturn gregturn added the in: repository Repositories abstraction label Apr 1, 2022
gregturn added a commit that referenced this issue Apr 1, 2022
Extend fluent findBy support through the usage of Specifications.

See #2274.
gregturn added a commit that referenced this issue Apr 1, 2022
Extend fluent findBy support through the usage of Specifications.

See #2274.
@gregturn gregturn closed this as completed Apr 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: repository Repositories abstraction type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

5 participants