JBStrap JPA DAO

In JBStrap, the lowest level of data handling takes place when database data is handled via source code. However, the framework provides higher levels which take this task over from the developer, and automatize data handling. The JPA DAO API offers a simple approach to JPA-based data handling.

JPA DAO API implements all sorts of data handling processes. Every process is compatible with the previously discussed DataRecord and Criteria classes. Query operations can receive the Criteria, and they will return with a DataRecord or a List<DataRecord>. If any error is encountered, developers must implement a handler for it. The JPA DAO is also capable of using DataDescriptor s which means that data structures and data access rights can be easily parameterized. To read more about the JBStrap DataDescriptor, click here .

Databases are made accessible using the JPA layer. The database access can be set in the persistence.xml file, and this is where entities must be listed. The framework uses the EclipseLink 2.6.2. JPA implementation . Please refer to the documentation to see how to create the entities you wish to work with.

Fetch

This functionality is used most often by JPA DAO API to fetch data from the database. This can be done by fetching all data from an entity, or you can use a filter criteria as well as look for something specific using its unique entity ID. The returned data can be further organized. You can specify if you want the full data set or just a part of it.

You can start a fetch based on an entity or DataDescriptor. If a Client object is passed to the fetch method, you can perform this while taking user access rights into account. This means that if the current user has no read rights to the data, the fetch will be unsuccessful, and it will return with an error.

General fetches (through an entity):

Fetching every data through an entity:
Fetching every data through an entity, while checking for access rights:
Fetching the data records, that meet the specified filters, through an entity (fetching every valid DataRecord):
Fetching every data record, that meets the specified filters, through an entity while checking for access rights:
Fetching the first 50 data records through an entity that meets the specified filters (Every record that has the column1 value of 100. If there are more than 50, only the first 50 will be fetched):
Getting the next 50 data records (51-100), while checking for access rights:
Getting the first 50 data records that meet the specified criteria, and ordering it descending order by column2:
Fetching the same data record, while checking for access rights, and ordering it, descending by column2 and ascending by column3:

General fetches (through a DataDescriptor ):

Fetching every data through a DataDescriptor:
Fetching every data record through a DataDescriptor, while checking for access rights:
Fetching every data record that meets the specified Criteria, through a DataSource (fetching every valid Record):
Fetching every valid data record, while checking for access rights, through a DataSource:
Fetching the first 50 data records through a DataDescriptor that meets the specified filters (Every record that has the column1 value of 100. If there are more than 50, only the first 50 will be fetched):
Getting the next 50 data records (51-100), while checking for access rights:
Getting the first 50 data records that meet the specified criteria, and ordering it descending order by column2:
Fetching the same data record, while checking for access rights, and ordering it, descending by column2 and ascending by column3:

Fetching using the unique ID:

Fetching the data record that corresponds to ID 23, from an entity:
Fetching the data record that corresponds to ID 23, from an entity, while checking for access rights:
Fetching the data record that corresponds to ID 23, from a DataDescriptor:
Fetching the data record that corresponds to ID 23, from a DataDescriptor, while checking for access rights:

Counting rows in a data source:

Counting the data record rows in an entity:
Counting the data record rows in an entity, while checking for access rights:
Counting the data record rows in an entity that meet the specified criteria (has the column1 value of 100):
Counting the data record rows in an entity that meet the specified criteria while checking for access rights:
Counting the data record rows in a DataDescriptor:
Counting the rows in a DataDescriptor, while checking for access rights:
Counting the rows in a DataDescriptor that meet the specified criteria (has the column1 value of 100):
Counting the rows in a DataDescriptor that meet the specified criteria while checking for access rights:

Save

A DataRecord in the memory can be saved to the database by using the JPA DAO API layer. If the primary key is present in the DataRecord and a DataRecord exists also in the database, the existing one will be overwritten with the one being saved. If no primary key value exists in the data record to be saved or the specified value does not exist in the database, a new value will be committed to the database.

If the save operation results in an error, data in the database will be left unchanged and an error message appears. You can also implement access control for a save operation. If a user has no access rights to write the data, the save operation will return with an error and data will be left unchanged.

Saving a data record to an entity:
Saving a data record to an entity with access control:
Saving multiple data records to an entity at the same time:
Saving multiple data records to an entity at the same time, using access control:
Saving a data record to a data source:
Saving a record to a data descriptor with access control:
Saving multiple data records to a data descriptor with access control:
Saving multiple data records to a data descriptor with access control:

Remove

JPA DAO API allows you to delete a record from a database. The prerequisite for this is that the data record must have a unique ID. Except for the unique ID, the API does not use any other values from the data record. Therefore, it is sufficient to use the unique ID to reference the record.

You can even use the API to delete a record. If an exception is thrown during the delete operation, the record will not be deleted. It remains unchanged in the database and an error message appears.

Deleting a record from an entity:
Deleting multiple records from an entity at the same time:
Deleting a record from a DataDescriptor:
Deleting multiple records from a DataDescriptor:

Related pages