JBStrap SQL DataDescriptor
SQL DataDescriptors allow you to handle data stored in an SQL database. This data descriptor also uses custom SQL statements to read and update data, so it has native data access. Native data access enables you to use unique syntactic elements and functions of the database, otherwise hidden from developers by the JPA layer.
Native data access also means that there are no entity classes for the data descriptor. When creating a data descriptor, you have to specify the SQL statements for a given operation. If you don't specify an SQL statement for an operation, that operation cannot be used with the data descriptor.
To create a data descriptor, you have to specify the following SQL statements.
Select statement (selectSQL)
Use this setting to specify the SQL statement to be executed by the data descriptor to fetch the data. The set of records returned by this query is the entire data set known to the data descriptor. Search criteria specified for the data descriptor can only narrow this data set down, but cannot expand it.
Example:
Insert statement (insertSQL)
Use this setting to specify the SQL statement to be used by the data descriptor if a new record is inserted into the database. In the SQL statement, you can use the columns in the data descriptor and reference them in the statement after a : character.
Example:
Update statement (updateSQL)
Use this setting to specify the SQL statement to be executed by the data descriptor when updating a record. In the SQL statement, use the columns listed in the data descriptor. You can reference data descriptor columns by inserting a : character before the column name.
Example:
Delete statement (deleteSQL)
Use this setting to specify the SQL statement to be executed when a record is deleted. In the SQL statement, you can also use the columns listed in the data descriptor. You can reference a data descriptor column by inserting a : character before its name.
Example:
You can also create this data descriptor from an XML file or you can create it programmatically.
Creating a simple SQL DataDescriptor with an XML structure:
Programmatically creating an SQL DataDescriptor that corresponds to the above XML:
You can perform any sort of data handling operation by using the SQL DataDescriptor: reading data from the database, updating and removing existing data.
Fetching all records from the database by using a data descriptor:
Fetching all records satisfying the specified criteria using a data descriptor:
Fetching a record using its primary key:
Saving a record to the database using a data descriptor:
Removing a record from the database using a data descriptor:
Data descriptors also support transaction handling. To handle a transaction, you have to specify an SQLTransaction for the DataDescriptor.
Fetching a record, updating and saving it within a single transaction: