jmix
jmix copied to clipboard
Cannot use JmixDataRepository defined in add-ons
There is a repository that inherits from jmixDataRepository. There is a service that uses this repository. Both beans are declared in the addon:
public interface NewEntityRepository extends JmixDataRepository<NewEntity, UUID> {
}
@Service
public class NewEntityService {
private final NewEntityRepository newEntityRepository;
public NewEntityService(NewEntityRepository newEntityRepository) {
this.newEntityRepository = newEntityRepository;
}
}
In a project that uses an addon, I get an error when starting the application:
Description:
Parameter 0 of constructor in com.company.jmixrepotestmodule.service.NewEntityService required a bean of type 'com.company.jmixrepotestmodule.repository.NewEntityRepository' that could not be found.
Action:
Consider defining a bean of type 'com.company.jmixrepotestmodule.repository.NewEntityRepository' in your configuration.
- At the same time, the similar structure of the repository and service in the project itself works fine.
- If instead of inheriting interface from JmixDataRepository inherit from PagingRepository the error reappears
- If instead of inheriting the interface from JmixDataRepository, implement the interface by creating a class with the @Repository annotation, the error disappears
Actual for jmix v. 1.3.3
Test project, where i found a bug: main project - https://github.com/nekogochanPomoika/jmix-repo-test/tree/module-test (branch module-test) Addon - https://github.com/nekogochanPomoika/jmix-repo-test-module
I solved this problem locally. There is no need to make changes in the platform code.
The solution for attached project:
- Add @Repository annotation for NewEntityRepository interface (addon)
- Exclude basePackageClasses = NewEntityRepository.class from @EnableJmixDataRepositories annotation in com.company.jmixrepotest.JmixRepoTestApplication (application)
The problem is solved, you can inject NewEntityRepository by interface
@dtaimanov fyi
We have tried to use JmixDataRepository
in our project, but there are several disadvantages:
- Experimental - may be removed or heavily modified API
- It has some open issues
- There aren't variety of methods, for example, lacks methods with
String
orConsumer<FetchPlanBuilder>
for fetchPlan parameter andId<T>
parameter for id.
Eventually our team has decided not to use this. JmixDataRepository
needs further improvement
@Flamesson, thank you for your solution! I will take it into account.
@lovtsovkn, thank you for your feedback! It will help to make JmixDataRepository better.
Hello, @nekogochan !
I've checked your examples and tried to replace Jmix Data Repositories with Spring Data Jpa Repositories and got the same behaviour.
Thus, it is standard behaviour for all Data Repositories (Jmix, Spring Jpa, Mongo, e.t.c): Spring will try to create repository bean each time this repository processed by @EnableXXXRepositories
annotation registrar.
In your case it happens twice: @EnableJmixDataRepositories
is on com.company.jmixrepotestmodule.JrtmConfiguration
and on com.company.jmixrepotest.JmixRepoTestApplication
with the same path.
In order to solve this problem, please specify in this annotation different packages for addon and test project, e.g.:
@EnableJmixDataRepositories(basePackageClasses = AnimalRepository.class)
public class JmixRepoTestApplication {
@EnableJmixDataRepositories(basePackageClasses = NewEntityRepository.class)
@PropertySource(name = "com.company.jmixrepotestmodule", value = "classpath:/com/company/jmixrepotestmodule/module.properties")
public class JrtmConfiguration {
or use exclude, as @Flamesson have suggested.
Note: No @Repository
annotation needed for Jmix Data Repositories! (@Flamesson fyi)
There is your sample and addon where the problem is fixed: JmixDataRepoAndAddons_fixed.zip you can check it by running test in "jmix-repo-test" project (I've also added absent liquibase script to addon).
If you have any further problems with data repositories, please, let me know.