Jakarta Data
1.0

Jakarta Data is a programming model for data access.You can use Jakarta Data to represent data with Java objects called entitiesand to define operations on data as methods of a repository interface.A Jakarta Data provider supplies the repository implementation to an application.The data-1.0 feature includes a built-in Jakarta Data provider for relationaldatabase access.

For more information about Jakarta Data, see Jakarta Data.

For the Jakarta Data API reference documentation, see Jakarta Data API documentation.

Enabling this feature

To enable the Jakarta Data 1.0 feature, add the following element declaration into your server.xml file, inside the featureManager element:

<feature>data-1.0</feature>

Examples

Define an entity class

The following example shows a City entity class with Jakarta Persistence annotations:

@Entity
class City {
    @Id
    long id;
    String name;
    String stateName;
    LocalDate founded;
    int population;

    // constructors, getters, setters, and other methods ...
}

Create a repository interface

Define a repository interface that extends CrudRepository to perform database operations on the City entity:

@Repository
interface Cities extends CrudRepository<City, Long> {

    @Find
    Optional<City> named(@By("name") String city,
                         @By("stateName") String state);

    @Find
    @OrderBy("stateName")
    @OrderBy("name")
    List<City> foundedOn(@By("founded") LocalDate foundedOn);

    @Find
    Page<City> inState(@By("stateName") String state,
                       Order<City> sortBy,
                       PageRequest pageReq);

    @Query("""
            UPDATE City
               SET population = ?3
             WHERE city = ?1 AND stateName = ?2
            """)
    boolean setPopulation(String city, String state, int newPopulation);
}

The repository interface includes:

  • Built-in CRUD methods inherited from CrudRepository

  • Custom @Find methods for querying cities by name, founding date, and state

  • A custom @Query method for updating population data

  • Support for pagination and sorting

Use the repository in a Jakarta EE component

Inject and use the repository in Jakarta EE components such as JAX-RS endpoints, servlets, or CDI beans:

@Inject
Cities cities;

void exampleUsage() {
    // Using a built-in CrudRepository method:
    cities.insert(City.of(1, "Rochester", "Minnesota",
                          LocalDate.of(1854, 7, 12),
                          121395));

    // Using a @Find method and another built-in CrudRepository method:
    City city = cities.named("Rochester", "Minnesota").orElseThrow();
    city.population = 123624;
    city = cities.update(city);

    // Using a @Query method:
    cities.setPopulation("Rochester", "Minnesota", 124648);

    // Using a @Find method with a PageRequest
    Order<City> sorts = Order.by(Sort.desc("population"),
                                 Sort.asc("name"));
    Page<City> page1 = cities.inState("Minnesota",
                                      sorts,
                                      PageRequest.ofSize(10));

    // Obtaining a next page
    Page<City> page2 = cities.inState("Minnesota",
                                      sorts,
                                      page1.nextPageRequest());
}

This example demonstrates:

  • Insert operation: Adding a new city using the built-in insert method

  • Query and update: Finding a city by name and state, modifying it, and saving changes

  • Custom query: Updating population using a JPQL query

  • Pagination: Retrieving cities in a specific state with sorting and pagination

  • Navigation: Moving to the next page of results

Standard API packages provided by this feature

  • jakarta.data

  • jakarta.data.exceptions

  • jakarta.data.metamodel

  • jakarta.data.metamodel.impl

  • jakarta.data.page

  • jakarta.data.page.impl

  • jakarta.data.repository

  • jakarta.data.spi

Features that this feature enables

Supported Java versions

  • JavaSE-17.0

  • JavaSE-21.0

  • JavaSE-25.0

  • JavaSE-26.0

Platform Versions

  • jakartaee-11.0

Features that enable this feature

Developing a feature that depends on this feature

If you are developing a feature that depends on this feature, include the following item in the Subsystem-Content header in your feature manifest file.

io.openliberty.data-1.0; type="osgi.subsystem.feature"