I understand why you do not like the solution.
You save information only once, but you must update changes in both classes.
If you change the Band to a CD , you need to remove the CD from the old Band , install the Band in the CD , and then add it to the new Band >.
This, of course, is complicated and error prone.
There are no simple getters / setters in your classes. Instead, you should implement a lot of logic in your domain classes in order to maintain consistency.
The advantage, of course, is that you always have a Band available CD and vice versa. This is a compromise. For example, if you, for example, use the CD Band as part of the equals implementation.
Here is an interesting alternative that may be beneficial in some situations:
Use the Band and CD classes only as simple POJOs and use another class (i.e. MusicService ) to resolve the relationship:
class MusicService { Band getBand(CD cd); List<CD> getCDs(Band band); addCD(Band band, CD cd); }
- Advantages: Separation of problems, stateless services
- Downside: more code
source share