Class SQLTransaction
- All Implemented Interfaces:
Transaction
This class ensures relational database connectivity through transactions. Largely similar to JpaDaoAPI, the difference between them is that transactions of the DbTransaction class do not automatically close. The transaction can either be validated or rolled back. A new instance of DbTransaction creates a new session to the database. Upon any error, the entire transaction is automatically rolled back.
The Java Persistence API, abbreviated as JPA, is a framework for the Java programming language with the purpose to handle relational data.
The JPA implementation used in the JBStrap framework is EclipseLink v2.6.2.
A JPA layer uses entities that can be marked with standard JPA annotations.
In order to access the persistence layer, the parameter JBStrapParamType.PERSISTENCE_UNIT_NAME
must be set.
The parameter expects the attribute persistence-unit name
specified in the file persistence.xml
.
In a query if any field of the entity references an other entity, all values of the detail entity are inserted into the record. Detail entity field names in the record receive a prefix consisting of the detail entity's field name and a dot separates the detail entity name from the detail entity field names. Every detail entity field name is stored as a separate field in the record.
Composite keyEntities can have composite keys: EmbeddedId
, Embeddable
, IdClass
. Any type of primary key may be used in entities and
records. The mapping of composite keys and records is handled.
File handling is done using the persistence layer. File saving and query is implemented through FileHandler
. If no proper
implementation is passed as a parameter to JBStrapParamType.FILE_HANDLER
, the default DefaultFileHandler
performs file
operations.
Files can exclusively be of the format JBFile
in case of data manipulation as well as query. Data manipulation operations and queries
only return files if the DataDescriptor was specified in a parameter, because only then can a field be defined as a file. Entity fields cannot
be marked as files.
Fields of DataDescriptor can be parameterized in order to have multiple values, as is the case with the field DualListBox
.
If a field has multiple values, the field is saved to a cross-reference table specified in a parameter and is also queried automatically
using the cross-reference table. Just as with files, multi-value fields return values only if a DataDescriptor is passed as a parameter for
a method, as these cannot be defined exclusively on an entity level.
JBStrap enables you to create a history of the entities' data manipulation operations. This makes it possible to track the entire life-cycle
of records on the server side. To create the history of an entity, use the annotation @Customizer(HistoryCustomizer.class)
.
- Audit
- SCD
- View mapping
- Foreign key
The DbTransaction class ensures the possibility to perform data manipulation operations and queries in a relational database without having to write SQL scripts.
Queries:
- Query by primary key
- Query by controls parameter-specifiedd controls
- Save
- Update
- Delete
- Execute native SQL script, either query or data manipulation operations
- Instantiate JDBC connection
- Query data in transactions and perform data manipulation operations.
commit
and roleBack
properly handle manage
the life-cycle of both objects.
- Since:
- 4.0
- Author:
- JBStrap
- See Also:
-
Constructor Details
-
SQLTransaction
public SQLTransaction()Instantiates a database transaction class. When a new instance is created, no new connection is opened on the database side, but a new session is opened using the existing connection. This session remains open until the transaction is rolled back or committed to the database.
-
-
Method Details
-
commit
Saves all operations executed in the current transaction instance to the database. Already executed operations cannot be rolled back, the open transaction becomes closed and no further operations are allowed on this transaction instance.- Specified by:
commit
in interfaceTransaction
- Specified by:
commit
in classBaseTransaction<SQLTransaction,
javax.persistence.EntityManager> - Returns:
- A closed instance of database transaction.
- Throws:
ConnectionException
- Thrown if an error occurs in the persistence layer, such as database not accessible, or persistence layer does not exist.
-
rollBack
Rolls back all operations within the current transaction instance. Already performed operations cannot be saved, the open transaction is closed, no further operations are allowed on the current transaction instance.- Specified by:
rollBack
in interfaceTransaction
- Specified by:
rollBack
in classBaseTransaction<SQLTransaction,
javax.persistence.EntityManager> - Returns:
- A closed instance of database transaction.
- Throws:
ConnectionException
- Thrown if an error occurs in the persistence layer, such as database not accessible, or persistence layer does not exist.
-
getTransactionObject
public javax.persistence.EntityManager getTransactionObject()- Specified by:
getTransactionObject
in interfaceTransaction
- Specified by:
getTransactionObject
in classBaseTransaction<SQLTransaction,
javax.persistence.EntityManager>
-