A mapping that involves objects with member collections (mapped as a many-to-one List) works well apart from the fact that the resultant List contains a lot of padding with null references. Is this normal? Do I have to trim these Lists at the time the objects are hydrated or is it a config issue (as I suspect!)
The other unsavoury alternative is to change all code that uses these lists to check for null during iteration - in the current JDBC implementation, nulls would never be added to the list so users of the code don't normally need to check. The biggest problem here would actually be the Velocity templates that access these lists.
The debug output from the net.sf.hibernate hierarchy shows SQL generation that is fine - it returns the correct data if pasted into a database operating shell.
Here's a partial config from the hibernate mapping file:
Code:
<!--
- httpsource groups
-->
<class name="HttpSourceGroup" table="groups" where="group_type='sources'">
<id name="id" column="id" type="string" unsaved-value="">
<generator class="assigned"/>
</id>
<property name="title" column="title"/>
<property name="groupType" column="group_type"/>
<list name="members" table="sources_groups">
<key column="group_id"/>
<index column="id" type="long"/>
<many-to-many class="HttpSource" column="source_id"/>
</list>
</class>
<!--
- sources
-->
<class name="HttpSource" table="sources">
<id name="id" column="id" type="long" unsaved-value="-1">
<generator class="identity"/>
</id>
<property name="name" column="name"/>
<property name="url" column="url"/>
<many-to-one name="transformerType" class="TransformerType" column="transformer_id"/>
</class>
Following is log output from a section where some 'HttpSourceGroup' objects are hydrated and the resultant SQL with final List population for 'members'. The SQL correctly returns the 2 member rows that are required by this example and further (omitted) logging shows those 2 objects being hydrated.
Code:
[12/01/04 09:36:40:023 GMT] 6fb2a9ce SystemOut U 2004-01-12 09:36:40,023 DEBUG [net.sf.hibernate.impl.SessionImpl] - <resolving
associations for [com.myco.model.HttpSourceGroup#internal]>
[12/01/04 09:36:40:023 GMT] 6fb2a9ce SystemOut U 2004-01-12 09:36:40,023 DEBUG [net.sf.hibernate.impl.SessionImpl] - <collection
not cached>
[12/01/04 09:36:40:023 GMT] 6fb2a9ce SystemOut U 2004-01-12 09:36:40,023 DEBUG [net.sf.hibernate.impl.SessionImpl] - <initializi
ng collection [com.myco.model.HttpSourceGroup.members#internal]>
[12/01/04 09:36:40:023 GMT] 6fb2a9ce SystemOut U 2004-01-12 09:36:40,023 DEBUG [net.sf.hibernate.impl.BatcherImpl] - <about to o
pen: 1 open PreparedStatements, 1 open ResultSets>
[12/01/04 09:36:40:023 GMT] 6fb2a9ce SystemOut U 2004-01-12 09:36:40,023 DEBUG [net.sf.hibernate.SQL] - <select members0_.source
_id as source_id__, members0_.group_id as group_id__, members0_.id as id__, httpsource1_.id as id0_, httpsource1_.name as name0_, ht
tpsource1_.url as url0_, httpsource1_.transformer_id as transfor4_0_, transforme2_.id as id1_, transforme2_.name as name1_ from sour
ces_groups members0_ inner join sources httpsource1_ on members0_.source_id=httpsource1_.id left outer join transformers transforme2
_ on httpsource1_.transformer_id=transforme2_.id where members0_.group_id=?>
[12/01/04 09:36:40:023 GMT] 6fb2a9ce SystemOut U 2004-01-12 09:36:40,023 DEBUG [net.sf.hibernate.impl.BatcherImpl] - <preparing
statement>
[12/01/04 09:36:40:023 GMT] 6fb2a9ce SystemOut U 2004-01-12 09:36:40,023 DEBUG [net.sf.hibernate.type.StringType] - <binding 'in
ternal' to parameter: 1>
(the above SQL returns the correct 2 members for the group with id 'internal')
Code:
[12/01/04 09:36:40:055 GMT] 6fb2a9ce SystemOut U 2004-01-12 09:36:40,055 DEBUG [net.sf.hibernate.loader.Loader] - <result set co
ntains (possibly empty) collection: [com.myco.model.HttpSourceGroup.members#internal]>
[12/01/04 09:36:40:055 GMT] 6fb2a9ce SystemOut U 2004-01-12 09:36:40,055 DEBUG [net.sf.hibernate.impl.SessionImpl] - <uninitiali
zed collection: initializing>
(logs then correctly show the 2 member objects being hydrated)
Code:
[12/01/04 09:36:41:008 GMT] 6fb2a9ce SystemOut U 2004-01-12 09:36:41,008 DEBUG [net.sf.hibernate.impl.Printer] - <com.myco.model.HttpSourceGroup{groupType=sources, members=[null, HttpSource#1, null, null, HttpSource#9], title=The Title, id=in
ternal}>
The last line of logging (above) highlights the null padded list of member objects. No logging output ever shows a null reference being added to the members collection so I'm guessing it's the result of some sort of internal data conversion (to/from an array?). Is there anything further I need do to prevent it happening?
Regards,
Darren.[/code]