You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Target GA release: same release as GA of Jakarta EE 11 in Open Liberty
Operating systems
Does the documentation apply to all operating systems?
Yes
No; specify operating systems: ______
Jakarta Data
Overview of Jakarta Data
Jakarta Data is a programming model for relational and non-relational data access where you define simple Java objects to represent data and you define interfaces for operations on data.
You inject these interfaces into your application and use them to access data.
The implementation is supplied for you by a Jakarta Data Provider.
Concepts in Jakarta Data
Jakarta Data simplifies data access by allowing you to
represent data with simple Java objects (entities), and
define interfaces (repositories) with methods that perform operations on it.
Jakarta Data Providers
Jakarta Data Providers supply the implementation of repositories, which can be injected into applications via CDI (Contexts and Dependency Injection).
Jakarta Data Providers can be third-party products or can be provided by the application server.
Open Liberty includes a built-in Jakarta Data Provider (link) for relational database access that is backed by the built-in Jakarta Persistence Provider, EclipseLink.
Some third-party Jakarta Data providers include Hibernate ORM (for relational data) and Eclipse JNoSQL (for various types of non-relational data).
Jakarta NoSQL entities for non-relational (NoSQL) data.
Jakarta Data Providers might define additional entity models, possibly by defining their own entity annotations, or by defining conventions for supporting unannotated entities, or by some other vendor-specific model.
An entity can optionally have a static metamodel,
typically generated from the entity class by development tooling, which can be used by the repository interface as well as the application in defining and invoking data access operations in a more type-safe manner.
By convention, static metamodel interface classes typically begin with the underscore character, which is followed by the entity class name.
Repository interfaces must be annotated with the Repository
annotation and can optionally inherit from any of the following repository supertypes that are defined by Jakarta Data:
The repository supertypes offer a variety of pre-defined methods for commonly-used data access operations.
The first type parameter to the repository supertypes specifies a primary Entity class that can be assumed for repository methods that do not otherwise specify an Entity class.
The second type parameter to the repository supertypes specifies the type of the Id field that can be assumed for repository methods that do not otherwise specify an Id type.
Repository Interface Example
@RepositorypublicinterfaceCarsextendsBasicRepository<Car, String> {
@InsertCaradd(Carcar);
@OrderBy(_Car.ODOMETER)
@OrderBy(_Car.VIN)
List<Car> findByModelYearGreaterThanEqualAndPriceBetween(intminYear,
floatminPrice,
floatmaxPrice);
@FindPage<Car> search(@By(_Car.MAKE) Stringmanufacturer,
@By(_Car.MODEL) Stringmodel,
@By(_Car.MODELYEAR) intyear,
PageRequestpageRequest,
Order<Car> sortBy);
@Query("UPDATE Car SET price = ?2 WHERE vin = ?1")
booleansetPrice(StringvehicleIdNum, floatnewPrice);
@Query("SELECT price FROM Car WHERE vin = :vehicleId AND price > 0")
Optional<Float> getPrice(@Param("vehicleId") Stringvin);
@Deletebooleanremove(@By(ID) StringvehicleIdNum);
}
When there is only one Jakarta Data Provider that handles the type of entity, it is unnecessary for the Repository to specify which Jakarta Data Provider to use.
However, when multiple Jakarta Data providers coexist in a system and handle the same entity type, the Repository must specify a provider name to disambiguate which Jakarta Data Provider is used.
The built-in Jakarta Data Provider uses the provider name, "Liberty".
It is included in the data-1.0 feature (link) and can be made unavailable (so it does not conflict with a third-party Jakarta Data Provider) by configuring the Liberty server to instead use the dataContainer-1.0 feature (link).
Open Liberty includes a built-in Jakarta Data Provider for relational database access that is backed by the built-in Jakarta Persistence Provider, EclipseLink.
The built-in provider is available when you enable the data-1.0 feature (link) alongside the persistence-3.2 feature (link) or jdbc-4.3 feature or jdbc-4.2 feature (links).
Entity Model
The built-in provider accepts Jakarta Persistence entities or Java Records as entities (link).
Provider Name
When multiple Jakarta Data providers supporting the same types of entities coexist in the system, use the provider name of "Liberty" to explicitly request the built-in Jakarta Data Provider.
The ddlGen binary will create a data definition language (DDL) file for each databaseStore element present on a Liberty server at runtime.
This includes all features on Liberty that utiltize the databaseStore configuration element.
The DDL files generated for Jakarta Data databaseStore elements will be suffixed with _JakartaData.ddl.
This utility will allow database administrators the ability to create or replicate the database schema necessary for the application(s) installed on a
Liberty server using their administrative priviages.
While at the same time limiting the privlages given to Liberty database user(s) to create connections, insert, update, and delete data from tables.
Server Configuration
To use the ddlGen binary you must enable the localConnector-1.0 feature on your server:
After successful execution of the ddlGen binary, a new directory will be created under wlp/usr/<server-name>/ddl/
and will contain a set of DDL files for each databaseStore.
Choosing the Database
The built-in Jakarta Data Provider, which uses the built-in Jakarta Persistence Provider, EclipseLink, can connect to the same databases that EclipseLink supports.
Individual capabilities within Jakarta Data and the entity model are limited by the extent to which EcilpseLink supports each database.
The Default Database
If you do not specify otherwise, you end up with the Jakarta EE Default DataSource, which is java:comp/DefaultDataSource, as the data store for your repository.
The Jakarta EE Default DataSource is not configured by default in Liberty.
To configure it, include configuration of a <dataSource> with id of DefaultDataSource in your server configuration.
CREATETABLECar (
VIN VARCHAR(255) NOT NULL, MAKE VARCHAR(255), MODEL VARCHAR(255),
MODELYEAR INTEGER, ODOMETER INTEGER, PRICE FLOAT,
PRIMARY KEY (VIN)
);
EclipseLink will attempt to create the necessary database tables automatically, which is convenient for development,
but is not efficient for running in production because checking for table existence incurs a lot of overhead and
database admin privileges might be required for table creation.
Refer to the sections on Specifying a DatabaseStore [link] and Specifying a Persistence Unit Reference [link] for information about how to control the table creation settings.
Specifying a DataSource
To specify which DataSource a repository will use, set the dataStore attribute of the Repository annotation to one of:
JNDI name of a resource reference to a DataSource. For example, java:app/env/jdbc/MyDataSourceRef.
JNDI name of a DataSource. This could be from the name value of a DataSourceDefinition or from the jndiName value of a dataSource from server configuration.
The id of a dataSource from server configuration.
When using JNDI names or resource reference names that are scoped to a namespace like java:comp or java:module,
you must ensure that the scope is accessible to the location of the repository interface within the application.
For example, a DataSource in the java:comp scope of an EJB is never accessible to a repository interface
because although a repository interface might exist in an EJB module, the repository interface is not part of any specific EJB.
A good practice when using scoped names is to use java:app to allow the repository interface to be defined in any module of the application.
EclipseLink will attempt to create the necessary database tables automatically, which is convenient for development,
but is not efficient for running in production because checking for table existence incurs a lot of overhead and database admin privileges might be required for table creation.
Refer to the sections on Specifying a DatabaseStore [link] and Specifying a Persistence Unit Reference [link] for information about how to control the table creation settings.
DataSource id Example
This example shows a repository interface that points to the id of a <dataSource> in server configuration.
CREATETABLECar (
VIN VARCHAR(255) FOR MIXED DATA NOT NULL, MAKE VARCHAR(255) FOR MIXED DATA, MODEL VARCHAR(255) FOR MIXED DATA,
MODELYEAR INTEGER, ODOMETER INTEGER, PRICE FLOAT,
PRIMARY KEY (VIN)
);
DataSource Resource Reference Example
This example shows a repository interface that points to a resource reference to a <dataSource> in server configuration.
CREATETABLECar (
VIN VARCHAR(255) NOT NULL, MAKE VARCHAR(255), MODEL VARCHAR(255),
MODELYEAR INTEGER, ODOMETER INTEGER, PRICE FLOAT,
PRIMARY KEY (VIN)
)
Specifying a Persistence Unit Reference
Defining a Persistence Unit gives you more control over the Jakarta Persistence configuration and EcilpseLink specific settings.
To specify which Persistence Unit a repository will use, set the dataStore attribute of the Repository annotation to a Persistence Unit reference.
The following secions provide an example of configuring and using a Persistence Unit reference.
The persistence unit reference can be defined in a Jakarta EE component. This example also defines a data source resource reference which is used by the persistence unit.
The persistence unit is defined according to the Jakarta Persistence specification.
It selects which DataSource to use and determines which entity classes are accessible to repositories.
Here is an example entry in persistence.xml defining a persistence unit that is used by the prior examples,
The jakarta.persistence.schema-generation.database.action property, which is from the Jakarta Persistence specification,
allows you to request the automatic creation and/or removal of database tables.
Values for this setting include: create, drop, drop-and-create, none.
When using JNDI names or resource reference names that are scoped to a namespace like java:comp or java:module,
you must ensure that the scope is accessible to the location of the repository interface within the application.
For example, a Persistence Unit reference in the java:comp scope of an EJB is never accessible to a repository interface
because although a repository interface might exist in an EJB module, the repository interface is not part of any specific EJB.
A good practice when using scoped names is to use java:app to allow the repository interface to be defined in any module of the application.
CREATETABLECar (
VIN VARCHAR(255) NOT NULL, MAKE VARCHAR(255), MODEL VARCHAR(255),
MODELYEAR INTEGER, ODOMETER INTEGER, PRICE FLOAT,
PRIMARY KEY (VIN)
);
Specifying a DatabaseStore
A databaseStore is an abstraction on top of a dataSource which gives you the ability to configure additional aspects related to Jakarta Persistence,
such as table creation and removal, without needing to provide persistence units.
It also ties into to the ddlgen tool, which generates DDL files for a database admin to run manually under the required database privileges.
Point the dataStore attribute of a Repository annotation at the id of a databaseStore element from server configuration.
CREATETABLEJAKARTA.DATACar (
VIN VARCHAR(255) NOT NULL, MAKE VARCHAR(255), MODEL VARCHAR(255),
MODELYEAR INTEGER, ODOMETER INTEGER, PRICE FLOAT,
PRIMARY KEY (VIN)
);
Java Records as Entities
The built-in Jakarta Data Provider allows Java records to be entities.
These entities are not annotated and instead rely on a couple of basic conventions to be interpreted as entities.
The built-in Jakarta Data Provider substitutes a Jakarta Persistence entity for the Java Record.
Thus the entity model for Java Record entities is in effect a subset of the Jakarta Persistence entity model,
with the difference that it is defined by a Java Record rather than entity annotations.
Record Components
The record components are the attributes or fields of the entity.
Persistent Field Types (basic types) that are specified by Jakarta Data can be used.
These include types such as int, Integer, String, UUID, byte[], LocalDateTime, Instant, and enum types.
Some additional types for collections and embeddables can also be used.
Determining the Id
Precedence for determining the Id attribute (which is required), from highest to lowest:
record component name is "id", ignoring case.
record component name ends with "_id", ignoring case.
record component name ends with "Id" or "ID".
record component type is UUID with any name.
Use the same basic types for Ids that you can use with Jakarta Persistence entities.
You can also form composite Ids by using a type that is another Java record whose record components are basic types for Ids that you can use with Jakarta Persistence entities.
Determining the Version
Precedence for determining the version attribute (optional), from highest to lowest:
record component name is "version", ignoring case.
record component name is "_version", ignoring case.
Nullable Attributes
Record components with primitive types are non-nullable. Record component with other types are nullable.
Collection Attributes
Record component types that are instances of java.util.Collection are considered element collections.
Relation Attributes
Record components with other types (including Java record) are considered embeddables.
Like Java record entities, these embeddables are not annotated.
Record Entity Example
public record Rectangle(
Stringid,
Pointposition,
intheight,
intwidth,
longversion) {
publicstatic record Point(
intx,
inty) {
}
}
The static metamodel for a record entities is optional and follows the same rules and conventions as with other types of entities.
The static metamodel is typically generated by tooling.
A Repository that defines queries and other operations on Java Record entities must indicate its use of the Java Record as an entity in one of the following ways:
Inherit from a built-in repository superinterface and specify the Java Record class as its first type parameter.
Instead of inheriting from a built-in repository superinterface, define one or more life cycle methods (annotated with Insert, Delete, Save, or Update, all of which specify the same Java record type as the type of the parameter or as the element type of the collection or array that is the parameter.
Jakarta Persistence does not allow Java records to be entities. The built-in Jakarta Data provider makes this possible by substituting a generated entity class for the record.
... more details such as the reserved entity name
The text was updated successfully, but these errors were encountered:
--- STILL IN PROGRESS BY DEV TEAM. DO NOT START ON DOCUMENTATION FOR THIS YET ---
Feature epic details
Operating systems
Does the documentation apply to all operating systems?
Jakarta Data
Overview of Jakarta Data
Jakarta Data is a programming model for relational and non-relational data access where you define simple Java objects to represent data and you define interfaces for operations on data.
You inject these interfaces into your application and use them to access data.
The implementation is supplied for you by a Jakarta Data Provider.
Concepts in Jakarta Data
Jakarta Data simplifies data access by allowing you to
Jakarta Data Providers
Jakarta Data Providers supply the implementation of repositories, which can be injected into applications via CDI (Contexts and Dependency Injection).
Jakarta Data Providers can be third-party products or can be provided by the application server.
Open Liberty includes a built-in Jakarta Data Provider (link) for relational database access that is backed by the built-in Jakarta Persistence Provider, EclipseLink.
Some third-party Jakarta Data providers include Hibernate ORM (for relational data) and Eclipse JNoSQL (for various types of non-relational data).
Entity Model
Jakarta Data reuses the entity models of Jakarta Persistence
and Jakarta NoSQL.
Jakarta Data Providers might define additional entity models, possibly by defining their own entity annotations, or by defining conventions for supporting unannotated entities, or by some other vendor-specific model.
Jakarta Persistence Entity Example
Static Metamodel
An entity can optionally have a static metamodel,
typically generated from the entity class by development tooling, which can be used by the repository interface as well as the application in defining and invoking data access operations in a more type-safe manner.
By convention, static metamodel interface classes typically begin with the underscore character, which is followed by the entity class name.
Static Metamodel Example
Repositories
Repository interfaces must be annotated with the Repository
annotation and can optionally inherit from any of the following repository supertypes that are defined by Jakarta Data:
The repository supertypes offer a variety of pre-defined methods for commonly-used data access operations.
The first type parameter to the repository supertypes specifies a primary Entity class that can be assumed for repository methods that do not otherwise specify an Entity class.
The second type parameter to the repository supertypes specifies the type of the Id field that can be assumed for repository methods that do not otherwise specify an Id type.
Repository Interface Example
Repository Interface Usage Example
Linking a Repository to a Provider
When there is only one Jakarta Data Provider that handles the type of entity, it is unnecessary for the Repository to specify which Jakarta Data Provider to use.
However, when multiple Jakarta Data providers coexist in a system and handle the same entity type, the Repository must specify a provider name to disambiguate which Jakarta Data Provider is used.
The built-in Jakarta Data Provider uses the provider name, "Liberty".
It is included in the data-1.0 feature (link) and can be made unavailable (so it does not conflict with a third-party Jakarta Data Provider) by configuring the Liberty server to instead use the dataContainer-1.0 feature (link).
Hibernate ORM Provider Name Example
Eclipse JNoSQL Provider Name Example
Built-in Provider Name Example
Built-in Jakarta Data Provider
Overview of Built-in Jakarta Data Provider
Open Liberty includes a built-in Jakarta Data Provider for relational database access that is backed by the built-in Jakarta Persistence Provider, EclipseLink.
The built-in provider is available when you enable the data-1.0 feature (link) alongside the persistence-3.2 feature (link) or jdbc-4.3 feature or jdbc-4.2 feature (links).
Entity Model
The built-in provider accepts Jakarta Persistence entities or Java Records as entities (link).
Provider Name
When multiple Jakarta Data providers supporting the same types of entities coexist in the system, use the provider name of "Liberty" to explicitly request the built-in Jakarta Data Provider.
DDL Generation
The ddlGen binary is provided by Liberty and is found alongside the Liberty installation in
wlp/bin/ddlGen
andwlp/bin/ddlGen.bat
.More detailed documentation on the ddlGen binary can be found at https://www.ibm.com/docs/en/was-liberty/nd?topic=line-generating-data-definition-language
Usage
The ddlGen binary will create a data definition language (DDL) file for each
databaseStore
element present on a Liberty server at runtime.This includes all features on Liberty that utiltize the
databaseStore
configuration element.The DDL files generated for Jakarta Data
databaseStore
elements will be suffixed with_JakartaData.ddl
.This utility will allow database administrators the ability to create or replicate the database schema necessary for the application(s) installed on a
Liberty server using their administrative priviages.
While at the same time limiting the privlages given to Liberty database user(s) to create connections, insert, update, and delete data from tables.
Server Configuration
To use the ddlGen binary you must enable the
localConnector-1.0
feature on your server:Execute
To execute the ddlGen binary first start your sever:
After successful execution of the ddlGen binary, a new directory will be created under
wlp/usr/<server-name>/ddl/
and will contain a set of DDL files for each
databaseStore
.Choosing the Database
The built-in Jakarta Data Provider, which uses the built-in Jakarta Persistence Provider, EclipseLink, can connect to the same databases that EclipseLink supports.
Individual capabilities within Jakarta Data and the entity model are limited by the extent to which EcilpseLink supports each database.
The Default Database
If you do not specify otherwise, you end up with the Jakarta EE Default DataSource, which is
java:comp/DefaultDataSource
, as the data store for your repository.The Jakarta EE Default DataSource is not configured by default in Liberty.
To configure it, include configuration of a
<dataSource>
with id ofDefaultDataSource
in your server configuration.Repository interface:
Server configuration:
Expected DDL File ( ...databaseStore[java.comp.DefaultDataSource]_JakartaData.ddl ):
EclipseLink will attempt to create the necessary database tables automatically, which is convenient for development,
but is not efficient for running in production because checking for table existence incurs a lot of overhead and
database admin privileges might be required for table creation.
Refer to the sections on Specifying a DatabaseStore [link] and Specifying a Persistence Unit Reference [link] for information about how to control the table creation settings.
Specifying a DataSource
To specify which DataSource a repository will use, set the
dataStore
attribute of theRepository
annotation to one of:java:app/env/jdbc/MyDataSourceRef
.name
value of aDataSourceDefinition
or from thejndiName
value of adataSource
from server configuration.id
of adataSource
from server configuration.When using JNDI names or resource reference names that are scoped to a namespace like
java:comp
orjava:module
,you must ensure that the scope is accessible to the location of the repository interface within the application.
For example, a DataSource in the
java:comp
scope of an EJB is never accessible to a repository interfacebecause although a repository interface might exist in an EJB module, the repository interface is not part of any specific EJB.
A good practice when using scoped names is to use
java:app
to allow the repository interface to be defined in any module of the application.EclipseLink will attempt to create the necessary database tables automatically, which is convenient for development,
but is not efficient for running in production because checking for table existence incurs a lot of overhead and database admin privileges might be required for table creation.
Refer to the sections on Specifying a DatabaseStore [link] and Specifying a Persistence Unit Reference [link] for information about how to control the table creation settings.
DataSource id Example
This example shows a repository interface that points to the
id
of a<dataSource>
in server configuration.Repository interface:
Server configuration:
Expected DDL File ( ...databaseStore[ExampleDataSourceID]_JakartaData.ddl ):
DataSource jndiName Example
This example shows a repository interface that points to the
jndiName
of a<dataSource>
in server configuration.Repository interface:
Server configuration:
Expected DDL File ( ...databaseStore[jdbc.ExampleDataSource]_JakartaData.ddl ):
DataSource Resource Reference Example
This example shows a repository interface that points to a resource reference to a
<dataSource>
in server configuration.Repository interface:
Jakarta EE component (defines the resource reference):
Server configuration:
Expected DDL File ( ...databaseStore[java.app.env.jdbc.ExampleDataSourceRef]_JakartaData.ddl ):
DataSourceDefinition Example
This example shows a repository interface that points to the
name
of aDataSourceDefinition
.Repository interface:
Web component or EJB component (defines the DataSourceDefinition):
Expected DDL File ( ...databaseStore[java.comp.jdbc.ExampleDataSourceDef]_JakartaData.ddl ):
Specifying a Persistence Unit Reference
Defining a Persistence Unit gives you more control over the Jakarta Persistence configuration and EcilpseLink specific settings.
To specify which Persistence Unit a repository will use, set the dataStore attribute of the Repository annotation to a Persistence Unit reference.
The following secions provide an example of configuring and using a Persistence Unit reference.
Repository interface
Jakarta EE component
The persistence unit reference can be defined in a Jakarta EE component. This example also defines a data source resource reference which is used by the persistence unit.
Persistence Unit
The persistence unit is defined according to the Jakarta Persistence specification.
It selects which DataSource to use and determines which entity classes are accessible to repositories.
Here is an example entry in persistence.xml defining a persistence unit that is used by the prior examples,
The
jakarta.persistence.schema-generation.database.action
property, which is from the Jakarta Persistence specification,allows you to request the automatic creation and/or removal of database tables.
Values for this setting include:
create
,drop
,drop-and-create
,none
.DataSource in server configuration:
When using JNDI names or resource reference names that are scoped to a namespace like
java:comp
orjava:module
,you must ensure that the scope is accessible to the location of the repository interface within the application.
For example, a Persistence Unit reference in the
java:comp
scope of an EJB is never accessible to a repository interfacebecause although a repository interface might exist in an EJB module, the repository interface is not part of any specific EJB.
A good practice when using scoped names is to use
java:app
to allow the repository interface to be defined in any module of the application.Expected DDL File
File name: ...databaseStore[java.app.env.persistence.ExamplePersistenceUnitRef]_JakartaData.ddl
Content:
Specifying a DatabaseStore
A
databaseStore
is an abstraction on top of adataSource
which gives you the ability to configure additional aspects related to Jakarta Persistence,such as table creation and removal, without needing to provide persistence units.
It also ties into to the ddlgen tool, which generates DDL files for a database admin to run manually under the required database privileges.
Point the
dataStore
attribute of aRepository
annotation at theid
of adatabaseStore
element from server configuration.Repository interface:
Server configuration:
Expected DDL File
File name: ...databaseStore[ExampleDatabaseStore]_JakartaData.ddl
Content:
Java Records as Entities
The built-in Jakarta Data Provider allows Java records to be entities.
These entities are not annotated and instead rely on a couple of basic conventions to be interpreted as entities.
The built-in Jakarta Data Provider substitutes a Jakarta Persistence entity for the Java Record.
Thus the entity model for Java Record entities is in effect a subset of the Jakarta Persistence entity model,
with the difference that it is defined by a Java Record rather than entity annotations.
Record Components
The record components are the attributes or fields of the entity.
Persistent Field Types (basic types) that are specified by Jakarta Data can be used.
These include types such as
int
,Integer
,String
,UUID
,byte[]
,LocalDateTime
,Instant
, and enum types.Some additional types for collections and embeddables can also be used.
Determining the Id
Precedence for determining the Id attribute (which is required), from highest to lowest:
Use the same basic types for Ids that you can use with Jakarta Persistence entities.
You can also form composite Ids by using a type that is another Java record whose record components are basic types for Ids that you can use with Jakarta Persistence entities.
Determining the Version
Precedence for determining the version attribute (optional), from highest to lowest:
Nullable Attributes
Record components with primitive types are non-nullable. Record component with other types are nullable.
Collection Attributes
Record component types that are instances of
java.util.Collection
are considered element collections.Relation Attributes
Record components with other types (including Java record) are considered embeddables.
Like Java record entities, these embeddables are not annotated.
Record Entity Example
The static metamodel for a record entities is optional and follows the same rules and conventions as with other types of entities.
The static metamodel is typically generated by tooling.
Static Metamodel Example
Identifying When a Record is an Entity
A Repository that defines queries and other operations on Java Record entities must indicate its use of the Java Record as an entity in one of the following ways:
Inherit from a built-in repository superinterface and specify the Java Record class as its first type parameter.
Repository with Superinterface Example
Instead of inheriting from a built-in repository superinterface, define one or more life cycle methods (annotated with
Insert
,Delete
,Save
, orUpdate
, all of which specify the same Java record type as the type of the parameter or as the element type of the collection or array that is the parameter.Repository with Life Cycle Methods Example
Example Table for a Record Entity
...
Limitations of Record Entities
Jakarta Persistence does not allow Java records to be entities. The built-in Jakarta Data provider makes this possible by substituting a generated entity class for the record.
... more details such as the reserved entity name
The text was updated successfully, but these errors were encountered: