使用CrudRepository和mysql数据库创建了一个新的应用程序。我尝试启动该应用程序,但**“A组件需要一个名为”entityManagerFactory“的bean,但无法找到。"**福尔斯我阅读了其他人的问题和答案,但没有找到适合我的问题的。
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;
应用
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);
}
}
我尝试用以下代码修复application.properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
我尝试使用JpaRepository而不是CrudRepository。
毕竟我有这个错误:
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
1条答案
按热度按时间l2osamch1#
尝试将@EnableTransactionManagement添加到
@SpringBootConfiguration
类中。