views:

668

answers:

0

I have a hibernate mapping which looks like this:

<hibernate-mapping>
    <class name="MutableEvent" table="events"
        mutable="true" dynamic-insert="true" dynamic-update="true">

        <id name="id">
            <generator class="assigned" />
        </id>
        <property name="sourceTimestamp" />
        <property name="entryTimestamp" />

        <map name="attributes" table="event_attribs"
            access="field" cascade="all">
            <key column="id" />
            <map-key type="string" column="key" />
            <element type="VariantHibernateType">
                <column name="value_type" not-null="false" />
                <column name="value_string" not-null="false" />
                <column name="value_integer" not-null="false" />
                <column name="value_double" not-null="false" />
            </element>
        </map>

    </class>
</hibernate-mapping>

Storing and loading of my Object works fine. The question I have, is querying for maps in hibernate supported, and how would I do it using the criteria api?

I want to do something like this (this is actually a part of my testcase):

...
m.getAttributes().put("other", new Variant("aValue"));
this.storeEvent(MutableEvent.fromEvent(e));
getSession().clear();
MutableEvent m = (MutableEvent) getSession().get(MutableEvent.class, e.getId());
Assert.assertNotNull(m.getAttributes().get("other"));
Assert.assertEquals(new Variant("aValue"), m.getAttributes().get("other"));
Assert.assertNull(m.getAttributes().get("other2"));
getSession().clear();
crit = DetachedCriteria.forClass(MutableEvent.class);
crit.add(Restrictions.eq("attributes.other", new Variant("aValue")));
List l = this.findByCriteria(crit);
Assert.assertEquals(1, l.size());

The important part is, this fails with "Could not resolve property: attributes.other":

crit.add(Restrictions.eq("attributes.other", new Variant("aValue")));
List l = this.findByCriteria(crit);

Is there a solution for that problem at all?

Update

List l = find("from MutableEvent M where M.attributes['other'] = ?", new Variant("aValue"));

The above code doesn't throw a exception, but the query itself is still not what I want to have. I created a custom type as one may see by the mapping, actually I would want to query for a string (column value_string) but any attempt to modify the query to access a part of the type like in "from MutableEvent M where M.attributes['other'].string = ?" doesnt work. So how would I query for a part of a component?

The Type is implemented like this:

...
private static final String[] PROPERTY_NAMES = { "type", "string", "integer", "double" };

public String[] getPropertyNames() {
    return PROPERTY_NAMES;
}
...