简体   繁体   中英

JDO best practice: Store objects as a collection under their parent or independently?

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 User object 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 User object can I also return just those Transaction objects 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.).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM