I should make the caveat that I have not used composite keys and I avoid them as much as possible. If you have control over your DB design I would recommend changing it to have a single primary or surrogate key.
Quote:
1. Why is hashCode() called so many times even though there is only one item in the barList?
Best guess is that it is being called in a couple of cases merely to generate a log message. It is also being called when Hibernate checks its session cache to see if the object is already there.
Also, I would probably modify my hashCode method to fail if any of the necessary components are null. It doesn't make sense to me to instantiate a composite-id class unless I am going to immediately set all the necessary attributes for hashCode to succeed.
Quote:
2. Since fooId is populated into the Foo object by using a sequence SEQ_FOOID, once Foo is saved, is the fooId value automatically populated in the BarCompositeKey for saving the Bar objects? Note: The barId value is always populated as it is sort of like the lineNumber and so the BarCompositeKey is never null.
I'm surprised you are getting as far as you are. Can you paste in your test code? There must be a place where you are instantiating BarCompositeKey yourself, because Hibernate does not have an IdentifierGenerator for composite keys, and that seemsto be the central problem. It seems like you want Hibernate to update a
part of a composite-id.
My guess is that the fooId will be populated in the table (shouldn't your mapping specify that the table is "BAR"? This isn't your actual code, is it?), but not in the object itself. In terms of the object graph, Hibernate manages the many-to-one from Bar to Foo independently of the one-to-many from Foo to Bar. That is why fooId is always null in your test.
You may want to read
http://www.hibernate.org/hib_docs/reference/html/parent-child.html
I would recommend setting inverse="true" on barList. Then, in your code, whenever you add a Bar to barList, you will need to instantiate a BarCompositeKey, set its fooId and barId, and then set bar.barCompositeKey to the the key you just created.
Really, though, it seems like it would be better to get rid of the composite-id and have either a surrogate key with the Bar-to-Foo relationship mapped as a regular many-to-one relationship (rather than a part of the identifier), or use the <list> mapping and make BARID the index.
Unfortunately I can't answer exactly why you are getting the exception you are receiving, but I'm pretty sure it has to do with the parent-child stuff.