The Spring Data Repository

Introduction

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. Enter 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

Edit the file, adding in the following contents:

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

Enter the following SQL commands into that file:

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

Next Steps

Now that the data model has been created and you have some sample data, you'll need to create the necessary beans to provide access to that data.