繁体   English   中英

找到 0 JPA 存储库接口

[英]Found 0 JPA repository interfaces

在此处输入图像描述

我创建了一个 spring 引导应用程序,当尝试运行它时,它抛出“在 4 毫秒内完成 Spring 数据存储库扫描。找到 0 JPA 存储库接口。” 我所有的包都是同一个package的子包。我不知道为什么会这样。 帮忙。

`

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);
   }

}

`

我分析了代码并发现了几个问题/改进,最后问题得到解决。 这是详细信息。

  1. 此依赖项存在于pom.xml中,但不是必需的,因为spring-boot-starter-data-jpa已经存在。 所以请移除这个依赖。 删除此依赖项后,您需要将实体类中的导入语句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. 在 Main class ( NewprojectApplication.java ) 中,只有一个注释 ( @SpringBootApplication ) 就足够了。 请删除其他注释。
@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. 您创建了名为User的实体,并且实体 class 中没有@Table注释,因此 Spring 框架试图在 PostgreSQL 数据库中创建User表,这是不允许的,因为User是保留字,因此请使用其他表名使用@Table注释。
@Entity
@Data
@Table(name = "MyUser")
public class User {}
  1. 在 Main class ( NewprojectApplication.java ) 中,您正在扩展SpringBootServletInitializer但不使用父 class 中的任何方法,因此最好删除它。

  2. application.properties文件中存在此属性

spring.jpa.hibernate.ddl-auto=create

请使用值update更改此属性,否则在每次重新启动时都会创建新表

spring.jpa.hibernate.ddl-auto=update

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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