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

Allow Supertypes Specifications in JpaSpecificationExecutor #3300

Open
aykborstelmann opened this issue Jan 4, 2024 · 5 comments · May be fixed by #3301
Open

Allow Supertypes Specifications in JpaSpecificationExecutor #3300

aykborstelmann opened this issue Jan 4, 2024 · 5 comments · May be fixed by #3301
Assignees
Labels
for: team-attention An issue we need to discuss as a team to make progress status: waiting-for-triage An issue we've not yet triaged

Comments

@aykborstelmann
Copy link

aykborstelmann commented Jan 4, 2024

See the following commit: 4c45125
I added a test in your codebase which is exactly the same as I am going to describe.

Suppose we have an abstract @MappedSupperclass AbstractMappedType and a concrete entity ConcreteType1 extending AbstractMappedType.

Then we also have MappedTypeRepository which is a @NoRepositoryBean but does extend JpaSpecificationExecutor and must therefore look like this:

@NoRepositoryBean
public interface MappedTypeRepository<T extends AbstractMappedType> extends JpaRepository<T, Long>, JpaSpecificationExecutor<T> {
}

However when using this repository in its abstract form, we face some issues.

@Autowired
@Qualifier("concreteRepository1")
private MappedTypeRepository<?> mappedTypeRepository;

@Test
void testUseMappedTypeRepository() {
  concreteRepository1.save(new ConcreteType1("Test"));

  List<? extends AbstractMappedType> findings = mappedTypeRepository.findAll(
      where(mappedTypeAttribute1Equals("Test")));
  assertThat(findings).isNotEmpty();
}

private static Specification<AbstractMappedType> mappedTypeAttribute1Equals(String attribute1) {
  return (root, query, criteriaBuilder) -> criteriaBuilder.equal(root.get(AbstractMappedType_.ATTRIBUTE1), attribute1);
}

This does not work because Specification<AbstractMappedType> cannot be applied as first parameter for
List<T> findAll(Specification<T> spec); of JpaSpecificationExecutor<T>.

IMO, the method JpaSpecificationExecutor#findAll should be defined as follows, accepting also specifications of supertypes of T.

List<T> findAll(Specification<? super T> spec); 

In fact, I have already implemented change, and you can have a look at #3301

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 4, 2024
@christophstrobl christophstrobl added the for: team-attention An issue we need to discuss as a team to make progress label Jan 9, 2024
@christophstrobl
Copy link
Member

@aykborstelmann thank you for getting in touch. Let me take this to the team.

@mp911de mp911de self-assigned this Jan 22, 2024
@aykborstelmann
Copy link
Author

@christophstrobl Thanks for taking care. Any updates on this ?

@aykborstelmann
Copy link
Author

Hi, once again, any updates on it ?
I already provided you an PR (#3301) which implements the change :)

@mp911de
Copy link
Member

mp911de commented Apr 22, 2024

For the time being, this looks like a wall of changes without providing an immediate benefit. As we don't have much bandwidth, this isn't something we will incorporate soon. We might revisit your proposal next year or so.

@aykborstelmann
Copy link
Author

Alright, thank you for the answer!

I hope I could motivate the benefits enough with the given example and test, if not please let me know !
Currently any usage of JpaSpecificationExecutor in a generic repository have to be faced with hacky solutions such as wildcards in returns types and unchecked castings. I have faced this issue in many projects until now.
Hopefully the passing tests can somehow ensure you valid working state of the PR.

I am looking forward to a diskussion then :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: team-attention An issue we need to discuss as a team to make progress status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants