jmix icon indicating copy to clipboard operation
jmix copied to clipboard

Cannot use JmixDataRepository defined in add-ons

Open nekogochan opened this issue 2 years ago • 1 comments

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.
  1. At the same time, the similar structure of the repository and service in the project itself works fine.
  2. If instead of inheriting interface from JmixDataRepository inherit from PagingRepository the error reappears
  3. 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

nekogochan avatar Sep 09 '22 06:09 nekogochan

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

Flamesson avatar Oct 17 '22 16:10 Flamesson

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 or Consumer<FetchPlanBuilder> for fetchPlan parameter and Id<T> parameter for id.

Eventually our team has decided not to use this. JmixDataRepository needs further improvement

lovtsovkn avatar Jan 25 '23 05:01 lovtsovkn

@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.

dtaimanov avatar Jan 25 '23 06:01 dtaimanov

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.

dtaimanov avatar Mar 29 '23 17:03 dtaimanov