0
0
Spring Bootframework~5 mins

Service calling repository in Spring Boot

Choose your learning style9 modes available
Introduction

Services call repositories to get or save data. This keeps code organized and easy to manage.

When you want to separate business logic from data access.
When you need to fetch data from a database in a clean way.
When you want to update or delete data safely.
When you want to reuse data access code in many places.
When you want to write tests for business logic without touching the database.
Syntax
Spring Boot
public class ServiceName {
    private final RepositoryName repository;

    public ServiceName(RepositoryName repository) {
        this.repository = repository;
    }

    public ReturnType someMethod() {
        return repository.someRepositoryMethod();
    }
}

Service classes use constructor injection to get repository instances.

Repository methods handle database operations like find, save, delete.

Examples
This service calls the repository to find a user by ID.
Spring Boot
public class UserService {
    private final UserRepository userRepository;

    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public User findUserById(Long id) {
        return userRepository.findById(id).orElse(null);
    }
}
This service gets all products by calling the repository's findAll method.
Spring Boot
public class ProductService {
    private final ProductRepository productRepository;

    public ProductService(ProductRepository productRepository) {
        this.productRepository = productRepository;
    }

    public List<Product> getAllProducts() {
        return productRepository.findAll();
    }
}
Sample Program

This example shows a BookService calling BookRepository to get a book by its ID. The repository handles database access, and the service uses it to get the book data.

Spring Boot
import org.springframework.stereotype.Service;
import java.util.Optional;

@Service
public class BookService {
    private final BookRepository bookRepository;

    public BookService(BookRepository bookRepository) {
        this.bookRepository = bookRepository;
    }

    public Book getBookById(Long id) {
        return bookRepository.findById(id).orElse(null);
    }
}

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface BookRepository extends JpaRepository<Book, Long> {
}

import jakarta.persistence.Entity;
import jakarta.persistence.Id;

@Entity
public class Book {
    @Id
    private Long id;
    private String title;

    public Book() {}

    public Book(Long id, String title) {
        this.id = id;
        this.title = title;
    }

    public Long getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}
OutputSuccess
Important Notes

Always use constructor injection for better testability and immutability.

Services should not contain database code directly; repositories handle that.

Use Optional from repository methods to handle missing data safely.

Summary

Services call repositories to separate business logic from data access.

Use constructor injection to provide repositories to services.

Repositories handle database operations; services use them to get or save data.