Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error creating bean with name 'entityManagerFactory' defined in class path resource : Invocation of init method failed

People also ask

How do I fix Error creating bean with name entityManagerFactory?

Error creating bean with name 'entityManagerFactory' defined in class path resource. Resolved this error by adding a Bean to the main application class.

How do I use entityManagerFactory in spring boot?

The complete example of getting EntityManager using the custom configuration in Spring Boot. Open eclipse and create maven project, Don't forget to check 'Create a simple project (skip)'click on next. Fill all details(GroupId – entitymanager, ArtifactId – entitymanager and name – entitymanager) and click on finish.

What is Entity Manager Factory?

Several entity manager factories can be prepared for connecting to different data stores. JPA EntityManager is used to access a database in a particular application. It is used to manage persistent entity instances, to find entities by their primary key identity, and to query over all entities.


I would start by adding the following dependency:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.1.4.Final</version>
</dependency>

and

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

UPDATE: Or simply add the following dependency.

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
</dependency>

People using Java 9 include this dependency:

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
</dependency>

Adding dependencies didn't fix the issue at my end.

The issue was happening at my end because of "additional" fields that are part of the "@Entity" class and don't exist in the database.

I removed the additional fields from the @Entity class and it worked.

Goodluck.


I suspect that the jar files of hibernate-core and hibernate-entitymanager dependencies are corrupted or were not installed properly on your machine.

I suggest that you just delete the folders named hibernate-core and hibernate-entitymanager from your Maven local repository and Maven will reinstall them.

The default location for Maven local repository is C:\Documents and Settings\[USERNAME]\.m2 in windows or ~/.m2 in Linux/Mac.


I've jdk-12.0.2.jdk, 've found solution to the problem, add dependencies to pom.xml:

<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.javassist/javassist -->
<dependency>
    <groupId>org.javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.25.0-GA</version>
</dependency>

In my case, deleting any of the below annotations cause the error message 'entityManagerFactory' to show, for example.

 @Id
  @GeneratedValue(strategy=GenerationType.AUTO)

or

@ManyToMany(targetEntity=listOfObject_x.class)

The error message disappears after adding the missing annotation(s).

package mypackage_unameit;
import javax.persistence.PrePersist;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import lombok.Data;

@Data
@Entity
public class Someclasss {

  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private Long id;

  @NotNull
  @Size(min=5, message="Name must be at least 5 characters long")
  private String name;

  private Date createdAt;

  @ManyToMany(targetEntity=listOfObject_x.class)
  @Size(min=1, message="You must choose at least 1 ingredient")
  private List<listOfObject_x>   = new ArrayList<>();

  @PrePersist
  void createdAt() {
    this.createdAt = new Date();
  }
}