I have a User and Transaction class
Each Transaction logically belongs to a User. But I may need to query for some subset of Transactions (ex: return all Transactions for User A with Transaction.type=1)
In SQL I just maintain a Transaction.userID field that links it with the User table.
- In JDO’s world of objects should I do the same? Store Transaction objects separate with a pointer-field to the
Userobject ID? Or should I just query for the appropriate User object and sub-query for transactions with type=1 (for example)? - If I query just for the
Userobject can I also return just thoseTransactionobjects that are of interest for the given query (as in the previous bullets example)?
IMHO, there is no such thing as a best practice in general. However, regarding a user with transactions in an object-oriented context, I’d model the user to have a list or set of transactions with each transaction having a reference to its user object.
This way you can either get all transactions of a user, simply by getting the user object and then getting the list of transactions from there.
On the other hand you can query for transactions for a specific user restricted to a specific type. As each transaction has an association to the user object, you always get the right context included “for free”.
Of course, you should consider settings like lazy vs. eager loading as well depending on what happens with the entities after they have been retrieved (are they used in-process or are they serialized and transferred to a remote process etc.).