0

Created a new application with CrudRepository and mysql database. I try to launch the application, but the "A component required a bean named 'entityManagerFactory' that could not be found." error falls. I read other people's questions and answers, but I did not find a suitable one for my problem.

build.gradle

plugins {
   id 'java'
   id 'org.springframework.boot' version '2.7.5-SNAPSHOT'
   id 'io.spring.dependency-management' version '1.1.0'
   id "org.hibernate.orm" version "6.1.6.Final"
}

group = 'c'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
   compileOnly {
      extendsFrom annotationProcessor
   }
}

repositories {
   mavenCentral()
   maven { url 'https://repo.spring.io/milestone' }
   maven { url 'https://repo.spring.io/snapshot' }
}

dependencies {
   implementation 'org.springframework.boot:spring-boot-starter-security'
   implementation 'org.springframework.boot:spring-boot-starter-web'
   implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
   implementation group: 'org.springframework.security.oauth', name: 'spring-security-oauth2', version: '2.5.2.RELEASE'
   implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.31'

   implementation group: 'org.json', name: 'json', version: '20220924'

   compileOnly 'org.projectlombok:lombok'
   annotationProcessor 'org.projectlombok:lombok'
}

targetCompatibility = JavaVersion.VERSION_11

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/fgfg
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
spring.jpa.show-sql=false
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true

UserRepository.java

import dataox.backhttpsbitbucket.orgintrolabsystemsfrontamirsharerbiz.entities.UserEntity;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.Optional;

public interface UserRepository extends CrudRepository<UserEntity, Long> {

    @Query("select u from UserEntity u where u.password = ?1 and u.email = ?2")
    Optional<UserEntity> findByPasswordAndEmail(String password, String email);

}

UserEntity.java

import lombok.*;

import javax.persistence.*;
import java.time.LocalDateTime;

@Builder
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Entity
public class UserEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;
    private String name;
    private String industry;
    private String currentCompany;
    private String currentPosition;
    private String photoUrl;
    private String linkedLink;
    private Long linkedId;
    private String phoneNumber;
    private String email;
    private String password;
    private LocalDateTime registeredAt;
    @Enumerated(EnumType.STRING)
    @Column(name = "user_role")
    private UserRole userRole;

Application

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

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

}

I tried fix application.properties with

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

I tried to use JpaRepository instead CrudRepository.

And after all I have this error:

2022-12-20 16:15:41.302  WARN 90827 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository' defined in dataox.backhttpsbitbucket.orgintrolabsystemsfrontamirsharerbiz.repositories.UserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot create inner bean '(inner bean)#602f8f94' 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)#602f8f94': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
2022-12-20 16:15:41.305  INFO 90827 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2022-12-20 16:15:41.316  INFO 90827 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-12-20 16:15:41.330 ERROR 90827 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

A component required a bean named 'entityManagerFactory' that could not be found.


Action:

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


> Task :Application.main() FAILED
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':Application.main()'.
> Process 'command '/opt/idea/jbr/bin/java'' finished with non-zero exit value 1
Shadow
  • 33,525
  • 10
  • 51
  • 64
  • 1
    Need to show also your configuration classes, including the main class with `@SpringBootApplication`. This answer is probably useful, https://stackoverflow.com/a/54663039/8194026 – John Mitchell Dec 20 '22 at 14:27
  • @JohnMitchell sorry, I don't have configuration class. What do you mean by that? I add information about SpringBootApplication. Thank you. – Liudmila Zavyalova Dec 20 '22 at 15:05

1 Answers1

0

Try adding @EnableTransactionManagement to your @SpringBootConfiguration class.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableTransactionManagement
@SpringBootApplication
public class Application {

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

}
Matt Brock
  • 5,337
  • 1
  • 27
  • 26