Hibernate Data Repositories

Repositories, quickly

A Jakarta Data repository interface exposes a typesafe API for interacting with a datastore. Here’s a quick overview of the kinds of methods you’ll find on a repository interface.

#1 Automatic query methods

In an automatic query method, the return type encodes the queried entity type, and the method parameters match the persistent fields of the entity by type and name.

@Find Optional<Book> byIsbn(String isbn);

The parameter names and types are validated against the entity fields at compilation time.

#2 Annotated query methods

An annotated query method lets us write a query in HQL, JDQL, or SQL. Query parameters are matched to method parameters by name or by position.

@Query("where title like :titlePattern order by publicationDate")
List<Book> byTitle(@NotBlank String titlePattern);

The syntax and typing of the query is completely validated at compilation time.

#3 Lifecycle methods

A lifecycle method inserts, updates, deletes, or upserts a record in the database.

@Save void upsert(Book book);

Calling a lifecycle method causes in an immediate database update.

#4 Resource accessor methods

A resource accessor method exposes the underlying StatelessSession for direct use by the client.

StatelessSession session();

#5 Default methods

A default method is one you implement yourself.

default void refresh(Book book) {
    session().refresh(book);
}

The method may call the resource accessor method to obtain the StatelessSession.

Back to top