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

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

Open
tcrespog opened this issue Feb 12, 2024 · 3 comments · Fixed by #1267
Assignees
Labels
info: workaround available A workaround is available for the issue type: bug Something isn't working

Comments

@tcrespog
Copy link

Expected Behavior

No exception on starting up a default Micronaut Data + Hibernate JPA app.

Actual Behaviour

The application fails to start with the following error:

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]

Steps To Reproduce

  1. Create a Micronaut Data + Hibernate JPA project using Micronaut 4.3.1: mn create-app -b gradle -l groovy -t spock --features=h2,jdbc-tomcat,data-jpa,hibernate-jpa,hibernate-validator mn-hibernate-playground.
  2. Run the app: ./gradlew run.
  3. The app fails to start.

Environment Information

  • Ubuntu 22.04
  • Java 17

Example Application

No response

Version

4.3.1

@tcrespog
Copy link
Author

Sorry, but the error seems to keep happening in Micronaut 4.3.2 (same steps to reproduce).

@sdelamo sdelamo reopened this Feb 19, 2024
@sdelamo
Copy link
Contributor

sdelamo commented Feb 19, 2024

A workaround to force the usage of the Micronaut hibernate validator ConstraintValidatorFactory is to create such as class:

package mn.hibernate.playground;

import io.micronaut.configuration.hibernate.validator.DefaultConstraintValidatorFactory;
import io.micronaut.context.BeanContext;
import io.micronaut.context.annotation.Replaces;
import jakarta.inject.Singleton;
import jakarta.validation.ConstraintValidatorFactory;

@Replaces(ConstraintValidatorFactory.class)
@Singleton
public class DefaultConstraintValidatorFactoryReplacement extends DefaultConstraintValidatorFactory {
    public DefaultConstraintValidatorFactoryReplacement(BeanContext beanContext) {
        super(beanContext);
    }
}

@sdelamo sdelamo added the info: workaround available A workaround is available for the issue label Feb 19, 2024
sdelamo added a commit to micronaut-projects/micronaut-hibernate-validator that referenced this issue Feb 19, 2024
A `ConstraintValidationFactory` aws added to Micronaut Validation in [PR 288](micronaut-projects/micronaut-validation#288). This PR sets the Hibernate Validator as the primary `ConstraintValidatorFactory` in case there are many in the classpath and avoids:

```
Caused by: io.micronaut.context.exceptions.NonUniqueBeanException: Multiple possible bean candidates found: [DefaultInternalConstraintValidatorFactory, DefaultConstraintValidatorFactory]
```

see: micronaut-projects/micronaut-sql#1266
sdelamo added a commit to micronaut-projects/micronaut-hibernate-validator that referenced this issue Feb 19, 2024
)

* fix(deps): update dependency io.micronaut.validation:micronaut-validation-bom to v4.4.0

* fix: add @primary annotation for DefaultConstraintValidatorFactory

A `ConstraintValidationFactory` aws added to Micronaut Validation in [PR 288](micronaut-projects/micronaut-validation#288). This PR sets the Hibernate Validator as the primary `ConstraintValidatorFactory` in case there are many in the classpath and avoids:

```
Caused by: io.micronaut.context.exceptions.NonUniqueBeanException: Multiple possible bean candidates found: [DefaultInternalConstraintValidatorFactory, DefaultConstraintValidatorFactory]
```

see: micronaut-projects/micronaut-sql#1266

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
sdelamo added a commit to micronaut-projects/micronaut-hibernate-validator that referenced this issue Feb 19, 2024
)

A `ConstraintValidationFactory` aws added to Micronaut Validation in [PR 288](micronaut-projects/micronaut-validation#288). This PR sets the Hibernate Validator as the primary `ConstraintValidatorFactory` in case there are many in the classpath and avoids:

```
Caused by: io.micronaut.context.exceptions.NonUniqueBeanException: Multiple possible bean candidates found: [DefaultInternalConstraintValidatorFactory, DefaultConstraintValidatorFactory]
```

see: micronaut-projects/micronaut-sql#1266

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
@sdelamo
Copy link
Contributor

sdelamo commented Feb 21, 2024

Micronaut Framework BOM 4.3.3 will include Micronaut hibernate validator 4.2.1. We will release it by the end of the week.

However, you can force it now:

configurations.all {
    resolutionStrategy {
        force "io.micronaut.beanvalidation:micronaut-hibernate-validator:4.2.1"
    }
}

If in a terminal running Micronaut CLI 4.3.2, I run

mn create-app -b gradle -l groovy -t spock --features=h2,jdbc-tomcat,data-jpa,hibernate-jpa,hibernate-validator mn-hibernate-playground.

then I force the micronaut-hibernate-validator to 4.2.1 with the above snippet.

I add a class such as:

package entities;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;

@Entity
public class Book {
    @Id
    private Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

and modify the JPA configuration to scan for entities with:

jpa.default.entity-scan.packages=entities

I can run ./gradle run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
info: workaround available A workaround is available for the issue type: bug Something isn't working
Projects
No open projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants