Skip to content
Advertisement

A bean named ‘entityManagerFactory’ that could not be found

I’m pretty new to Hibernate/JPA. Running the application, I’m getting this error:

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

The main class (I use @EnableJpaRepositories to let Spring find the repository in other package):

@SpringBootApplication
@EnableJpaRepositories
public class RunApp {

    public static void main(String[] args) {
        SpringApplication.run(RunApp.class, args);
    }
}

My main entity class:

@Entity(name = "User")
@Table(name = "users")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {

    @Id
    @GeneratedValue
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

A repository:

@Repository
public interface UserRepository extends CrudRepository<User, Long> {

}

A properties file:

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/mydbtest
spring.datasource.username=root
spring.datasource.password=123123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

Maven dependencies:

<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-jpa</artifactId>
  <version>2.7.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <version>2.7.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>8.0.29</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.18.24</version>
  <scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api -->
<dependency>
  <groupId>javax.persistence</groupId>
  <artifactId>javax.persistence-api</artifactId>
  <version>2.2</version>
</dependency>

What should I do here?

Advertisement

Answer

I solved this adding the following dependency instead of spring-data-jpa:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
  <version>2.7.1</version>
</dependency>

After that, I got this exception:

Error creating bean with name 'entityManagerFactory' defined in class path resource

But simply solved it by adding this dependency:

<dependency>
  <groupId>javax.xml.bind</groupId>
  <artifactId>jaxb-api</artifactId>
  <version>2.3.0</version>
</dependency>
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement