micronaut-projects / micronaut-sql

Projects to support SQL Database access in Micronaut
Apache License 2.0
74 stars 42 forks source link

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

Open tcrespog opened 9 months ago

tcrespog commented 9 months ago

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

Example Application

No response

Version

4.3.1

tcrespog commented 9 months ago

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

sdelamo commented 9 months ago

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 commented 9 months ago

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