Cannot invoke "org.hibernate.boot.spi.MetadataBuildingContext.getMetadataCollector()" because "this.metadataBuildingContext" is null

Hi Team, I upgraded the spring boot project and hibernate. I use postgresdb and have some entities with ENUM-type columns.

like:-
@Enumerated(EnumType.STRING)
@Column(columnDefinition = "status_enum")
@Type(PostgreSQLEnumType.class)
 private Status status

package -  import com.vladmihalcea.hibernate.type.basic.PostgreSQLEnumType

and on start it is throwing NULL pointer exception. I am sharing stack trace and MVN tree dependency. Please suggest or let me know if I am doing something wrong here.

stack trace:- Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaSharedEM_entityManagerFactory': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:377)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:135)
	at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:688)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:512)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1332)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1162)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:560)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:325)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:323)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:365)
	... 95 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Cannot invoke "org.hibernate.boot.spi.MetadataBuildingContext.getMetadataCollector()" because "this.metadataBuildingContext" is null
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1770)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:598)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:325)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:323)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:365)
	... 107 common frames omitted
Caused by: java.lang.NullPointerException: Cannot invoke "org.hibernate.boot.spi.MetadataBuildingContext.getMetadataCollector()" because "this.metadataBuildingContext" is null
	at org.hibernate.type.spi.TypeConfiguration$Scope.getDialect(TypeConfiguration.java:459)
	at org.hibernate.type.EnumType$LocalJdbcTypeIndicators.getDialect(EnumType.java:516)
	at org.hibernate.type.descriptor.jdbc.VarcharJdbcType.shouldUseMaterializedLob(VarcharJdbcType.java:90)
	at org.hibernate.type.descriptor.jdbc.VarcharJdbcType.resolveIndicatedType(VarcharJdbcType.java:79)
	at org.hibernate.type.descriptor.java.BasicJavaType.getRecommendedJdbcType(BasicJavaType.java:33)
	at org.hibernate.type.descriptor.java.StringJavaType.getRecommendedJdbcType(StringJavaType.java:56)
	at org.hibernate.type.EnumType.configureUsingReader(EnumType.java:180)
	at org.hibernate.type.EnumType.setParameterValues(EnumType.java:142)
	at com.vladmihalcea.hibernate.type.basic.PostgreSQLEnumType.setParameterValues(PostgreSQLEnumType.java:46)
	at org.hibernate.mapping.MappingHelper.injectParameters(MappingHelper.java:111)
	at org.hibernate.mapping.BasicValue.setExplicitCustomType(BasicValue.java:836)
	at org.hibernate.boot.model.internal.BasicValueBinder.fillSimpleValue(BasicValueBinder.java:1363)
	at org.hibernate.boot.model.internal.SetBasicValueTypeSecondPass.doSecondPass(SetBasicValueTypeSecondPass.java:27)
	at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1870)
	at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1815)
	at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:331)
	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1380)
	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1451)
	at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:75)
	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:376)
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409)
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396)
	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:352)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1817)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1766)
	... 114 common frames omitted

mvn tree:- 
+- org.springframework.boot:spring-boot-starter-web:jar:3.1.4:compile
+- org.postgresql:postgresql:jar:42.4.1:runtime
 |  \- org.checkerframework:checker-qual:jar:3.5.0:runtime
+- com.vladmihalcea:hibernate-types-60:jar:2.21.1:compile
 |  +- javax.xml.bind:jaxb-api:jar:2.3.0:compile
 |  \- jakarta.xml.bind:jakarta.xml.bind-api:jar:4.0.1:compile
 |     \- jakarta.activation:jakarta.activation-api:jar:2.1.2:compile
 +- org.hibernate.orm:hibernate-core:jar:6.3.1.Final:compile
 |  +- jakarta.persistence:jakarta.persistence-api:jar:3.1.0:compile
 |  +- jakarta.transaction:jakarta.transaction-api:jar:2.0.1:compile
 |  +- org.jboss.logging:jboss-logging:jar:3.5.3.Final:compile
 |  +- io.smallrye:jandex:jar:3.1.2:runtime
 |  +- com.fasterxml:classmate:jar:1.5.1:compile
 |  +- net.bytebuddy:byte-buddy:jar:1.14.8:runtime
 |  +- org.glassfish.jaxb:jaxb-runtime:jar:4.0.3:runtime
 |  |  \- org.glassfish.jaxb:jaxb-core:jar:4.0.3:runtime
 |  |     +- org.eclipse.angus:angus-activation:jar:2.0.1:runtime
 |  |     +- org.glassfish.jaxb:txw2:jar:4.0.3:runtime
 |  |     \- com.sun.istack:istack-commons-runtime:jar:4.1.2:runtime
 |  +- jakarta.inject:jakarta.inject-api:jar:2.0.1:runtime
 |  \- org.antlr:antlr4-runtime:jar:4.10.1:compile
 +- org.hibernate.common:hibernate-commons-annotations:jar:6.0.6.Final:compile
 +- io.hypersistence:hypersistence-utils-hibernate-62:jar:3.5.3:compile
 |  \- io.hypersistence:hypersistence-tsid:jar:2.1.1:compile
 +- org.hibernate.validator:hibernate-validator:jar:8.0.1.Final:compile
 |  \- jakarta.validation:jakarta.validation-api:jar:3.0.2:compile

Hibernate 6.3 has support for native enums out of the box. Try removing @Type(PostgreSQLEnumType.class)

@beikov Thanks. I tried removing the @Type
and its saying

Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: status_enum = character varying

Well, then stop comparing apples to oranges :wink:

What query are you executing?

It is a simple select query. Query Like SELECT * FROM USER WHERE status = 'ACTIVE' here status is enum type.

Before upgrading it was working fine. We were using <dependency> <groupId>com.vladmihalcea</groupId> <artifactId>hibernate-types-52</artifactId> <version>2.21.1</version> </dependency> which support for PostgreSQLEnumType with Hibernate @TypeDefs

import com.vladmihalcea.hibernate.type.basic.PostgreSQLEnumType;
 @TypeDef(name = "pgsql_enum", typeClass = PostgreSQLEnumType.class)

 @Type(type = "pgsql_enum")
 @Column(name = "status", columnDefinition = "status_enum")
private Status status;

TypeDef is deprecated now.
And regarding the same problem, I found one stack overflow postgresql - Hibernate 6+ Unable to map java enum to postgres enum - Stack Overflow where someone said adding ```

Type(PostgreSQLEnumType.class)


And if I am lowering the hibernate version to 6.2.9. then getting the exception.

`Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [status] in table [user]; found ["status_enum" (Types#VARCHAR)], but expecting [status_enum (Types#OTHER)]`

Your query is wrong and just worked by accident before because prior to Hibernate 6 there was no proper type validation for comparison predicates. Use a query like from User u where u.status = Status.ACTIVE

@beikov Sorry!! My bad.

In our case, tables have - a database-specific Enumerated column type. That is the reason for importing PostgreSQLEnumType.class from hypersistence-utils-hibernate.

Yes, Passing the params(enum - Status.ACTIVE ) to the repository find query the same way you mentioned.
Other queries are working fine, Only queries having an enum field throw that org.postgresql.util.PSQLException: ERROR: operator does not exist: status_enum = character varying

Remove the use of PostgreSQLEnumType, with Hibernate 6.3 you don’t need that anymore. If you fix your query like I showed you, this should work just fine.

1 Like

@beikov ok, I will remove that. I will try Hibernate 6.3 as you said removing PostgreSQLEnumType will work and support our custom enum datatype between the spring application and database communication. Right?

Yes, that is correct

I’m encountering a similar issue with PostgreSQL and Micronaut. Initially, I faced problems using PostgreSQLEnumType, and after removing it, I ran into the status_enum = character varying issue.

In my entity, I annotate the enum with @Enumerated(EnumType.STRING). For the query, I’m using Micronaut’s QuerySpecification<T>. When building the criteriaBuilder, I use the following code:

predicate = criteriaBuilder.equal(root.get(key), value);

Here, key is the column name and value is the enum. Has anyone else experienced this and found a solution?

Please try to create a reproducer with our test case template and if you are able to reproduce the issue, create a bug ticket in our issue tracker and attach that reproducer.