back to all blogsSee all blog posts

Jakarta Data preview update in Open Liberty 23.0.0.4-beta

image of author
Michal Broz on Apr 18, 2023
Post available in languages:

Open Liberty 23.0.0.4-beta updates the Jakarta Data preview with an option to combine multiple ways to specify ordering and sorting.

The Open Liberty 23.0.0.4-beta includes the following beta features (along with all GA features):

A Very Early Preview of Jakarta Data (Beta 2)

In Open Liberty 23.0.0.2-beta, we introduced a very early preview of Jakarta Data, which aims to standardize the popular Data Repository pattern across relational and NoSQL databases.

This Jakarta Data beta update allows you to combine multiple ways of specifying ordering and sorting, defining a precedence. Sorting that is defined by the OrderBy annotation or query-by-method keyword is applied first, followed by Sort parameters on the method or Pageable.

Building on from the code snippet provided in the original preview, the following example uses both OrderBy and Sort:

public class Product { // entity
    public long id;
    public String name;
    public float price;
}

@Repository
public interface Products {
    Product findById(long productId);

    @OrderBy("price")
    List<Product> findByNameContains(String searchFor);

    @OrderBy("price")
    Page<Product> findByNameIgnoreCaseContains(String searchFor, Pageable pageRequest);

    @Query("UPDATE Product o SET o.price = o.price - (?2 * o.price) WHERE o.id = ?1")
    boolean discount(long productId, float discountRate);

    void save(Product p);
}

public class MyServlet extends HttpServlet {
    @Inject
    Products products;

    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // Find all matching:
        List<Product> all = products.findByNameContains(searchFor);

        // Request only the first page, ordered by price, then name, then id:
        Pageable pageRequest = Pageable.size(20).sortBy(Sort.asc("name"), Sort.asc("id"));
        Page<Product> page1 = products.findByNameIgnoreCaseContains(searchFor, pageRequest);
    }
}

Jakarta Data has not yet defined a standard configuration model. For now, the Open Liberty beta reuses the existing defaultDatabaseStore configuration for Jakarta Data, which defaults to the DefaultDataSource data source. As a result, the beta of Jakarta Data is currently limited to a single data source. This of course will change as the specification defines more of a configuration model. The data-1.0 Liberty beta feature makes the Jakarta Data API available, and you also need a feature for Jakarta Persistence or JDBC (or at some point in the future, Jakarta NoSQL).

Example server.xml configuration:

<server>
  <featureManager>
    <feature>data-1.0</feature>
    <feature>persistence-3.1</feature> <!-- could use jdbc-4.3 instead -->
    <feature>servlet-6.0</feature> <!-- the example includes a servlet -->
  </featureManager>

  <dataSource id="DefaultDataSource">
    <jdbcDriver libraryRef="PostgreSQL"/>
    <properties.postgresql databaseName="testdb" serverName="localhost" portNumber="5432"/>
    <containerAuthData user="dbuser1" password="dbpwd1"/>
  </dataSource>

  <library id="PostgreSQL">
    <file name="${shared.resource.dir}/jdbc/postgresql.jar"/>
  </library>

  <!-- optional because the following are all defaulted: -->
  <databaseStore id="defaultDatabaseStore" createTables="true" dropTables="false"
          tablePrefix="WLP" dataSourceRef="DefaultDataSource"/>
</server>

You can find the 1.0.0-b2 beta release of Jakarta Data on Maven, which corresponds to the Open Liberty 23.0.0.4 beta.

<dependency>
  <groupId>jakarta.data</groupId>
  <artifactId>jakarta-data-api</artifactId>
  <version>1.0.0-b2</version>
</dependency>

For more information, refer to the the draft of the Jakarta Data specification and the corresponding Javadoc.

Your feedback is welcome on all of the Jakarta Data features and will be helpful as the specification develops further. Let us know what you think and/or be involved directly in the specification on github.

Try it now

To try out these features, update your build tools to pull the Open Liberty All Beta Features package instead of the main release. The beta works with Java SE 20, Java SE 17, Java SE 11, and Java SE 8.

If you’re using Maven, you can install the All Beta Features package using:

<plugin>
    <groupId>io.openliberty.tools</groupId>
    <artifactId>liberty-maven-plugin</artifactId>
    <version>RELEASE</version>
    <configuration>
        <runtimeArtifact>
          <groupId>io.openliberty.beta</groupId>
          <artifactId>openliberty-runtime</artifactId>
          <version>23.0.0.4-beta</version>
          <type>zip</type>
        </runtimeArtifact>
    </configuration>
</plugin>

You must also add dependencies to your pom.xml file for the beta version of the APIs that are associated with the beta features that you want to try. For example, for Jakarta Data Beta 2, you would include:

<dependency>
  <groupId>jakarta.data</groupId>
  <artifactId>jakarta-data-api</artifactId>
  <version>1.0.0-b2</version>
</dependency>

Or for Gradle:

dependencies {
    libertyRuntime group: 'io.openliberty.beta', name: 'openliberty-runtime', version: '[23.0.0.4-beta,)'
}

Or if you’re using container images:

FROM icr.io/appcafe/open-liberty:beta

Or take a look at our Downloads page.

For more information on using a beta release, refer to the Installing Open Liberty beta releases documentation.

We welcome your feedback

Let us know what you think on our mailing list. If you hit a problem, post a question on StackOverflow. If you hit a bug, please raise an issue.