[英]Found 0 JPA repository interfaces
I have created a spring boot application and when tried to run it, it threw "Finished Spring Data repository scanning in 4 ms. Found 0 JPA repository interfaces."我创建了一个 spring 引导应用程序,当尝试运行它时,它抛出“在 4 毫秒内完成 Spring 数据存储库扫描。找到 0 JPA 存储库接口。” All my packages are subpackage of the same package. I don't know why is it happening.
我所有的包都是同一个package的子包。我不知道为什么会这样。 Do help.
帮忙。
` `
package com.newproject.repo;
import com.newproject.entities.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepo extends JpaRepository<User, String> {
}
` `
` `
package com.newproject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication(scanBasePackages = "com.newproject")
@EnableAutoConfiguration
@ComponentScan(basePackages={"com.newproject.controller.UserController", "com.newproject.service.UserService", "com.newproject.service.UserServiceImpl"})
@EnableJpaRepositories(basePackages="com.newproject.repo.UserRepo")
@EnableTransactionManagement
@EntityScan(basePackages={"com.newproject.entities.User", "com.newproject.dto.UserDTO"})
public class NewprojectApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(NewprojectApplication.class, args);
}
}
` `
I have analysed the code and found couple of issues/improvements and finally issue is fixed.我分析了代码并发现了几个问题/改进,最后问题得到解决。 Here are the details.
这是详细信息。
spring-boot-starter-data-jpa
is already present.spring-boot-starter-data-jpa
已经存在。 So please remove this dependency.javax.persistence.*
to jakarta.persistence.*
because you are using Spring Boot 3.x and it Support Jakarta EE 10 with an EE 9 baseline.javax.persistence.*
更改为jakarta.persistence.*
,因为您使用的是 Spring Boot 3.x,它支持具有 EE 9 基线的 Jakarta EE 10。 <dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
NewprojectApplication.java
) only one annotation ( @SpringBootApplication
) is enough.NewprojectApplication.java
) 中,只有一个注释 ( @SpringBootApplication
) 就足够了。 Please remove other annotations.@EnableAutoConfiguration
@ComponentScan(basePackages={"com.newproject.repo.UserRepo", "com.newproject.controller.UserController", "com.newproject.service.UserService", "com.newproject.service.UserServiceImpl"})
@EnableJpaRepositories(basePackages="com.newproject.repo.UserRepo")
@EnableTransactionManagement
@EntityScan(basePackages={"com.newproject.entities.User", "com.newproject.dto.UserDTO"})
User
and there is no @Table
annotation present in entity class so Spring framework trying to create User
table in PostgreSQL database, that is not allowed because User
is reserved word so please use some other table name using @Table
annotation.User
的实体,并且实体 class 中没有@Table
注释,因此 Spring 框架试图在 PostgreSQL 数据库中创建User
表,这是不允许的,因为User
是保留字,因此请使用其他表名使用@Table
注释。@Entity
@Data
@Table(name = "MyUser")
public class User {}
In Main class ( NewprojectApplication.java
), you are extending SpringBootServletInitializer
but not using any methods from parent class so better remove this.在 Main class (
NewprojectApplication.java
) 中,您正在扩展SpringBootServletInitializer
但不使用父 class 中的任何方法,因此最好删除它。
In application.properties
file this property is present在
application.properties
文件中存在此属性
spring.jpa.hibernate.ddl-auto=create
Please change this property with value update
otherwise on every restart new table will be created请使用值
update
更改此属性,否则在每次重新启动时都会创建新表
spring.jpa.hibernate.ddl-auto=update
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.