简体   繁体   English

找到 0 JPA 存储库接口

[英]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.这是详细信息。

  1. This dependency is present in pom.xml but not required because spring-boot-starter-data-jpa is already present.此依赖项存在于pom.xml中,但不是必需的,因为spring-boot-starter-data-jpa已经存在。 So please remove this dependency.所以请移除这个依赖。 After removing this dependency you need to change import statements in entity classes from 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> 
  1. In Main class ( NewprojectApplication.java ) only one annotation ( @SpringBootApplication ) is enough.在 Main class ( 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"})
  1. You created entity with name 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 {}
  1. 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 中的任何方法,因此最好删除它。

  2. In application.properties file this property is presentapplication.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM