The Spring Data Repository

The Spring Data repository abstraction simplifies dealing with data models by reducing the amount of boilerplate code required to implement data access layers for various persistence stores (such as JPA). @Repository and its sub-interfaces like @CrudRepository are the central concept in Spring Data which are marker interfaces to provide data manipulation functionality for the entity class that is being managed. When the application starts, Quarkus configures the required persistence technologies and provides an implementation for the interfaces used.

Creating the Model

The first step is to create the data model. Create a new file at:  


The object will be a simple POJO representing details about a type of fruit. Copy the following into

package org.acme.quickstart;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

public class Fruit {

    private Long id;

    private String name;

    private String color;

    public Fruit() {

    public Fruit(String name, String color) { = name;
        this.color = color;

    public Long getId() {
        return id;

    public void setId(Long id) { = id;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getColor() {
        return color;

    public void setColor(String color) {
        this.color = color;

Creating the Spring Data Repository

The next step is to create the data repository. Create a new file at the following location:

Add this code:

package org.acme.quickstart;


import java.util.List;

public interface FruitRepository extends CrudRepository<Fruit, Long> {

    List<Fruit> findByColor(String color);


The key feature here is the CrudRepository base class, which provides a complete set of methods to manipulate the entity (count(), findAll(), delete(), and others).

Creating Sample Data

Finally, we need some sample data to work with. Create and open a new file to hold the sample data at:  


Copy this sample data which will be used to populate our database when the app runs:

INSERT INTO fruit(id, name, color) VALUES (nextval('hibernate_sequence'), 'cherry', 'red');
INSERT INTO fruit(id, name, color) VALUES (nextval('hibernate_sequence'), 'orange', 'orange');
INSERT INTO fruit(id, name, color) VALUES (nextval('hibernate_sequence'), 'banana', 'yellow');
INSERT INTO fruit(id, name, color) VALUES (nextval('hibernate_sequence'), 'avocado', 'green');
INSERT INTO fruit(id, name, color) VALUES (nextval('hibernate_sequence'), 'strawberry', 'red');
Note: Although you are using import.sql to initialize the database in your application, you shouldn't use this in production. Instead, review suggested uses of Hibernate ORM in production.

Configuring Quarkus

The last step is to configure the application with database connection settings. Edit the file:  

And enter the following values:

That’s it! Now you have a database, domain model (Fruit) and a repository (FruitRepository) to retrieve the domain model, and some sample data.

NOTE Although we are using import.sql to initialize our database in our app, you shouldn't use this in production. Instead, review suggested uses of Hibernate ORM in production.

Next Steps

We'll create some injectable Spring Beans that will give us access to the data using the Spring DI annotations.

Daniel Oh
Daniel Oh
Senior Principal Developer Advocate
Daniel Oh is a Senior Principal Developer Advocate at Red Hat. He works to evangelize building cloud-native microservices and serverless functions with cloud-native runtimes to developers. He also continues to contribute to various open-source cloud projects and ecosystems as a Cloud Native Computing Foundation (CNCF) ambassador for accelerating DevOps adoption in enterprises. Daniel also speaks at technical seminars, workshops, and meetups to elaborate on new emerging technologies for enterprise developers, SREs, platform engineers, and DevOps teams.