Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider defining a bean named 'entityManagerFactory' in your configuration. #10

Closed
sathishkumarkk opened this issue Mar 11, 2018 · 49 comments

Comments

@sathishkumarkk
Copy link

sathishkumarkk commented Mar 11, 2018

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.0.RELEASE)

2018-03-11 16:15:22.152  INFO 12204 --- [           main] hello.Application                        : Starting Application on Sathish-Lenovo-PC with PID 12204 (C:\Users\SathishKumar\Documents\workspace-sts-3.9.2.RELEASE\gs-accessing-data-mysql\complete\target\classes started by SathishKumar in C:\Users\SathishKumar\Documents\workspace-sts-3.9.2.RELEASE\gs-accessing-data-mysql\complete)
2018-03-11 16:15:22.152  INFO 12204 --- [           main] hello.Application                        : No active profile set, falling back to default profiles: default
2018-03-11 16:15:22.183  INFO 12204 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6295d394: startup date [Sun Mar 11 16:15:22 IST 2018]; root of context hierarchy
2018-03-11 16:15:23.096  INFO 12204 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$5d19f29e] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-03-11 16:15:23.451  INFO 12204 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-03-11 16:15:23.478  INFO 12204 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-03-11 16:15:23.479  INFO 12204 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.28
2018-03-11 16:15:23.483  INFO 12204 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_45\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jdk1.8.0_45/bin/../jre/bin/server;C:/Program Files/Java/jdk1.8.0_45/bin/../jre/bin;C:/Program Files/Java/jdk1.8.0_45/bin/../jre/lib/amd64;C:\Program Files (x86)\Skype\Phone\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Java\jdk1.8.0_45\bin;C:\Program Files\MySQL\MySQL Utilities 1.6\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Users\SathishKumar\AppData\Local\Microsoft\WindowsApps;;C:\WINDOWS\System32;;.]
2018-03-11 16:15:23.591  INFO 12204 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-03-11 16:15:23.591  INFO 12204 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1408 ms
2018-03-11 16:15:23.716  INFO 12204 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-03-11 16:15:23.716  INFO 12204 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-03-11 16:15:23.716  INFO 12204 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-03-11 16:15:23.716  INFO 12204 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-03-11 16:15:23.716  INFO 12204 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-03-11 16:15:23.778  WARN 12204 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mainController': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Cannot create inner bean '(inner bean)#344561e0' 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)#344561e0': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
2018-03-11 16:15:23.778  INFO 12204 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2018-03-11 16:15:23.794  INFO 12204 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-03-11 16:15:23.841 ERROR 12204 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field userRepository in hello.MainController required a bean named 'entityManagerFactory' that could not be found.


Action:

Consider defining a bean named 'entityManagerFactory' in your configuration.
@dsyer
Copy link
Collaborator

dsyer commented Mar 11, 2018

Works for me. What did you do exactly?

@sathishkumarkk
Copy link
Author

sathishkumarkk commented Mar 11, 2018

I cloned and created database in mysql with the same user name and password. Then I started my application with Run As -> Spring Boot App. @dsyer May I know what would cause such an application failure?

@dsyer
Copy link
Collaborator

dsyer commented Mar 11, 2018

May I know what would cause such an application failure?

The error diagnostic is quite clear ("hello.MainController required a bean named 'entityManagerFactory'"). If you don't have one you aren't running the same app as me.

Which code are you running exactly? Maybe clear your Maven cache and try again to be on the safe side?

@helsonxiao
Copy link

I had the same error. After deleting all repos under .m2 folder and redownloading. It works.

@dsyer
Copy link
Collaborator

dsyer commented Mar 28, 2018

I think it must be a bad jar file.

@dsyer dsyer closed this as completed Mar 28, 2018
@paritoshh
Copy link

I am also facing a similar issue, however deleting .m2 and redownload didn't work for me.
I am still investigation it, I will update once I get root cause of my issue.

@kullov
Copy link

kullov commented Dec 5, 2018

How about your project? I have the same issue!

@praful09534
Copy link

I have exactly the same issue and tried all the possible solutions mentioned on the net. Still, the issue is not fixed. Please suggest a proper solution. Any help would be highly appreciated.

@polaroi8d
Copy link

any update?

@dsyer
Copy link
Collaborator

dsyer commented Jan 6, 2019

There is no issue in the code for this guide. If you have a bad jar the best thing to do is delete it and let it be downloaded again.

@joker7blue
Copy link

It's simple, i have solved my issue..
Add this dependency in your pom.xml

<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>5.2.3.Final</version> </dependency>

@janarrdhan
Copy link

janarrdhan commented Mar 4, 2019

still i have the issue,please help me.

Description:

Field userRepository in com.b2b.services.security.CustomAppUserDetailsService required a bean named 'entityManagerFactory' that could not be found.

The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean named 'entityManagerFactory' in your configuration.

@photoTed
Copy link

Not sure if it helps or is the same issue: I was getting the same error in a project with two manually configured datasources and inadvertently had both identified as @primary. Once I removed that annotation from one, the error went away.

@GanDigit
Copy link

If you have a Wrapper class for Id field in your entity class, convert that into primitive.

--------------------- Wrong ---------------------
@entity
public class Customer {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@Column(name = "name")
private String name;

--------------------- Correct ---------------------
@entity
public class Customer {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@Column(name = "name")
private String name;

@petertogara
Copy link

whats the difference there?

@sajis79
Copy link

sajis79 commented Aug 17, 2020

None of the above comments are working. #petertogara difference is Long -> long (Long's premitive)!

@amitsingh6764
Copy link

I have same issue.any Idea how to solve?

@muthu1712
Copy link

Same issue here.

@petertogara
Copy link

None of the above comments are working. #petertogara difference is Long -> long (Long's premitive)!

ok, let me test and see if that works.

@petertogara
Copy link

Same issue here.

change Long to long and see as suggested by @GanDigit

@muthu1712
Copy link

I had two datasources defined in my code, but missed to specify which should be primary one. Once i added the @primary annotation, the issue was fixed.
Thanks anyway @petertogara :)

@pMan
Copy link

pMan commented Oct 15, 2020

If you are using 'spring-boot-starter-jdbc' and 'javax.persistence-api' in your pom by mistake, replace them with 'spring-boot-starter-data-jpa'. Just in case.

@raviredddy5209
Copy link

Please help with the issue

@dsyer
Copy link
Collaborator

dsyer commented Dec 28, 2020

#10 (comment)

@Manyat5
Copy link

Manyat5 commented Mar 9, 2021

i have meet the same issue.I finally found that entityManagerFactory will auto create in an abstract class JpaBaseConfiguration, and its implementation HibernateJpaConfiguration have an annotation @ConditionalOnSingleCandidate,which means if you inject two DataSource bean, entityManagerFactory will not auto created.

@chenlangping
Copy link

I had the same error. After deleting all repos under .m2 folder and redownloading. It works.

It worked for me. But which jar file is the bad jar? I want to know.

@bondress
Copy link

org.hibernate hibernate-entitymanager 5.2.3.Final

This was very helpful. Thanks @joker7blue

@joker7blue
Copy link

org.hibernate hibernate-entitymanager 5.2.3.Final

This was very helpful. Thanks @joker7blue

Your welcome

@akm1007
Copy link

akm1007 commented May 7, 2021

deleting spring folder from my .m2 & updating repository helped me to solve this issue. This seems to be an issue due to multiple verison of Spring data JPA starter.

@sheetalj2205
Copy link

I had the same error. After deleting all repos under .m2 folder and redownloading. It works.

hey! how to delete all and install them again?

@wanisramdani
Copy link

@sheetalj2205 u'll find the folder in C:\Users<YOUR PC NAME>.m2

@rpcabrera
Copy link

I had two datasources defined in my code, but missed to specify which should be primary one. Once i added the @primary annotation, the issue was fixed. Thanks anyway @petertogara :)

It worked for me! In my case I'm using two different datasources, but as I'm defining them with JpaRepositories I need to configure manually (using Bean annotation of course) in a configuration class the Entity Manager (EM) and the Transaction Manager (TM). So the problem in my case is that the EM & TM should be marked with @primary annotation too, not only the Datasource.

@AbeethaHeshan
Copy link

AbeethaHeshan commented Oct 2, 2022

I got the same problem . I used an appropriate hibernate version through the maven repository (try other version of hibernate core) and delete target folder and rerun

@tminard
Copy link

tminard commented Dec 25, 2022

If you are using 'spring-boot-starter-jdbc' and 'javax.persistence-api' in your pom by mistake, replace them with 'spring-boot-starter-data-jpa'. Just in case.

I was using spring-boot-starter-web and adding spring-boot-starter-data-jpa resolved the issue for me

@Renuroyal
Copy link

i had followed all the above methods but my issue not resolved please help me

@Kacem300
Copy link

i had followed all the above methods but my issue not resolved please help me (feb 2023 )

@Rustic5i
Copy link

@primary

Мне помог ответ https://stackoverflow.com/a/54663039
То есть аннатация с именем @bean(name="entityManagerFactory")

@Musdon
Copy link

Musdon commented Mar 1, 2023

I think the issue from from defects in the .m2 repository where maven stores all its files. So i was able to solve this problem by running these commands in the following order:
mvn package
mvn install
mvn dependency:resolve

if perhaps due to a network glitch, your maven gets corrupted, you can follow the following steps:
mvn package -U
mvn install -U
mvn dependency:purge-local-repository

if you follow these steps carefully, i believe the issue shoud be resolved.

@karthikbalaji022
Copy link

In My case, I had the wrong version of spring-boot-starter-data-jpa, I had version 3.0.3 setup before and in my new project I used version below 3.0.3, eclipse was throwing a warning, and removing the version from pom.xml solved the issue for me.

@vinay-keshava
Copy link

was facing the same issue downgrading spring boot version from 3.0.4 to 3.0.2 worked

@salakanavinda
Copy link

I had same issue when have two custom data sources with flyway DB migration. After adding @primary annotation for one data source, it worked.

@m4fl4
Copy link

m4fl4 commented Mar 29, 2023

I resolve it here.
On my case, the problem was in the DTO And Controller class, in annotation @notblank and @Valid.
By a mistake im import this annotations from jakarta, and the correct is from javax.validation.constraints...
After fix it the project run correctly

@strangervn
Copy link

still i have the issue,please help me.

Description:

Field adminRepository in com.ecommerce.library.service.impl.AdminServiceImpl required a bean named 'entityManagerFactory' that could not be found.

The injection point has the following annotations:

  • @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean named 'entityManagerFactory' in your configuration.

@StreletsA
Copy link

I had two annotations: @EnableJpaRepositories and @EnableR2dbcRepositories. (I changed jpa dependency to r2dbc)
I deleted @EnableJpaRepositories and project run correctly.

@Garoradua
Copy link

I'm also facing this same issue, please help me.

@aagamjtdev
Copy link

Still facing the same issue

@Buzzardo
Copy link
Contributor

Buzzardo commented Jan 10, 2024

Just to summarize the solutions from the various comments:

  1. Delete your spring directory in .m2 and rebuild.
  2. Make sure that one and only one data source is marked as @primary.
  3. Make sure you don't have both @EnableJpaRepositories and @EnableR2dbcRepositories.
  4. Make sure you have both spring-boot-starter-web and adding spring-boot-starter-data-jpa.
  5. Make sure you have a dependency for Hibernate and that it is the correct version (probably the latest, but you may have to adjust it).
  6. Change Long to long (note the capitalization - you want the primitive type).

NOTE that your problem could be caused by any one of these, so do not implement them all at once. Rather, implement each one and test again. If none work, start trying them in combination.

Sorry we can't give a simple "Do this and be done" answer. The code in the guide works as written, but there are a lot of ways to go wrong once you start to customize it for your needs, so there's no single good answer.

@Minakshi-RG
Copy link

org.springframework.boot spring-boot-starter-data-jpa
	use this dependency without declaring the version, it will automatically create the bean entityManagerFactory and thus resolve the issue

@raghu-raman-btech
Copy link

raghu-raman-btech commented Aug 6, 2024

i had nothing to do with entity as of now , so removing JPA implementation worked for me.

//implementation("org.springframework.boot:spring-boot-starter-data-jpa")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests