Error creating bean with name ‘entityManagerFactory’ defined in class path resource

Alexandre Antonio Barbosa
2 min readDec 30, 2018

I was facing the following problem and I fixed when I realize that I was mapping the Entity OneToOne wrongly.

This was the problem:

@OneToOne(mappedBy=”Event”, orphanRemoval=true, cascade=CascadeType.PERSIST)
private Actor actor;

@OneToOne(mappedBy=”Event”, orphanRemoval=true, cascade=CascadeType.PERSIST)
private Repo repo;

and this was causing the following problem:

> org.springframework.beans.factory.BeanCreationException: Error
> creating bean with name ‘entityManagerFactory’ defined in class path
> resource
> [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]:
> Invocation of init method failed; nested exception is
> org.hibernate.AnnotationException: Unknown mappedBy in:
> com.hackerrank.vanhack.model.Event.actor, referenced property unknown:
> com.hackerrank.vanhack.model.Actor.Event at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1745)
> ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576)
> ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
> ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] at
> org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
> ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] at
> org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
> ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] at
> org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
> ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] at
> org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
> ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] at
> org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083)
> ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE] at
> org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:853)
> ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE] at
> org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
> ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE] at
> org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)
> ~[spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at
> org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
> [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at
> org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
> [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at
> org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
> [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at
> org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
> [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at
> org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
> [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at
> com.hackerrank.vanhack.VanhackApplication.main(VanhackApplication.java:10)
> [classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native
> Method) ~[na:1.8.0_131] at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
> ~[na:1.8.0_131] at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> ~[na:1.8.0_131] at java.lang.reflect.Method.invoke(Method.java:498)
> ~[na:1.8.0_131] at
> org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
> [spring-boot-devtools-2.1.1.RELEASE.jar:2.1.1.RELEASE] Caused by:
> org.hibernate.AnnotationException: Unknown mappedBy in:
> com.hackerrank.vanhack.model.Event.actor, referenced property unknown:
> com.hackerrank.vanhack.model.Actor.Event at
> org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:168)
> ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final] at
> org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1696)
> ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final] at
> org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1664)
> ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final] at
> org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:287)
> ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final] at
> org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:904)
> ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final] at
> org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:935)
> ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final] at
> org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57)
> ~[spring-orm-5.1.3.RELEASE.jar:5.1.3.RELEASE] at
> org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365)
> ~[spring-orm-5.1.3.RELEASE.jar:5.1.3.RELEASE] at
> org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:390)
> ~[spring-orm-5.1.3.RELEASE.jar:5.1.3.RELEASE] at
> org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:377)
> ~[spring-orm-5.1.3.RELEASE.jar:5.1.3.RELEASE] at
> org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341)
> ~[spring-orm-5.1.3.RELEASE.jar:5.1.3.RELEASE] at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1804)
> ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1741)
> ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] … 21 common frames
> omitted

I have fixed when I change to

@OneToOne(mappedBy=”event”, orphanRemoval=true, cascade=CascadeType.PERSIST)
private Actor actor;

@OneToOne(mappedBy=”event”,orphanRemoval=true, cascade=CascadeType.PERSIST)
private Repo repo;

This issue has already in StackOverflow but I could not share the solution because of my reputation.

I will test my Entitities in a few next hours but the problem was fixed

I hope you have enjoyed.

Alexandre

--

--

Alexandre Antonio Barbosa

Sênior Java Developer, triathlete, father and married. No alcohol, no smoke and I am also addicted to challenges.