Skip to main content
PreparationZone Logo
PrepZone

Entities, Repositories, and Queries

JPA entities, Spring Data repositories, and derived query methods.

Pass checkpoint below

Why this matters

  • JPA is the default persistence layer for BookStore — understanding entities and repositories is prerequisite for transactions, relationships, and migrations.
  • Derived query methods eliminate boilerplate SQL for common lookups.
  • Hibernate manages the object-relational mapping so you work with Java objects, not JDBC rows.

Entity basics

L1 · Fresher

@Entity classes map to tables; @Id and @GeneratedValue define primary keys for BookStore rows.

Repository interfaces

L2 · Intermediate

findByTitleContaining style methods are implemented by Spring Data at runtime — no implementation class required.

@Query and projections

L3 · Advanced

Use JPQL or native SQL for reports; interface projections fetch only needed columns for list screens.

ddl-auto in production

L4 · Expert

Never rely on create or update in prod — pair JPA with Flyway migrations and treat schema as versioned code.

Entity mapping

Java
// learn:fragment
@Entity
@Table(name = "books")
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false, length = 255)
    private String title;

    @Column(nullable = false, unique = true, length = 13)
    private String isbn;

    @Column(nullable = false, precision = 10, scale = 2)
    private BigDecimal price;

    @Column(nullable = false)
    private String genre;

    @Column(name = "created_at", updatable = false)
    private Instant createdAt;

    @PrePersist
    void onCreate() {
        this.createdAt = Instant.now();
    }

    protected Book() {}

    public Book(String title, String isbn, BigDecimal price, String genre) {
        this.title = title;
        this.isbn = isbn;
        this.price = price;
        this.genre = genre;
    }

    // getters and setters
}

Essential JPA annotations

  • @Entity — Marks a class as a persistent domain object.
  • @Table — Maps to a specific database table name.
  • @Id + @GeneratedValue — Primary key with auto-increment strategy.
  • @Column — Column constraints: nullable, length, unique, precision.
  • @PrePersist / @PreUpdate — Lifecycle callbacks before insert/update.
Author1
BookN
Order1
Author has many Books. Order has many OrderItems referencing Books.

Repository interface

Java
// learn:fragment
public interface BookRepository extends JpaRepository<Book, Long> {
    List<Book> findByGenre(String genre);
    Optional<Book> findByIsbn(String isbn);
    List<Book> findByTitleContainingIgnoreCase(String keyword);
    List<Book> findByPriceBetween(BigDecimal min, BigDecimal max);
    boolean existsByIsbn(String isbn);
    long countByGenre(String genre);
}

Spring Data parses method names into queries:

Method name → SQL mapping

  • findByGenre → WHERE genre = ?
  • findByTitleContainingIgnoreCase → WHERE LOWER(title) LIKE LOWER(?%
  • findByPriceBetween → WHERE price BETWEEN ? AND ?

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 6 sections

  • Custom JPQL queries
  • @Embeddable value types
  • Native SQL when JPQL is not enough
  • Pagination
  • Configuration
  • Auditing

Test yourself

Answer these before moving on — recall is what makes it stick.