Should one place a container structure in a model?
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?
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.
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: