Skip to content
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

NonUniqueBeanException: Multiple possible bean candidates found: [DefaultInternalConstraintValidatorFactory, DefaultConstraintValidatorFactory] #2793

Closed
kareemelzayat opened this issue Feb 13, 2024 · 16 comments
Assignees
Labels
closed: duplicate This issue or pull request already exists

Comments

@kareemelzayat
Copy link
Contributor

Expected Behavior

After upgrading to Micronaut 4.3.1, I should be able to have the app running locally without issues

Actual Behaviour

I am getting the following error when trying to run the Micronaut app locally:

11:49:05.435 [main] ERROR io.micronaut.runtime.Micronaut - Error starting Micronaut server: Bean definition [org.hibernate.SessionFactory] could not be loaded: Error instantiating bean of type  [io.micronaut.configuration.hibernate.jpa.conf.sessionfactory.configure.internal.ValidatorFactoryConfigurer]

Message: Multiple possible bean candidates found: [DefaultInternalConstraintValidatorFactory, DefaultConstraintValidatorFactory]
Path Taken: SessionFactoryPerDataSourceFactory.buildHibernateSessionFactoryBuilder(SessionFactoryBuilder sessionFactoryBuilder) --> new SessionFactoryPerDataSourceFactory(Environment environment,[List configures],StandardServiceRegistryBuilderCreator serviceRegistryBuilderSupplier,List standardServiceRegistryBuilderConfigurers,JpaConfiguration jpaConfiguration,ApplicationContext applicationContext,Integrator integrator) --> new ValidatorFactoryConfigurer([ValidatorFactory validatorFactory])
io.micronaut.context.exceptions.BeanInstantiationException: Bean definition [org.hibernate.SessionFactory] could not be loaded: Error instantiating bean of type  [io.micronaut.configuration.hibernate.jpa.conf.sessionfactory.configure.internal.ValidatorFactoryConfigurer]

Message: Multiple possible bean candidates found: [DefaultInternalConstraintValidatorFactory, DefaultConstraintValidatorFactory]
Path Taken: SessionFactoryPerDataSourceFactory.buildHibernateSessionFactoryBuilder(SessionFactoryBuilder sessionFactoryBuilder) --> new SessionFactoryPerDataSourceFactory(Environment environment,[List configures],StandardServiceRegistryBuilderCreator serviceRegistryBuilderSupplier,List standardServiceRegistryBuilderConfigurers,JpaConfiguration jpaConfiguration,ApplicationContext applicationContext,Integrator integrator) --> new ValidatorFactoryConfigurer([ValidatorFactory validatorFactory])
	at io.micronaut.context.DefaultBeanContext.initializeContext(DefaultBeanContext.java:2001)
	at io.micronaut.context.DefaultApplicationContext.initializeContext(DefaultApplicationContext.java:292)
	at io.micronaut.context.DefaultBeanContext.readAllBeanDefinitionClasses(DefaultBeanContext.java:3382)
	at io.micronaut.context.DefaultBeanContext.finalizeConfiguration(DefaultBeanContext.java:3737)
	at io.micronaut.context.DefaultBeanContext.start(DefaultBeanContext.java:361)
	at io.micronaut.context.DefaultApplicationContext.start(DefaultApplicationContext.java:200)
	at io.micronaut.runtime.Micronaut.start(Micronaut.java:72)
	at com.uberall.lsus.ApplicationKt.main(ApplicationKt.kt:25)
Caused by: io.micronaut.context.exceptions.BeanInstantiationException: Error instantiating bean of type  [io.micronaut.configuration.hibernate.jpa.conf.sessionfactory.configure.internal.ValidatorFactoryConfigurer]

Message: Multiple possible bean candidates found: [DefaultInternalConstraintValidatorFactory, DefaultConstraintValidatorFactory]
Path Taken: SessionFactoryPerDataSourceFactory.buildHibernateSessionFactoryBuilder(SessionFactoryBuilder sessionFactoryBuilder) --> new SessionFactoryPerDataSourceFactory(Environment environment,[List configures],StandardServiceRegistryBuilderCreator serviceRegistryBuilderSupplier,List standardServiceRegistryBuilderConfigurers,JpaConfiguration jpaConfiguration,ApplicationContext applicationContext,Integrator integrator) --> new ValidatorFactoryConfigurer([ValidatorFactory validatorFactory])
	at io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2345)
	at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2300)
	at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2312)
	at io.micronaut.context.DefaultBeanContext.createRegistration(DefaultBeanContext.java:3114)
	at io.micronaut.context.SingletonScope.getOrCreate(SingletonScope.java:80)
	at io.micronaut.context.DefaultBeanContext.findOrCreateSingletonBeanRegistration(DefaultBeanContext.java:3016)
	at io.micronaut.context.DefaultBeanContext.resolveBeanRegistration(DefaultBeanContext.java:2977)
	at io.micronaut.context.DefaultBeanContext.resolveBeanRegistration(DefaultBeanContext.java:2751)
	at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:1750)
	at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:1732)
	at io.micronaut.configuration.hibernate.validator.$ValidatorFactoryProvider$ValidatorFactory0$Definition.instantiate(Unknown Source)
	at io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2330)
	at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2300)
	at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2312)
	at io.micronaut.context.DefaultBeanContext.createRegistration(DefaultBeanContext.java:3114)
	at io.micronaut.context.SingletonScope.getOrCreate(SingletonScope.java:80)
	at io.micronaut.context.DefaultBeanContext.findOrCreateSingletonBeanRegistration(DefaultBeanContext.java:3016)
	at io.micronaut.context.DefaultBeanContext.resolveBeanRegistration(DefaultBeanContext.java:2977)
	at io.micronaut.context.DefaultBeanContext.resolveBeanRegistration(DefaultBeanContext.java:2751)
	at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:1750)
	at io.micronaut.context.AbstractBeanResolutionContext.getBean(AbstractBeanResolutionContext.java:89)
	at io.micronaut.context.AbstractInitializableBeanDefinition.resolveBean(AbstractInitializableBeanDefinition.java:2165)
	at io.micronaut.context.AbstractInitializableBeanDefinition.getBeanForConstructorArgument(AbstractInitializableBeanDefinition.java:1328)
	at io.micronaut.configuration.hibernate.jpa.conf.sessionfactory.configure.internal.$ValidatorFactoryConfigurer$Definition.instantiate(Unknown Source)
	at io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2330)
	at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2300)
	at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2312)
	at io.micronaut.context.DefaultBeanContext.createRegistration(DefaultBeanContext.java:3114)
	at io.micronaut.context.DefaultBeanContext.resolveBeanRegistration(DefaultBeanContext.java:3004)
	at io.micronaut.context.DefaultBeanContext.resolveBeanRegistration(DefaultBeanContext.java:2951)
	at io.micronaut.context.DefaultBeanContext.addCandidateToList(DefaultBeanContext.java:3566)
	at io.micronaut.context.DefaultBeanContext.resolveBeanRegistrations(DefaultBeanContext.java:3514)
	at io.micronaut.context.DefaultBeanContext.getBeanRegistrations(DefaultBeanContext.java:3488)
	at io.micronaut.context.DefaultBeanContext.getBeansOfType(DefaultBeanContext.java:1451)
	at io.micronaut.context.AbstractBeanResolutionContext.getBeansOfType(AbstractBeanResolutionContext.java:95)
	at io.micronaut.context.AbstractInitializableBeanDefinition.resolveBeansOfType(AbstractInitializableBeanDefinition.java:2253)
	at io.micronaut.context.AbstractInitializableBeanDefinition.getBeansOfTypeForConstructorArgument(AbstractInitializableBeanDefinition.java:1476)
	at io.micronaut.configuration.hibernate.jpa.conf.$SessionFactoryPerDataSourceFactory$Definition.instantiate(Unknown Source)
	at io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2330)
	at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2300)
	at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2312)
	at io.micronaut.context.DefaultBeanContext.createRegistration(DefaultBeanContext.java:3114)
	at io.micronaut.context.SingletonScope.getOrCreate(SingletonScope.java:80)
	at io.micronaut.context.DefaultBeanContext.findOrCreateSingletonBeanRegistration(DefaultBeanContext.java:3016)
	at io.micronaut.context.DefaultBeanContext.resolveBeanRegistration(DefaultBeanContext.java:2977)
	at io.micronaut.context.DefaultBeanContext.resolveBeanRegistration(DefaultBeanContext.java:2751)
	at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:1750)
	at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:1732)
	at io.micronaut.configuration.hibernate.jpa.conf.$SessionFactoryPerDataSourceFactory$BuildHibernateSessionFactoryBuilder4$Definition.instantiate(Unknown Source)
	at io.micronaut.context.BeanDefinitionDelegate.instantiate(BeanDefinitionDelegate.java:159)
	at io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2330)
	at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2300)
	at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2312)
	at io.micronaut.context.DefaultBeanContext.createRegistration(DefaultBeanContext.java:3114)
	at io.micronaut.context.SingletonScope.getOrCreate(SingletonScope.java:80)
	at io.micronaut.context.DefaultBeanContext.findOrCreateSingletonBeanRegistration(DefaultBeanContext.java:3016)
	at io.micronaut.context.DefaultBeanContext.initializeEagerBean(DefaultBeanContext.java:2688)
	at io.micronaut.context.DefaultBeanContext.initializeContext(DefaultBeanContext.java:1995)
	... 7 common frames omitted
Caused by: io.micronaut.context.exceptions.NonUniqueBeanException: Multiple possible bean candidates found: [DefaultInternalConstraintValidatorFactory, DefaultConstraintValidatorFactory]
	at io.micronaut.context.DefaultBeanContext.findConcreteCandidate(DefaultBeanContext.java:2449)
	at io.micronaut.context.DefaultApplicationContext.findConcreteCandidate(DefaultApplicationContext.java:618)
	at io.micronaut.context.DefaultBeanContext.lastChanceResolve(DefaultBeanContext.java:3272)
	at io.micronaut.context.DefaultBeanContext.pickOneBean(DefaultBeanContext.java:3216)
	at io.micronaut.context.DefaultBeanContext.findConcreteCandidateNoCache(DefaultBeanContext.java:3172)
	at io.micronaut.context.DefaultBeanContext.findConcreteCandidate(DefaultBeanContext.java:3155)
	at io.micronaut.context.DefaultBeanContext.findBeanDefinition(DefaultBeanContext.java:2766)
	at io.micronaut.context.DefaultBeanContext.resolveBeanRegistration(DefaultBeanContext.java:2741)
	at io.micronaut.context.DefaultBeanContext.findBean(DefaultBeanContext.java:1813)
	at io.micronaut.context.AbstractBeanResolutionContext.findBean(AbstractBeanResolutionContext.java:112)
	at io.micronaut.context.AbstractInitializableBeanDefinition.resolveOptionalBean(AbstractInitializableBeanDefinition.java:2309)
	at io.micronaut.context.AbstractInitializableBeanDefinition.findBeanForField(AbstractInitializableBeanDefinition.java:1908)
	at io.micronaut.configuration.hibernate.validator.$ValidatorFactoryProvider$Definition.inject(Unknown Source)
	at io.micronaut.configuration.hibernate.validator.$ValidatorFactoryProvider$Definition.instantiate(Unknown Source)
	at io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2330)
	... 64 common frames omitted

From checking, it seems that Hibernate is confused which concrete Bean to use for validation. And it seems to be that DefaultInternalConstraintValidatorFactory was introduced lately.

Is there any configuration that needs to be coded?

Steps To Reproduce

Run a Micronaut app locally with the environment set up below.

Environment Information

Environment:

  • Micronaut 4.3.1
  • Micronaut Data JPA Hibernate
  • io.micronaut.beanvalidation:micronaut-hibernate-validator
  • JDK: 21
  • Kotlin: 1.9.22

Example Application

No response

Version

4.3.X

@kareemelzayat
Copy link
Contributor Author

@dstepanov I see you were the author of the DefaultInternalConstraintValidatorFactory class. Is there a way to configure Hibernate to use that class without issues?

@sdelamo
Copy link
Contributor

sdelamo commented Feb 13, 2024

@kareemelzayat is this a duplicated of micronaut-projects/micronaut-sql#1266

@sdelamo sdelamo moved this to In Progress in 4.3.2 Release Feb 13, 2024
@sdelamo sdelamo self-assigned this Feb 13, 2024
@kareemelzayat
Copy link
Contributor Author

@sdelamo yes! that definitely seems like a duplicate. I suppose it will be fixed with Micronaut 4.3.2?

@sdelamo
Copy link
Contributor

sdelamo commented Feb 14, 2024

I suppose it will be fixed with Micronaut 4.3.2?

Yes.

@sdelamo sdelamo added the closed: duplicate This issue or pull request already exists label Feb 15, 2024
@sdelamo sdelamo closed this as completed Feb 15, 2024
@github-project-automation github-project-automation bot moved this from In Progress to Done in 4.3.2 Release Feb 15, 2024
@kareemelzayat
Copy link
Contributor Author

kareemelzayat commented Feb 19, 2024

@sdelamo I am still experiencing the same exact issue on Micronaut 4.3.2. Are you sure the problem got solved?

@dstepanov
Copy link
Contributor

@kareemelzayat Make sure you don't have Micronaut Validation and Micronaut Hibernate Validation on the classpath

@kareemelzayat
Copy link
Contributor Author

@dstepanov why not? I use them both 🤔

@dstepanov
Copy link
Contributor

How??

@kareemelzayat
Copy link
Contributor Author

Without the implementation("io.micronaut.validation:micronaut-validation"), I cannot use the @Validated API for example. Without the implementation("io.micronaut.beanvalidation:micronaut-hibernate-validator") the same issue arises

@kareemelzayat
Copy link
Contributor Author

kareemelzayat commented Feb 19, 2024

Also, without the implementation("io.micronaut.beanvalidation:micronaut-hibernate-validator"); I am not able to use the DefaultValidator bean in my Domain Specs as it does not behave properly - returns wrong results

@dstepanov
Copy link
Contributor

Please create issues when you need correct results.

@kareemelzayat
Copy link
Contributor Author

But I don't understand why I have to use one or the other 🤔 Because I have been using both since forever

@dstepanov
Copy link
Contributor

Micronaut Validation implements the Jakarta validation API so that it doesn't use reflection, etc. It looks like we need to correct the bother use-case.

@kareemelzayat
Copy link
Contributor Author

@dstepanov yeah I think both libraries should be available to use without an issue. Do you need a ticket?

@sdelamo
Copy link
Contributor

sdelamo commented Feb 19, 2024

@kareemelzayat you should use either Micronaut Validation or Micronaut Hibernate Validator. Both are implementations of the bean validation specification.

@sdelamo
Copy link
Contributor

sdelamo commented Feb 19, 2024

@sdelamo I am still experiencing the same exact issue on Micronaut 4.3.2. Are you sure the problem got solved?

the problem will be solved in 4.3.3. We found: micronaut-projects/micronaut-hibernate-validator#370

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed: duplicate This issue or pull request already exists
Projects
No open projects
Status: Done
Development

No branches or pull requests

3 participants