Bean with same name already defined error

In my jmix gradle project i have added my another library project as dependency. That added project is spring boot project that uses spring-data-jpa.

in my jmix application i changed the main like
@SpringBootApplication(scanBasePackages = {“com.batchpackage.batchproj”})
public class JmixApp extends SpringBootServletInitializer {

com.batchpackage.batchproj is the library project.
And i have some repository classes like

public interface BatchConfigRepository extends CrudRepository<BatchConfig, Integer> {} , public interface BatchStatusRepository extends CrudRepository<BatchStatus, Integer> {}.

When i try to run jmix application it shows error as The bean 'batchConfigRepository ', defined in com.batchpackage.batchproj.repository.BatchConfigRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration, could not be registered. A bean with that name has already been defined in com.batchpackage.batchproj.repository.BatchConfigRepository defined in @EnableJmixDataRepositories declared on BootJmixRepositoriesRegistrar.EnableJmixDataRepositoriesConfiguration and overriding is disabled.

My conclusion is both jmix and spring-data jpa is trying to create bean for same repository. Is there any solution for this. More specific can i say i dont want jmix data to scan the given package like jmix please exclude com.batchpackage.batchproj from scanning

Hello, @kiran.ouseph !
You are right, both jmix and spring-data jpa repositories are trying to create the same repository.
In order to solve this problem you can define annotation io.jmix.core.repository.EnableJmixDataRepositories manually on app configuration class and specify the package directly inside of it, e.g.:

@SpringBootApplication
@EnableJmixDataRepositories(basePackages = "package.with.jmix.repositories")
public class MyApplication {
//...

Similar thing should be done with @EnableJpaRepositories if you have JmixDataRepositories in the project and they are affected by spring data jpa repository mechanism.

You can also disable Jmix Data Repositories entirely (see Bean Conflict with JmixDataRepositories) if you don’t need them at all.

Please let me know if this solution will not work properly

Regards,
Dmitry

Sorry for late reply @taimanov I was trying various things and not seems working anything

This is where i am now
@SpringBootApplication(scanBasePackages = {“com.main.proj.", "com.library.proj.”})
@EnableJmixDataRepositories(basePackages = {})
@EnableJpaRepositories(basePackages = {“com.library.proj.*”})

So my intension is scanning all classes of both projects, setting jmix repo as {} (No need to scan any jmix repo because i dont have any), jpa repo as library proj package name.
I have not disabled jmix repo in application.properties. When i do i am not able to access the required repos like user repository and role related repositories

And now i am getting error like
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘OAuth2AuthorizationServerConfiguration’: Unsatisfied dependency expressed through field ‘tokenStore’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘sec_TokenStore’ defined in class path resource [io/jmix/autoconfigure/securityoauth2/SecurityOAuth2AutoConfiguration$JdbcTokenStoreConfiguration.class]: Unsatisfied dependency expressed through method ‘tokenStore’ parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘stellantisroiApplication’: Unsatisfied dependency expressed through field ‘batchExecuter’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘batchExecuter’: Unsatisfied dependency expressed through field ‘batchConfigRepository’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘batchConfigRepository’ defined in com.publicismedia.uniquebatchjava.repository.BatchConfigRepository defined in @EnableJpaRepositories declared on StellantisroiApplication: Cannot create inner bean ‘(inner bean)#51e0629a’ of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property ‘entityManager’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘(inner bean)#51e0629a’: Cannot resolve reference to bean ‘entityManagerFactory’ while setting constructor argument; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘jmix_Liquibase’ defined in class path resource [io/jmix/autoconfigure/data/JmixLiquibaseAutoConfiguration.class]: Unsatisfied dependency expressed through method ‘liquibase’ parameter 0; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name ‘dataSource’: Requested bean is currently in creation: Is there an unresolvable circular reference?

@taimanov
disabling jmix repo in aplication properties not works because it will disable all repo including the ones used in application like userrepository etc

So how to solve.
Allow only the default repos of jmix and the repos of added dependecy

Hello, @kiran.ouseph !
I do apologize for the late reply.

I
First of all, Jmix Data Repositories is nothing else but optional syntactic sugar over the DataManager. It is just a wrapper that gives the ability to work with entities in a convenient way and do not break jmix-specific mechanisms like soft-deletion, audit, dynamic attributes, lazy loading, etc.

So you can freely disable them by adding jmix.core.data-repositories.enabled=false into application.properties.
See the example project with disabled jmix repositories:
disable-jmix-repositories.zip (382.2 KB)

UserRepository, RoleReposotiry and other mandatory for work classes has nothing in common with data repositories but name (you can see it by viewing their code).

II
An example of application using two different types of Data Repositories you can see in next project:
repclashsample.zip (344.3 KB)

Please specify different packages without overlap:

// ...
@SpringBootApplication
@EnableJpaRepositories("com.company.repclashsample.plainrepo")
@EnableJmixDataRepositories("com.company.repclashsample.repo")
public class RepclashsampleApplication {
// ...

III
Please, provide some project example in order to say, why the last error occurs (which ends with org.springframework.beans.factory.BeanCurrentlyInCreationException).

Unfortunately, for now, I can only guess that it is some bean configuration conflict between Jmix app and your library.

Regards,
Dmitry