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:  

src/main/java/org/acme/quickstart/Fruit.java

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

package org.acme.quickstart;

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

@Entity
public class Fruit {

    @Id
    @GeneratedValue
    private Long id;

    private String name;

    private String color;

    public Fruit() {
    }

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

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.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:
src/main/java/org/acme/quickstart/FruitRepository.java

Add this code:

package org.acme.quickstart;

import org.springframework.data.repository.CrudRepository;

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:  

src/main/resources/import.sql

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:  

src/main/resources/application.properties
And enter the following values:
quarkus.datasource.db-kind=h2
quarkus.datasource.jdbc.url=jdbc:h2:mem:rest-crud
quarkus.datasource.jdbc.driver=org.h2.Driver
quarkus.hibernate-orm.database.generation=drop-and-create
quarkus.hibernate-orm.log.sql=true

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.