Search code examples
springspring-bootjpaspring-data-jpaspring-jdbc

Found 0 JPA repository interfaces after I ran my spring boot application


1. UserRecord

package auj.helpdesk.model;

package auj.helpdesk.model;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class UserRecord {
  @Id
  private int id;
  private String name;
  private String email;

//default conatructor    
  public UserRecord() {
  }

  public int getId() {
      return id;
  }

  public void setId(int id) {
      this.id = id;
  }

  public String getName() {
      return name;
  }

  public void setName(String name) {
      this.name = name;
  }

  public String getEmail() {
      return email;
  }

  public void setEmail(String email) {
      this.email = email;
  }
}

2. UserController

package auj.helpdesk.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import auj.helpdesk.model.UserRecord;
import auj.helpdesk.services.UserService;

import java.util.List;

@RestController
public class UserController {
  @Autowired
  private UserService userService;

  @RequestMapping("/")
  public List<UserRecord> getAllUser() {
      return userService.getAllUsers();
  }

  @RequestMapping(value = "/add-user", method = RequestMethod.POST)
  public void addUser(@RequestBody UserRecord userRecord) {
      userService.addUser(userRecord);
  }
}

3. UserRepository

package auj.helpdesk.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;
import auj.helpdesk.model.UserRecord;
public interface UserRepository extends CrudRepository<UserRecord, String> {
}

4. UserService

package auj.helpdesk.services;

import java.util.List;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import auj.helpdesk.model.UserRecord;
import auj.helpdesk.repository.UserRepository;

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    public List<UserRecord> getAllUsers() {
        List<UserRecord> userRecords = new ArrayList<>();
        userRepository.findAll().forEach(userRecords::add);
        return userRecords;
    }

    public void addUser(UserRecord userRecord) {
        userRepository.save(userRecord);
    }
}

AujhelpdeskApplication

package auj.helpdesk.starter;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AujhelpdeskApplication {

    public static void main(String[] args) {
        SpringApplication.run(AujhelpdeskApplication.class, args);
    }

}

5. Application.properties

spring.datasource.url=jdbc:oracle:thin:@LAPTOP-U9NGFKE9:1521:XE
spring.datasource.username=M1
spring.datasource.password=M1
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
spring.jpa.show-sql=true
server.port=8090

6. pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>auj.helpdesk</groupId>
    <artifactId>aujhelpdesk</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>aujhelpdesk</name>
    <description>A help desk for Auj</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

My Spring Data repository not scanned . I have checked my application.properties but its not working .


Solution

  • Since your AujhelpdeskApplication is in package auj.helpdesk.starter;, Spring boot will only scan packages that start with auj.helpdesk.starter, which means it will skip auj.helpdesk.repository because they are siblings, repository does not lie inside auj.helpdesk.starter package.

    package auj.helpdesk.starter;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class AujhelpdeskApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(AujhelpdeskApplication.class, args);
        }
    
    }
    

    To address this, you either have to move your main class to package auj.helpdesk so that containing package is parent to all, or you have to specify locations where you want components to look into.

    package auj.helpdesk.starter;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication(scanBasePackages = "auj.helpdesk")
    public class AujhelpdeskApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(AujhelpdeskApplication.class, args);
        }
    
    }