4 Replies Latest reply on Jan 15, 2019 4:18 AM by jorcx.liu

    EAGER CDI with AttributeConverter error

    jorcx.liu

      Hi,

      I want to load some configurable data from database when my application startup, so i use . javax.enterprise.inject.spi.Extension

      BUT

      When i use AttributeConverter with JPA, i got the following exception

      Caused by: javax.persistence.PersistenceException: Error attempting to apply AttributeConverter

              at org.hibernate.type.descriptor.converter.AttributeConverterSqlTypeDescriptorAdapter$2.doConversion(AttributeConverterSqlTypeDescriptorAdapter.java:148)

              at org.hibernate.type.descriptor.converter.AttributeConverterSqlTypeDescriptorAdapter$2.extract(AttributeConverterSqlTypeDescriptorAdapter.java:121)

              at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:261)

              at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:257)

              at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:247)

              at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:333)

              at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:3007)

              at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1746)

              at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1672)

              at org.hibernate.loader.Loader.getRow(Loader.java:1561)

              at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:731)

              at org.hibernate.loader.Loader.processResultSet(Loader.java:990)

              at org.hibernate.loader.Loader.doQuery(Loader.java:948)

              at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:340)

              at org.hibernate.loader.Loader.doList(Loader.java:2689)

              at org.hibernate.loader.Loader.doList(Loader.java:2672)

              at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2506)

              at org.hibernate.loader.Loader.list(Loader.java:2501)

              at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:504)

              at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:395)

              at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:220)

              at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1508)

              at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1537)

              at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1505)

              at org.hibernate.query.Query.getResultList(Query.java:135)

              at org.hibernate.query.criteria.internal.compile.CriteriaQueryTypeQueryAdapter.getResultList(CriteriaQueryTypeQueryAdapter.java:74)

              at org.jboss.as.jpa.container.TypedQueryNonTxInvocationDetacher.getResultList(TypedQueryNonTxInvocationDetacher.java:58)

              at com.me.ta.foundation.service.info.base.impl.JPAAgioLimitService.load(JPAAgioLimitService.java:66)

              at com.me.ta.foundation.service.info.base.impl.JPAAgioLimitService$Proxy$_$$_WeldClientProxy.load(Unknown Source)

              at com.me.ta.foundation.service.info.CachedInfoImpl.load(CachedInfoImpl.java:372)

              at com.me.ta.foundation.service.info.CachedInfoImpl$Proxy$_$$_WeldClientProxy.load(Unknown Source)

              at com.me.ta.foundation.service.info.base.impl.JPABaseInfo.loadInfo(JPABaseInfo.java:234)

              ... 27 more

      Caused by: java.lang.IllegalStateException: org.hibernate.resource.beans.container.internal.NotYetReadyException: CDI BeanManager not (yet) ready to use

              at org.hibernate.resource.beans.container.internal.JpaCompliantLifecycleStrategy$BeanImpl.initialize(JpaCompliantLifecycleStrategy.java:112)

              at org.hibernate.resource.beans.container.internal.CdiBeanContainerExtendedAccessImpl$BeanImpl.initialize(CdiBeanContainerExtendedAccessImpl.java:113)

              at org.hibernate.resource.beans.container.internal.CdiBeanContainerExtendedAccessImpl$BeanImpl.getBeanInstance(CdiBeanContainerExtendedAccessImpl.java:119)

              at org.hibernate.resource.beans.internal.ManagedBeanRegistryImpl$ContainedBeanManagedBeanAdapter.getBeanInstance(ManagedBeanRegistryImpl.java:139)

              at org.hibernate.metamodel.model.convert.internal.JpaAttributeConverterImpl.toDomainValue(JpaAttributeConverterImpl.java:45)

              at org.hibernate.type.descriptor.converter.AttributeConverterSqlTypeDescriptorAdapter$2.doConversion(AttributeConverterSqlTypeDescriptorAdapter.java:140)

              ... 58 more

      Caused by: org.hibernate.resource.beans.container.internal.NotYetReadyException: CDI BeanManager not (yet) ready to use

              ... 64 more

      Caused by: java.lang.NullPointerException

              at org.hibernate.resource.beans.container.internal.JpaCompliantLifecycleStrategy$BeanImpl.initialize(JpaCompliantLifecycleStrategy.java:109)

              ... 63 more

      Thanks

        • 1. Re: EAGER CDI with AttributeConverter error
          manovotn

          Hello,

           

          the exception suggests that you are attempting to use BeanManager too early on.

          What observer method in CDI extension do you use? I would expect this won't work until AfterDeploymentValidation.

          • 2. Re: EAGER CDI with AttributeConverter error
            jorcx.liu

            Hi,Matej

            Thank you for your reply.

            Yes, i use AfterDeploymentValidation observer event. e.g.

            public class EagerCDIExtension implements javax.enterprise.inject.spi.Extension {
                @SuppressWarnings("serial")
                public void validate(@Observes AfterDeploymentValidation event, BeanManager bm) {
                    bm.getBeans(Object.class, new AnnotationLiteral<Eager>() {})
                      .parallelStream()
                      .filter(bean -> bean.getBeanClass().isAnnotationPresent(ApplicationScoped.class))
                      .forEach(
                          bean -> {
                              bm.getReference(bean, bean.getBeanClass(), bm.createCreationalContext(bean)).toString();
                          }
                      );
                }
            }
            

             

            What shoud i use if i want loading data from database after my application started?

            It works when there is no AttributeConverter

            • 3. Re: EAGER CDI with AttributeConverter error
              manovotn

              This sounds more like a Hibernate bug, or a problem in integrating CDI and hibernate bootstrap.

              I have found this Hibernate issue which seems spot on to what you are describing - [HHH-12912] NullPointerException when named query with a converter is compiled before CDI BeanManager is available using… (although I don't know if you are using WFLY).

               

              The issue above has some workarounds mentioned, give it a spin.

              Otherwise, I don't think you can do much from CDI side. Maybe look for some lifecycle event that comes even after CDI's AfterDeploymentValidation, perhaps some event coming from Hibernate that you could hook into? But I am really just guessing here.

              1 of 1 people found this helpful
              • 4. Re: EAGER CDI with AttributeConverter error
                jorcx.liu

                I'm using Wildfly 14.0.1.Final