In a legacy database that I am accessing via hibernate I have a table [Claim_Detail] indexed by char(19) [Claim_Number].
I also have a table [Diary] that references [Claim_Detail] using char(22) [Ref_No]. There is no foreign key constraint defined. A foreign key would probably fail as the fields are different lengths, and I think [Ref_No] is used for other purposes.
In the ClaimDetail.hbm.xml mapping I set up the following collection for the Diary:
<set name="diarySet">
<key column="Ref_No" />
<one-to-many class="Diary" />
</set>
This collection would then never be populated. With logging set to DEBUG, I would see the correct query being run, and the correct number of results being hydrated. I even managed to get in with the debugger and see a Set being populated with all the object. But the Set given to the ClaimDetail object would be empty.
I eventually worked out that Hibernate appears to double check the results of the query before populating the Set. The check was failing because there was a different amount of whitespace.
I have now solved this problem by setting up a UserType that trims the char data, and all works well.
I don't know if this is a bug, or desired behaviour. But the debug output was misleading and made it difficult to diagnose why I wasn't getting any results. Maybe something as simple as logging when objects are discarded from a collection because they don't match would have made it easier.
Hibernate version:
2.1.8
Name and version of the database you are using:
MS SQLServer 2000
|