Skip to content

Introduce @Lookup methods on repository #3001

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
wants to merge 1 commit into from

Conversation

quaff
Copy link
Contributor

@quaff quaff commented Dec 12, 2023

After this commit, we could lookup beans from BeanFactory and use them in default methods.

interface FooRepository extends CrudRepository<Foo, Long> {

	default void doSomething(Long fooId, Long barId) {
		Optional<Foo> foo = findById(fooId);
		Optional<Bar> bar = barRepository().findById(barId);
		//TODO do something with foo and bar
	}

	@Lookup
	BarRepository barRepository();
}
interface BarRepository extends CrudRepository<Bar, Long> {

}

After this commit, we could lookup beans from BeanFactory and use them in default methods.

```java
interface FooRepository extends CrudRepository<Foo, Long> {

	default void doSomething(Long fooId, Long barId) {
		Optional<Foo> foo = findById(fooId);
		Optional<Bar> bar = barRepository().findById(barId);
		//TODO do something with foo and bar
	}

	@lookup
	BarRepository barRepository();
}
```

```java
interface BarRepository extends CrudRepository<Bar, Long> {

}
```
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 12, 2023
@mp911de
Copy link
Member

mp911de commented Dec 14, 2023

That's not how we want our code to be used. You're mixing concerns of a bean container with public API. Use custom repository fragments with @Autowired instead.

@mp911de mp911de closed this Dec 14, 2023
@mp911de mp911de added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged labels Dec 14, 2023
@quaff
Copy link
Contributor Author

quaff commented Dec 15, 2023

Or we could narrow the type only accept EntityManager and Repository.

interface FooRepository extends CrudRepository<Foo, Long> {

	default void doSomething(Long fooId) {
		EntityManager entityManager = entityManager();
		//TODO do something with entityManager and fooId
	}

	@Lookup
	EntityManager entityManager();
}

It's more concise compare to custom repository fragments, WDYT?

@quaff
Copy link
Contributor Author

quaff commented Dec 19, 2023

@mp911de Could you guide me the easiest way to implement such extension at application side, customizing repositoryFactoryBeanClass of @EnableJpaRepositories seems viable but not easy because currently there are not enough extension points, some methods are private and some codes should be extracted to protected method to allow override.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants