6

Let's say I have two classes: one for Band and one for CD. I want to be able to access easily all CD from one Band and to find the Band of a CD.

My solution is to have a field Band in CD, and a ArrayList<CD> in Band.

But I don't find this is a good solution. Does anyone know of a better design for this scenario?

6
  • 7
    Why do you not find it a good solution?
    – Joni
    Aug 29, 2013 at 11:48
  • 2
    what is the relation between java and foreign keys.As far as i know foreign keys and primary keys are there in sql.Why your tags are java and foreign keys? Aug 29, 2013 at 11:49
  • I put foreign key because if I map it to a database I will use a foreign key to link CD and Band. I don't find it is a good solution because I store two times the same information, and I was just asking me if a better solution is possible :)
    – trnsnt
    Aug 29, 2013 at 11:53
  • 1
    You probably know this already but because you are only storing a reference you aren't really duplicating the information, just the references to the information; which you'd do one way or annother (or create some sort of hideous search argorithm). I personally use this pattern all the time and find it very acceptable Aug 29, 2013 at 12:04
  • 1
    You are only storing the information once, but you have to update it in both classes, if you add a band, or worse, change the Band of a CD. You have to remove the CD from the old Band, set the Band in the CD, then add it to the new Band. Thus, I concurr in it being complicated and prone to error. Aug 29, 2013 at 12:45

4 Answers 4

4

I see why you dislike the solution.

You are only storing the information once, but you have to update changes in both classes.

If you change the Band of a CD, you have to remove the CD from the old Band, set the Band in the CD, then add it to the new Band.

That is of course complicated and prone to error.

Your classes don't have simple getters/setters. Instead, you have to implement a lot of logic in your domain classes to keep consistency.

The advantage is, of course, that you always have a the Band of a CD accessible and vice versa. Its a trade-off. A neccesary one, should you, for example, use a CD's Band as part of its equals implementation.

Here is an interesting alternative, that may be advantegous in some situations:

Use your classes Band and CD only as simple POJOs and use another class (i.e. MusicService) to resolve the relation:

class MusicService {
  Band getBand(CD cd);
  List<CD> getCDs(Band band);
  addCD(Band band, CD cd);
}
  • Advantages: Separation of concerns, stateless services
  • Disadvantage: More code
1
  • Thanks for your answer you have completely understood my point! I think I will use a Service class, sound a safer idea!
    – trnsnt
    Aug 29, 2013 at 13:09
1

Your solution totally makes sense. Actually that's the principle JPA works with. See Java Persistence/OneToMany. JPA is a pretty good reference on how implement your own ORM.

0

You could have a Set of CDs in the Band class.

Alternatively, keep a unique ID in the Band class which will map each unique Band with multiple CDs.

There is no need to have a Band field in CD, as per my understanding.

2
  • Is there (functionally) any difference between having a band ID held by every CD and a reference to a Band in every CD? Aug 29, 2013 at 12:12
  • If your CD holds a reference to an ID only, the Band might get garbage collected if there are no other references to it.
    – Holger
    Aug 29, 2013 at 12:25
0

You are not storing the information twice: CD just holds a pointer to the class Band. If would have done in C++ then this would have required some thinking (e.g. a weak_ptr to Band in CD to avoid circular references), but in Java the GC will take care of removing both objects when Band is not referenced anywhere else.

You can ensure some consistency (like a CD is only in one band) by writing some code that takes care of this, like the code below (WARNING: NOT TESTED):

class Band
{
 private List<CD> m_cds;

 public void addCD(CD cd)
 {
  id(cd.getBand() == this)
  {
   return;
  }
  if(cd.getBand() != null)
  {
   cd.getBand().removeCd(CD cd);
  }
  cd.setBand(this);
  m_cds.add(cd);
 }

 public void removeCd(CD cd)
 {
  cd.setBand(null);
  m_cds.remove(cd);
 }
}


class CD
{
 private Band m_band;

 public void setBand(Band band)
 {
  m_band = band;
 }

 public Band getBand()
 {
  return m_band;
 }
}
2
  • Thanks for your answer. I know that I am not storing the information twice in memory, but there is like a duplication of the functional information. If I store in CD the reference to the band, it is functionally equivalent to have a Set of CD in Band!
    – trnsnt
    Aug 29, 2013 at 12:31
  • I would consider this a sort of index, in Database terms. When you use a DB Index the same information may stored in different places, but makes retrieving them much easier. Aug 29, 2013 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.