3

次のような休止状態のマッピングがあります。

<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>

オブジェクトの保存と読み込みは正常に機能します。私が持っている質問は、休止状態でのマップのクエリはサポートされていますか?基準 API を使用してそれを行うにはどうすればよいですか?

私はこのようなことをしたいです (これは実際には私のテストケースの一部です):

...
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());

重要な部分は、これが「プロパティを解決できませんでした: attributes.other」で失敗することです。

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

その問題の解決策はまったくありますか?

アップデート

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

上記のコードは例外をスローしませんが、クエリ自体はまだ必要なものではありません。マッピングでわかるように、カスタムタイプを作成しました。実際には、文字列 (列 value_string) を照会したいのですが、"from MutableEvent M where M.attributes"のようにタイプの一部にアクセスするようにクエリを変更しようとすると、 ['その他'].string = ?" 動作しません。では、コンポーネントの一部を照会するにはどうすればよいでしょうか?

Type は次のように実装されます。

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

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

1 に答える 1

0

基準のエイリアスを作成してみてください。

criteria.createAlias( "attributes", "as" );
criteria.add( Restrictions.ilike( "as.other", new Variant("aValue") );
于 2011-12-16T06:35:10.267 に答える