Sometimes Relational is Better.
I just came across another one of those problems I encounter from time to time, where trying to cram something into an object model just doesn't work. In this case I have a many to many relationship between two classes. Each class wants to be able to look up the other by its ID. Hibernate does a good job of generating a relationship table that can hook these two classes together. All is fine so far.
Now I want to characterize, optionally, each link between the two objects with a number or string. In the relational world it's simple -- just add the field to the association table and move on to the next problem. But we're dealing with objects, and it isn't so easy.
Sure, I can create an intervening object that contains the value I want, and I can reset the maps to point to it...but it's harder to look things up fast. What I want is an association class here -- UML lets me do this, but...object-oriented collections libraries generally don't.
The java.util Collections library gives me key and value, but no ability to stuff something else in there, unless I use a pair-like class that sits in the middle. And since I want this relationship to be bidirectional...yuck.
See how complicated such a simple thing becomes?
Hibernate provides "persistence for idiomatic Java", but every once in a while that's not such a good thing. There's more to the world than objects; they do a decent job of expressing things but not a perfect one.
11:03:25 PM
|