Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To store different Java engine types in a NoSQL database, persist an explicit type discriminator alongside each engine’s data, map that discriminator to concrete Java classes, and use a persistence converter to bridge the Java object and the database representation. A Jakarta NoSQL example applies this pattern to a Machine document whose engine can be either gas or electric.

What polymorphism means in this example

Here, polymorphism is about mapping Java subtype instances to and from stored JSON. Application code can refer to an abstract Engine type while individual documents retain the information needed to identify a concrete subtype. The pattern is not a claim that document databases are inherently better than relational databases; it is one way to represent and query a model with multiple engine variants.

Otavio Santana’s July 26, 2024, tutorial demonstrates the pattern with Jakarta NoSQL, JSON-B, Helidon, and Oracle NoSQL. Read the tutorial.

How the Java-to-document mapping works

Give the document an engine field

The sample’s Machine entity has an ID, an engine field, a manufacturer, and a year. The engine field is declared using the abstract Engine type and marked with a custom converter. That converter forms the persistence boundary: it translates between the Java field and the form the provider stores.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Record the subtype with a discriminator

The abstract Engine class uses JSON-B type metadata to name a property type and associate aliases with concrete classes: gas maps to GasEngine, and electric maps to ElectricEngine. A stored engine object can therefore carry a marker such as "type": "gas" or "type": "electric". When JSON-B reads the object, that marker identifies which subtype to instantiate; Java code can still work with the shared Engine abstraction.

Keep the converter provider-aware

The example separates subtype-aware JSON binding from persistence integration. JSON-B handles the subtype metadata, while the converter connects the Java value to the provider. The tutorial notes that a provider’s concrete representation may differ—for example, it could be a string, a Map<String, Object>, or BSON. Do not assume all Jakarta NoSQL providers use the same representation or converter behavior. See the tutorial’s converter and mapping example.

Query and retrieve by engine type

A discriminator can do more than guide deserialization: it can also be a queryable document property. The sample repository uses a query equivalent to:

from Machine where engine.type = :type

The parameter value selects the desired subtype, such as gas or electric. The REST resource in the example provides operations to list machines, retrieve one by ID, save a machine, and fetch machines by engine type. Sample payloads include the type marker and engine data such as horsepower; those payload values illustrate the schema rather than report real vehicle measurements. The query and resource examples are in the tutorial.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Run the tutorial’s local sample

The tutorial’s configuration uses a document database named machines, an Oracle NoSQL endpoint at http://localhost:8080, and a Helidon server port of 8181. It runs Oracle NoSQL Community Edition in Docker. The linked sample repository specifies JDK 21 for its build and run instructions and gives these commands:

  1. Build the project: mvn package

  2. Run the packaged application: java -jar target/helidon.jar

These are the tutorial and repository’s sample instructions, not a guarantee of compatibility across every release of Jakarta NoSQL, Helidon, Oracle NoSQL, or their drivers. Check the versions and provider setup for the project you choose. The sample and its README are at helidon-oracle-json-types.

Check the API and database versions you choose

Jakarta NoSQL is an API standard for applications using NoSQL databases; it is not itself a database engine. The Eclipse Foundation’s specification page lists Jakarta NoSQL 1.0 as available and 1.1 as under development (page accessed September 30, 2026). Verify that your chosen API release, provider implementation, driver, and database version work together before adapting the sample. See the Jakarta NoSQL specification page.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Oracle’s product overview describes support for JSON, table, and key-value data types, as well as on-premises and cloud deployment; Oracle describes its Cloud Service as fully managed. The tutorial itself is local-first, so deploying beyond its Docker setup requires separate configuration for the chosen environment. See Oracle NoSQL Database Technical Overview.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Decide whether this document pattern fits your model

A discriminator-based document model is a practical option, not a universal replacement for relational inheritance or other persistence designs. Weigh the requirements that shape the application:

The cited tutorial and sample demonstrate a mapping and query approach; they do not report a performance benchmark or establish that document storage is the right choice for every polymorphic model.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.