Well, if you read about the Java API, you will know that Set will only add object that does not exist in the Set itself. You must be aware of this especially when working with Hibernate! Here's my experience:
I was trying to add a few objects in a Set and set this Set. Then I found out from the database that there is always only ONE object added. I did a debug and found the size of the Set is always > 1 !!!
What happened? Here is my answer from torough checking:
Only the 1st object had been added to the Set. It is because the rest are actually "equal" to the first object so Set does not take them anymore! Bear in mind that each object differs from each other using their ID field. They do have their own ID yet when they are being added into the Set so they are rejected by the Set!
Thus, i have to save each object first before adding them into the Set. The reason is the ID will be generated by Hibernate for each object. The Set knows that they are NOT EQUAL right now then they will be added.
No comments:
Post a Comment