Annotates a repository interface to be implemented by the container/runtime.
This class is a CDI bean-defining annotation when CDI is available.
Regardless of whether CDI or custom dependency injection is used, the
repository implementation must be made available to applications via the
jakarta.inject.Inject annotation.
For example,
@Repository
public interface Products extends DataRepository<Product, Long> {
@Find
@OrderBy("price")
List<Product> namedLike(@By("name") @Is(Like.class) String namePattern);
@Query("UPDATE Product SET price = price - (price * ?1) WHERE price * ?1 <= ?2")
int putOnSale(float rateOfDiscount, float maxDiscount);
...
}
@Inject
Products products;
...
found = products.namedLike("%Printer%");
numUpdated = products.putOnSale(0.15f, 20.0f);
The module Javadoc provides an overview of Jakarta
Data.
-
Optional Element Summary
Optional Elements -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringValue for theprovider()attribute that allows the use of any available Jakarta Data provider that supports the type of entity annotation that is present on the repository's entity class.static final StringValue for thedataStore()attribute that indicates to use a default data store.
-
Field Details
-
ANY_PROVIDER
Value for theprovider()attribute that allows the use of any available Jakarta Data provider that supports the type of entity annotation that is present on the repository's entity class.- See Also:
-
DEFAULT_DATA_STORE
Value for the
dataStore()attribute that indicates to use a default data store.When running in a Jakarta EE profile or platform and the entity annotations indicate a relational database, the default data store is the Jakarta EE default data source,
java:comp/DefaultDataSource. Otherwise, the default data store is determined by the Jakarta Data provider.The default data store might require additional vendor-specific configuration, depending on the vendor.
- See Also:
-
-
Element Details
-
dataStore
String dataStoreOptionally indicates the data store to use for the repository.
Precedence for interpreting the
dataStorevalue is as follows, from higher precedence to lower precedence.- If running in an environment where Jakarta Config is available and the value is found in Jakarta Config, an error is raised. Interoperability with Jakarta Config is reserved for future versions of Jakarta Data.
- If running in an environment where JNDI is available and the
dataStorevalue has the prefixjava:, the Jakarta Data provider attempts to look up the value as a name in JNDI. If the resource found in JNDI is compatible with the Jakarta Data provider and the entities accessed by theRepository, then the resource becomes the data store for theRepository. For relational data access, thedataStorevalue can be:- the JNDI name of a
DataSource, - the name of a
jakarta.annotation.sql.DataSourceDefinition, - the JNDI name of a resource reference to a
DataSource, - the JNDI name of a
jakarta.peristence.EntityManagerFactory, or - the JNDI name of a persistence unit reference.
- the JNDI name of a
- Otherwise, if the entities used by the
Repositoryindicate a relational database and thedataStorevalue matches the name of a persistence unit, then the correspondingjakarta.peristence.EntityManagerFactorybecomes the data store for theRepository. Precedence for matching a persistence unit name is as follows, from higher precedence to lower precedence:an identically named persistence unit defined in the same module as the
Repository. The persistence unit is represented in JNDI asjava:module/persistence/{dataStore-value}/EntityManagerFactoryan identically named persistence unit defined in the same application as the
Repository. The persistence unit is represented in JNDI asjava:app/persistence/{dataStore-value}/EntityManagerFactory
- Otherwise, the value serves as an identifier linking to vendor-specific configuration for the Jakarta Data provider to interpret in a vendor-specific way. Refer to the documentation of the Jakarta Data provider.
The default value of this attribute is
DEFAULT_DATA_STORE.- Returns:
- the name of a data store or
DEFAULT_DATA_STORE.
- Default:
""
-
provider
String providerRestricts the repository implementation to that of a specific Jakarta Data provider.
This is useful when multiple Jakarta Data providers support the same type of entity annotation, in which case the provider attribute clarifies which Jakarta Data provider must be used. Jakarta Data providers must ignore
Repositoryannotations that indicate a different provider's name as the provider.The default value of this attribute is
ANY_PROVIDER, allowing the use of any available Jakarta Data provider that supports the type of entity annotation that is present on the repository's entity class.- Returns:
- the name of a Jakarta Data provider or
ANY_PROVIDER.
- Default:
""
-