design - A class modeling a M:N relationship in Java: remember instances or just primary keys? -
i have class lease
modeling relationship between classes customer
, videogame
. pretty simple , straightforward; looks this:
class lease() { private videogame videogame; private customer customer; // etc. public lease(videogame videogame, customer customer) { this.videogame = videogame; this.customer = customer; } }
each of 3 classes represented database table autogenerated index , additionaly leases
table has foreign key reference both customer
, videogame
.
while retrieving lease
database, should i...
- proceed retrieve both
videogame
,lease
instances ingetlease(long id)
method, , store objects inlease
instance or, better
lease
class remember "foreign keys" , should retrieve instance ofcustomer
,videogame
when needed? requirelease
class change toclass lease() { long videogame; long customer; // etc. }
the first approach seems more natural, i'm looking "best practices" kind of advice on this. thank you!
if using orm framework (hibernate, jpa etc) should create classes relationship want, i.e. typically using class-to-class relationship. framwork responsible on creating queries , saving primary keys in db tables.
if not use framework convert keys objects , how create model, using objects in model more natural.
Comments
Post a Comment