Having avoided the use of the <any> mapping, I find myself using it now for an Audit implementation. However, I'm not sure how to load the associated entities mapped with <any> using Hibernate 3.2 with XML mapping files.
Suppose the Audit class has an auditedEntity attribute mapped with an <any> element:
Code:
<any name="auditedEntity" id-type="long" meta-type="string">
<meta-value value="ENTITY_A" class="EntityA" />
<meta-value value="ENTITY_B" class="EntityB" />
<meta-value value="ENTITY_C" class="EntityC" />
<meta-value value="ENTITY_Z" class="EntityZ" />
<column name="ENTITY_TYPE" />
<column name="ENTITY_ID" />
</any>
The EntityX classes don't have a mapped attribute to the Audit class because no foreign key relationship can exist from these to the Audit table.
Now, I want to load only those audit records pertaining to EntityA, where the endtityA.id = ?.
I'm just not sure what the query should look like. I've tried a Criteria query against Audit where I do a join to the auditedEntity, like this:
Code:
List list = session.createCriteria(Audit.class)
.setFetchMode("auditedEntity", FetchMode.JOIN)
.add( Restrictions.like("id", id))
.list();
but I get an UnsupportedOperationException with the message, "any types do not have a unique referenced persister"
Clearly, I'm missing something important about using the <any> mapping.
TIA.