• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Should one place a list of models inside another model?

 
Ranch Hand
Posts: 54
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Should one place a container structure in a model?

For example,

AModel {

   List<BModel> listB;
}

In terms of OO this makes sense. But when working with models and databases will I run into problems as AModel and BModel have their own tables?  For instance, when I get AModel from a database do I just leave listB empty, or in order to completely retrieve the object do I also get all of BModel objects that are associated with AModel? However, sometimes I might just want to get AModel and not the list of BModels referencing AModel. Right now,
I am considering deleting the list from AModel. This would then better reflect the database schema in which AModel has a 1-to-many relationship with BModel. I feel that it would be a more elegant approach. What do you think?

 
Saloon Keeper
Posts: 13005
281
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a relational database, one-to-many relationships are usually implemented by a column inside the 'many'. In object oriented code, they are usually implemented by a collection inside the 'one'. Let the database be the database and the code the code. If you are going to write code that completely mimics the database, you're going to end up with a mess that's not object oriented, and you might as well write your application in SQL completely.

Many persistence frameworks implement data to model conversions through proxy objects. When you retrieve a row from the database, it is converted to an automatically generated subclass of your model. For one-to-many relationships, it contains a special collection implementation that will only retrieve a row from the database when you query the collection, to avoid serializing the complete database when you're only interested in one row.

I recommend spending some time on a JPA tutorial. You only have to declare your intentions using annotations on your data model, and JPA will take care of a lot of the nitty gritty implementation details.
 
William Ng
Ranch Hand
Posts: 54
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After doing research and considering it thoughtfully, I agree with your remarks completely. Even thought I was considering modifying the model, my OO intuition told me otherwise. And thanks to you, I know that my intuition was spot on. So I will leave it as is. My design is not overly complicated, but, if future requirements dictates, I will look into using JPA. Thanks.
 
I would challenge you to a battle of wits, but I see you are unarmed - shakespear. Unarmed tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic