Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在hibernate中查询地图元素?_Java_Hibernate_Criteria - Fatal编程技术网

Java 如何在hibernate中查询地图元素?

Java 如何在hibernate中查询地图元素?,java,hibernate,criteria,Java,Hibernate,Criteria,我有一个hibernate映射,如下所示: <hibernate-mapping> <class name="MutableEvent" table="events" mutable="true" dynamic-insert="true" dynamic-update="true"> <id name="id"> <generator class="assigned" />

我有一个hibernate映射,如下所示:

<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>
...
private static final String[] PROPERTY_NAMES = { "type", "string", "integer", "double" };

public String[] getPropertyNames() {
    return PROPERTY_NAMES;
}
...
重要的是,此操作失败,出现“无法解析属性:attributes.other”:

这个问题有解决办法吗

更新

List l = find("from MutableEvent M where M.attributes['other'] = ?", new Variant("aValue"));
上面的代码没有抛出异常,但查询本身仍然不是我想要的。我创建了一个自定义类型,正如映射所示,实际上我希望查询一个字符串(column value_string),但是任何修改查询以访问该类型的一部分的尝试,如“from MutableEvent M where M.attributes['other'].string=?”都不起作用。那么如何查询组件的一部分呢

该类型的实现方式如下:

<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>
...
private static final String[] PROPERTY_NAMES = { "type", "string", "integer", "double" };

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

尝试为条件创建别名

criteria.createAlias( "attributes", "as" );
criteria.add( Restrictions.ilike( "as.other", new Variant("aValue") );

CriteriaAPI不如HQL强大,您可以在HQL中查询映射。