Overview
TheTransaction class represents a financial transaction in the accounting system. Each transaction consists of multiple entries (debits and credits) applied to different accounts. Transactions enforce double-entry bookkeeping rules by default, ensuring that debits equal credits.
Package: com.softwarearchetypes.accounting
Access: Public final class (created via TransactionBuilder)
Constructor
Unique identifier for the transaction
Reference to another transaction (e.g., for reversals). Can be null.
Type/category of transaction (e.g., “payment”, “invoice”, “reversal”)
When the transaction occurred in real time
When the transaction applies for accounting purposes (may differ from occurredAt)
Map of entries to their target accounts
Constraint to validate entries (e.g., balancing constraint)
TransactionBuilder instead of calling this constructor directly.
Properties
id
Returns the unique transaction identifier.The transaction’s unique identifier
refId
Returns the reference transaction ID if this transaction references another.Reference transaction ID for reversals or related transactions, or empty
type
Returns the transaction type.The type/category of this transaction
INITIALIZATION- Opening balancesREVERSAL- Reversal of previous transactionEXPIRATION_COMPENSATION- Compensating expired entries- Custom types: “transfer”, “payment”, “invoice”, etc.
occurredAt
Returns when the transaction occurred in real time.Real-world timestamp of when the transaction occurred
appliesAt
Returns when the transaction applies for accounting purposes.Accounting timestamp determining when this transaction affects balances
accountsInvolved
Returns all accounts affected by this transaction.All accounts that have entries in this transaction
entries
Returns all entries grouped by account.Immutable map of accounts to their entries in this transaction
Methods
execute
Executes the transaction by applying all entries to their accounts.- Adds entries to each involved account
- Updates account balances
- Generates domain events for each entry
AccountingFacade.execute() instead.
Transaction Constraints
Transactions are validated usingTransactionEntriesConstraint implementations:
BALANCING_CONSTRAINT
Ensures that debits equal credits for double-entry accounts.MIN_2_ENTRIES_CONSTRAINT
Ensures at least two entries per transaction.MIN_2_ACCOUNTS_INVOLVED_CONSTRAINT
Ensures at least two different accounts are involved.Creating Transactions
Using TransactionBuilder
The recommended way to create transactions is throughTransactionBuilder:
Simple Transfer
For basic transfers between two accounts:Complex Transaction with Multiple Entries
Transaction with Entry Validity
Entries can have validity periods:Reversing Transactions
To reverse a previously executed transaction:- Automatically creates opposite entries for all original entries
- Sets
refIdto the original transaction ID - Sets type to
REVERSAL - Debits become credits and vice versa
Using ReverseTransactionCommand
Transaction Views
To query transaction details:Related Types
TransactionId
TransactionType
Value object representing transaction types:TransactionView
Read model for querying transaction data:Best Practices
- Use TransactionBuilder: Always create transactions through the builder pattern for proper validation
- Set Meaningful Types: Use descriptive transaction types for better reporting and auditing
-
Include Metadata: Attach relevant business context using
withMetadata() -
Distinguish occurredAt vs appliesAt:
occurredAt: When the event happened in realityappliesAt: When it should affect accounting reports
-
Handle Results: Always check
Resultreturn values for success/failure -
Use Transfers for Simple Cases: The
transfer()method is simpler than building transactions manually - Validate Before Execute: Let constraints validate your entries before execution
Related Classes
- AccountingFacade - Main interface for executing transactions
- Entry - Individual debit/credit entries
- Account - Accounts affected by transactions
TransactionBuilder- Builder for constructing transactions
