We're updating the issue view to help you get more done. 

Hibernate setting up AttributeConverters before EntityManagerFactory is available under Wildfly

Description

The EntityManagerFactory is needed to build a Repository which is being injected into the AttributeConverter. e.g.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public class ExampleAttributeConverter implements AttributeConverter<Attribute, Long> { public ExampleRepository exampleRepository; // constructor injection public ExampleAttributeConverter(ExampleRepository exampleRepository){ this.exampleRepository = exampleRepository; } @Override public Long convertToDatabaseColumn(Attribute attribute) { // perform operation with repository } @Override public Attribute convertToEntityAttribute(Long dbData) { // perform operation with repository } }

This triggers the following exception:

1 2 3 4 5 6 7 Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'exampleRepository': Cannot create inner bean '(inner bean)#59fc0247' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#59fc0247': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'entityManagerFactory': FactoryBean which is currently in creation returned null from getObject Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#59fc0247': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'entityManagerFactory': FactoryBean which is currently in creation returned null from getObject Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'entityManagerFactory: FactoryBean which is currently in creation returned null from getObject"}, "WFLYCTL0412: Required services that are not installed:" => ["jboss.undertow.deployment.default-server.default-host./ExampleService"], "WFLYCTL0180: Services with missing/unavailable dependencies" => undefined } }

I am using the following configuration to provide Hibernate the set of SpringBeans for CDI:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory(ConfigurableListableBeanFactory beanFactory) { final LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean(); bean.getJpaPropertyMap().put(AvailableSettings.BEAN_CONTAINER, new SpringBeanContainer(beanFactory)); bean.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); bean.setJtaDataSource(dataSource()); bean.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver()); bean.setPackagesToScan(new String[] { "com.example.test" }); final Properties props = new Properties(); props.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect"); //props.setProperty("hibernate.show_sql", "true"); props.setProperty("hibernate.format_sql", "true"); props.setProperty("hibernate.transaction.coordinator_class", "jta"); props.setProperty("hibernate.transaction.jta.platform", "org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform"); bean.setJpaProperties(props); return bean; }

If I try a non-constructor injection approach in my ExampleAttributeConverter e.g.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class ExampleAttributeConverter implements AttributeConverter<Attribute, Long> { @Autowired public ExampleRepository exampleRepository; @Override public Long convertToDatabaseColumn(Attribute attribute) { // perform operation with repository } @Override public Attribute convertToEntityAttribute(Long dbData) { // perform operation with repository } }

Then exampleRepository is never set and is constantly null when the AttributeConverter methods are invoked.

Environment

Spring: 5.1.7.Release
Wildfly: 10.1.0-Final
Hibernate: 5.3.7.Final

Status

Assignee

Unassigned

Reporter

Rooshan Aslam

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

5.3.7

Priority

Major