JDO practice: store objects as a collection under parents or on their own?

I have a class User and Transaction

Each Transaction logically belongs to a User . But I may need a query for a subset of transactions (for example: return all transactions for user A with Transaction.type=1 )

In SQL, I just maintain the Transaction.userID field, which binds it to the User table.

  • In the world of JDO objects, should I do the same? Store Transaction objects separately from the pointer field to the identifier of the User object? Or should I just query the appropriate user object and subquery for transactions with type = 1 (for example)?
  • If I request only for the User object, can I also return only those Transaction objects that are of interest for this request (as in the previous example with bullets)?
+4
source share
1 answer

IMHO, there is no such thing as best practice in general . However, referring to a user with transactions in an object-oriented context, I would model the user to have a list or set of transactions with each transaction that has a link to its user object.

That way you can either get all the user’s transactions by simply getting the user’s object and then getting the list of transactions.

On the other hand, you can request transactions for a specific user, limited to a specific type. Since each transaction has an association with a user object, you always get the correct context, included "for free."

Of course, you should consider parameters such as lazy and impatient loading, depending on what happens to the objects after they are restored (they are used in the process or they are serialized and transferred to the remote process, etc.) ..

+1
source

Source: https://habr.com/ru/post/1337929/


All Articles