Home > other >  Spring boot repository not found
Spring boot repository not found

Time:06-19

This is my service class

package com.example.demo.service;
import com.example.demo.dto.Invoicedto;
import com.example.demo.model.Item;
import com.example.demo.repository.ItemRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class InvoiceService implements InvoiceServiceInterface {
    @Autowired
    private ItemRepository itemRepository;
    public void createInvoice(Invoicedto invoiceDto) {
        try {
            List<Item> itemList = this.itemRepository.findAll();
            for (Item item : itemList) {
                System.out.println(item.getId());
            }
        } catch (Exception e) {
            System.out.println("Exception occurred "   e.getMessage());
        }
    }
}

this is my repository

package com.example.demo.repository;
import com.example.demo.model.Item;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Repository
public interface ItemRepository   extends JpaRepository<Item, Long> {

    List<Item> findAll();

    @Modifying
    @Query("DELETE Item c WHERE c.id = ?1")
    void deleteById(long id); 
}

Though I added necessary annotations this always give

Exception occurred Cannot invoke "com.example.demo.repository.ItemRepository.findAll()" because "this.itemRepository" is null

Why is this happening what is the fix?

CodePudding user response:

Maybe Spring Component Scan is not finding your repository bean due to your package structure.

Make sure the main class with the @SpringBootApplication annotation is in this package: package com.example.demo;

CodePudding user response:

Here two things I can see in your code which are not required.

    1. List<Item> itemList = this.itemRepository.findAll(); 
   

to

    List<Item> itemList = itemRepository.findAll();
  1. As you are extending JpaRepository no need to declare findAll() and deleteById() methods in repository interface. So please change as below and try once.

    public interface ItemRepository extends JpaRepository<Item, Long> {
    
    }
    

References: findAll deleteById

CodePudding user response:

You can try to manually specify directory of your repositories with: @EnableJpaRepositories("some.root.package") //path to repository package

CodePudding user response:

1)List<Item> findAll();
you don't have to write this line inside the repository you will get this from JPA JpaRepository<ClassName,Long> Interface;.

2)List<Item> getAllItem();this line declare inside your service package 

3) List<Item> itemList = this.itemRepository.findAll();
            for (Item item : itemList) {
                System.out.println(item.getId());}
and here you don't have to user this before item itemRepository.findAll().
you can directly use  List<Item> itemList = itemRepository.findAll();
  • Related