-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Fragment repository implementation should be imported even it's not in basePackages of @EnableJpaRepositories #3287
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
Comments
This is by design. Custom implementations/fragments use two different strategies though:
Both schemes use the base package as defined in the Fragment implementations must reside in the same package as their fragment interface and fragment implementations must be within one of the specified base packages. |
It's a valid use case that reuse fragment repositories as common library share by applications, normally application use different base package name, then I propose that Or It should be available if it's registered as spring bean: package test;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.test.context.ContextConfiguration;
import com.example.demo.CustomRepository;
import com.example.demo.CustomRepositoryImpl;
@DataJpaTest
@EnableJpaRepositories(basePackageClasses = { TestEntityRepository.class })
@EntityScan(basePackageClasses = TestEntity.class)
@ContextConfiguration(classes = TestEntityRepositoryTests.Config.class)
public class TestEntityRepositoryTests {
@Autowired
private TestEntityRepository testEntityRepository;
@Test
void test() {
assertThatThrownBy(() -> testEntityRepository.customSave(new TestEntity()))
.isInstanceOf(UnsupportedOperationException.class).hasMessage("Not Implemented");
}
static class Config {
@Bean
CustomRepository<?> customRepositoryImpl() {
return new CustomRepositoryImpl<>();
}
}
} In a nutshell, it's not a good idea that |
It's an enhancement that should not break existing behaviors. The default implementation is `$FragmentInterfaceName + $ImplementationPostfix`, for example `com.example.FragmentImpl` is the default implementation of `com.example.Fragment`. It's useful for sharing repository fragments as library, application doesn't have to include library package in `@Enable…Repositories`, and it will back off if application provides custom implementation. See spring-projects/spring-data-jpa#3287
It's an enhancement that should not break existing behaviors. The default implementation is `$FragmentInterfaceName + $ImplementationPostfix`, for example `com.example.FragmentImpl` is the default implementation of `com.example.Fragment`. It's useful for sharing repository fragments as library, application doesn't have to include library package in `@Enable…Repositories`, and it will back off if application provides custom implementation. See spring-projects/spring-data-jpa#3287
Given:
Then:
com.example.demo.CustomRepositoryImpl
should be registered as repository fragment implementation implicitly sincecom.example.demo.CustomRepository
is declared as repository fragment.Actual:
Here is a failed test
Minimal reproducible project: repository-scan.zip
The text was updated successfully, but these errors were encountered: