How to Use Spring Data JPA: Complete Guide to @Entity, @Repository, and JPA Annotations

2 min read

How to Use Spring Data JPA: Complete Guide to @Entity, @Repository, and JPA Annotations

1. Introduction

Data access is a core requirement in any enterprise application. Spring Boot simplifies database interaction through Spring Data JPA, providing a powerful abstraction that minimizes boilerplate and allows developers to focus on domain logic. In this article, we walk through the key JPA and Spring Data annotations—@Entity, @Table, @Column, @Id, @GeneratedValue, @Repository, and @EnableJpaRepositories—and demonstrate how they work together in a real implementation.

2. Problem

Traditional data access layers can become verbose and error-prone. Developers must:

  • Manually write CRUD SQL
  • Map result sets to Java objects
  • Handle connection management
  • Maintain a large volume of boilerplate DAO code

This slows down development and increases maintenance complexity. To solve this, we need a consistent and simpler way to:

  • Describe relational database table structures
  • Automatically generate SQL
  • Perform CRUD operations through repository interfaces
  • Keep the codebase clean and maintainable

3. Solution

Spring Data JPA solves these challenges by using annotations to express database structure and behavior.

Key Annotations:

  • @Entity — marks a Java class as a JPA entity mapped to a database table
  • @Table — specifies the table name
  • @Column — maps Java fields to table columns
  • @Id — identifies the primary key field
  • @GeneratedValue — auto-generates primary key values
  • @Repository — marks a repository component and integrates exception translation
  • @EnableJpaRepositories — enables scanning and creation of JPA repositories

By combining these annotations, we can define our database schema and CRUD operations with minimal code.

4. Implementation

Below is a complete example demonstrating Spring Data JPA usage from configuration to entity mapping to repository creation.

4.1 Project Setup (Spring Boot)

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

4.2 Application Configuration

@Configuration
@EnableJpaRepositories(basePackages = "com.example.demo.repository")
public class JpaConfig {
}

4.3 Creating an Entity Using JPA Annotations

@Entity
@Table(name = "products")
public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "product_name", nullable = false, length = 150)
    private String name;

    @Column(name = "price", nullable = false)
    private Double price;

    @Column(name = "description")
    private String description;

    public Product() {}

    public Product(String name, Double price, String description) {
        this.name = name;
        this.price = price;
        this.description = description;
    }

    // Getters and setters...
}

Explanation:

  • @Entity makes Product a persistent JPA object
  • @Table(name = "products") maps the class to the table
  • @Id marks the primary key
  • @GeneratedValue auto-generates PK values
  • @Column defines column-level properties

4.4 Creating a Repository

@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
    List<Product> findByName(String name);
}

4.5 Using the Repository in a Service

@Service
public class ProductService {

    private final ProductRepository repo;

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

    public Product saveProduct(Product product) {
        return repo.save(product);
    }

    public List<Product> getAllProducts() {
        return repo.findAll();
    }

    public List<Product> searchByName(String name) {
        return repo.findByName(name);
    }
}

4.6 REST Controller Example

@RestController
@RequestMapping("/products")
public class ProductController {

    private final ProductService service;

    public ProductController(ProductService service) {
        this.service = service;
    }

    @PostMapping
    public Product create(@RequestBody Product product) {
        return service.saveProduct(product);
    }

    @GetMapping
    public List<Product> list() {
        return service.getAllProducts();
    }

    @GetMapping("/search")
    public List<Product> search(@RequestParam String name) {
        return service.searchByName(name);
    }
}

5. Conclusion

Spring Data JPA dramatically simplifies data access by eliminating boilerplate and generating queries dynamically. With only a few core annotations—@Entity, @Table, @Column, @Id, @GeneratedValue, @Repository, and @EnableJpaRepositories—you can define entity models, map them to tables, and work with fully functional repositories. This approach leads to cleaner code, improved maintainability, and faster development.

🤞 Never miss a story from us, get weekly updates to your inbox!

Leave a Reply

Your email address will not be published. Required fields are marked *