Exerpt from my mapping:
Code:
<class name="de.fzi.dbs.cwb.impl.RequestImpl" table="Request">
<id name="request_id" column="Request_ID" type="integer">
<generator class="increment"/>
</id>
<property name="r_description" column="R_Description" type="string"/>
<property name="r_date" column="R_Date" type="date"/>
<bag name="attachments" lazy="true" inverse="true">
<key column="request"/>
<one-to-many class="de.fzi.dbs.cwb.impl.RequestAImpl"/>
</bag>
<bag name="advances" lazy="true" inverse="true">
<key column="request"/>
<one-to-many class="de.fzi.dbs.cwb.impl.AdvanceImpl"/>
</bag>
Code:
</class>
So, I have two bags in this class.
Then I'm trying to load an object of this class and initialize both collections.
Code is bit tricky because of reflection, but anyway
Code:
session = bindTag.getHibernateComponent().openSession();
// Extract parameters
Object[] values = parameterValues.toArray();
Type[] types = (Type[])parameterTypes.toArray(new Type[0]);
results = session.find(query, values, types);
// Map of getter
final Map getters = new HashMap();
// Map of setters
final Map setters = new HashMap();
// Now iterate over filter trying to filter collections
for (Iterator filters = filterList.iterator(); filters.hasNext();)
{
// Get current filter
CollectionFilter filter = (CollectionFilter)filters.next();
// Iterate over the list of results
for (Iterator entities = results.iterator(); entities.hasNext();)
{
final Object object = entities.next();
// Get class
final Class clazz = object.getClass();
Method getter = (Method) getters.get(clazz);
if (null == getter)
{
// Find a getter method for the collection
try
{
getter =
clazz.getMethod(
"get"
+ Character.toUpperCase(filter.getCollectionName().charAt(0))
+ filter.getCollectionName().substring(1),
new Class[0]);
getters.put(clazz, getter);
} catch (NoSuchMethodException nsmex)
{
throw new JspException(
"[HXT00005] No getter for collection "
+ filter.getCollectionName()
+ " in class "
+ clazz.getName());
}
}
Method setter = (Method) setters.get(clazz);
if (null == setter)
{
// Find a setter method for the collection
try
{
setter =
clazz.getMethod(
"set"
+ Character.toUpperCase(filter.getCollectionName().charAt(0))
+ filter.getCollectionName().substring(1),
new Class[] { Collection.class });
setters.put(clazz, setter);
} catch (NoSuchMethodException nsmex)
{
throw new JspException(
"[HXT00006] No setter for collection "
+ filter.getCollectionName()
+ " in class "
+ clazz.getName());
}
}
// Get collection
try
{
final Object collection = getter.invoke(object, null);
if (null == filter.getQuery()) {
// If there is no query, just initialize collection
Hibernate.initialize(collection);Code:
}
else
{
// Otherwise filter it and put again
Object[] filterValues = filter.getParameterValues().toArray();
Type[] filterTypes = (Type[])filter.getParameterTypes().toArray(new Type[0]);
Collection filtered = session.filter(collection, filter.getQuery(), filterValues, filterTypes);
setter.invoke(object, new Object[] { filtered });
}
} catch (IllegalAccessException iaex)
{
throw new JspException(
"[HXT00008] Couldn't access collection "
+ filter.getCollectionName()
+ " in the object ["
+ object
+ "].",
iaex);
} catch (InvocationTargetException itex)
{
throw new JspException(
"[HXT00009] Error during acessing collection "
+ filter.getCollectionName()
+ " in the object ["
+ object
+ "].",
itex);
}
}
}
The problem is that only the FIRST collection gets initialized!!!
I tried to investigate it a bit and found out, that collection objects (those of class net.sf.hibernate.collection.Bag) have actually the same
hashCode() , so after the first collection is initialized, the second one is considered to be initialized too and therefore, is skipped :((
Did I do something wrong or is it a bug?
Thank you in advance, Alexei