I am writing a Java program using the MVC design pattern.
I have classes Item
and Supplier
. In the database they are connected through a item_supplier
table.
I’m writing a method which will give me all suppliers for a specific item (using itemID):
public ArrayList<Supplier> getItemSuppliers(int itemID)
I have a DB layer as well and I have DBItem
and DBSupplier
. Where should this method go? I will use it only (mostly) on my ItemUI
so I am thinking of DBItem
as the correct place.
—
Usually when we have the SalesLineItem pattern (Sales * – 1 SalesLineItem 1 – * Item) we have a separate class, but in that case, do I need such as my only interaction with that table (item_supplier
) will be with this printing (and one updating) method?
Basically, do I need to do a ItemSupplier
model layer class and respectively DBItemSupplier
or can I just have those two methods getItemSuppliers
and updateItemSuppliers
on either DBItem
or DBSupplier
(and if the latter, where?)
3
Putting that method on your DBItem class is probably the right thing to do. I assume that the DBItem class encapsulates all the queries necessary to receive the data associated with an item, in which case this is the correct thing to do.
As for whether you need the joining table in your Many-to-Many relationship, probably not. You usually do not need to represent this joining table in your model layer, it is enough to have Item.Suppliers
and Supplier.Items
collections on your model classes.
EDIT
I should point out, that there is a fairly simple question to ask, in order to decide whether to include the joining table at your model layer: “is this table a weak entity, or a strong entity?”. If it is a weak entity then you don’t have to include. Now, what is a weak entity? A weak entity is one that would not exist on its own, ie it simply joins to other entities together. In fact, even if it had other properties of the relationship between those two other entities, if it wouldn’t make sense to have one or other end of the relationship missing (or both missing even), then it is a weak entity. Of course if your weak entity had properties that you wanted to surface through your model, you might decide to include it anyway.