• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Coding a 1 to N relationship. Question about OO Design and Implementation

 
Rodrigo Bossini
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I have a simple question regarding OO design.

In my relational database I have a 1 to N relationship between client and messages. I mean, one client can have multiple messages and one message belongs to one single client.

The question about OO is how to "map" it to a OO application.

As per my understanding, I would create a Client class with a Collection of messages. I understand that a Message should NOT have a Client as an attribute. Is it right?
If it's right, how does one message know what client it belongs to? Is my doubt clear enough?

Thanks for any help.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope, the message can have a reference to the client, and the client can have a reference to many (java.util.List / Set) of messages.

Here's a little tutorial I put together on such a subject, although I use the old "A Team Has Player" analogy. But it's the same idea as a Client has Messages. You can see the Java, class diagram and ERD for this scenario. A team has a list of players, and a player has a reference back to the team. The question is, 'does the player need to know which team they are on.' If the answer is yes, the property makes sense. Does a message need to know what client it belongs to? Maybe?

One to Many Association in Java: A Team Has Players

-Cameron McKenzie


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic