简体   繁体   中英

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." All my packages are subpackage of the same package. I don't know why is it happening. 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. 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.
        <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. 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.
@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.

  2. In application.properties file this property is present

spring.jpa.hibernate.ddl-auto=create

Please change this property with value update otherwise on every restart new table will be created

spring.jpa.hibernate.ddl-auto=update

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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