-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Query By Criteria With a Map of Strings?
PostPosted: Wed Jun 30, 2004 6:39 pm 
Newbie

Joined: Thu Dec 18, 2003 12:30 am
Posts: 9
Using Hibernate v2.1

I have a dynamic app that allows users to associate textual attributes to an object. The user can use any string for the name of the attribute as well as any string for the value. Then, users can search for objects based on an attribute name and value.

Mapping document looks like (cut irrelevant aspects out):

Code:
<class name="Thing" table="Thing">
    <map name="attributes" table="Thing_Attribute">
        <key column="thingId"/>
        <index type="string" column="mapIndex"/>
        <element type="string" column="value"/>
    </map>
</class>


The java class is pretty straightforward (cut irrelevant aspects out):

Code:
public class Thing
{
    private Map attributes = new HashMap();

    protected getAttributes() { return attributes; }

    protected setAttributes(Map value) { attributes = value; }
}


My question is: how do I query for a Thing that has attribute X=x? And, I can't just query the join table directly because there are other aspects to the query (some non-dynamic fields can be queried as well) besides just the attribute.

Seems like I should be able to do something like:

Code:
...
Criteria thingCriteria = session.createCriteria(Thing.class);
Criteria attributeCriteria = thingCriteria.createCriteria("attributes");
Expression attributeName = Expression.eq(
    "<WHAT GOES HERE TO REFERENCE THE mapIndex?>",
    search.getAttributeName());
Expression attributeValue = Expression.eq(
    "<WHAT GOES HERE TO REFERENCE THE value?>",
    search.getAttributeName());
Junction junction = Expression.and(attributeName, attributeValue);
attributeCriteria.add(junction);
...


Unfortunately, I don't know what to use for property names.

I tried:

Code:
Criteria thingCriteria = session.createCriteria(Thing.class);
Criteria attributeCriteria = thingCriteria.createCriteria("attributes");
Criterion sql = Expression.sql("{alias}.mapIndex=? AND {alias}.value=?",
    new Object[] {search.getAttributeName(), search.getAttributeValue()},
    new Type[] {Hibernate.STRING, Hibernate.STRING});
attributeCriteria.add(sql);


This results in all Things being returned.

Please help,
Dan


Top
 Profile  
 
 Post subject: Uggghhhhh! Please tell me that I've made a mistake.
PostPosted: Wed Jun 30, 2004 9:03 pm 
Newbie

Joined: Thu Dec 18, 2003 12:30 am
Posts: 9
Okay, I found out why using the sql expression wasn't working. The criteria.createCriteria("attributes") was throwing an exception because it didn't see 'attributes' as an association. What?!?!

My work-around looks like this:

Mapping:

Code:
<class name="StringMapping" table="Attribute_String">
    <id name="key" column="mapIndex" unsaved-value="null" >
        <generator class="assigned"/>
    </id>
    <property name="value"/>
</class>

The map


Code:
<class name="Thing" table="Thing">
    <map name="attributes" table="Thing_Attribute">
        <key column="thingId"/>
        <index type="string" column="mapIndex"/>
        <one-to-many class="StringMapping"/>
    </map>
</class>


Java class for StringMapping:
Code:
public class StringMapping
{
    private String key;
    private String value;

    // appropriate accessors and mutators
}


This is grotesque. What did I miss? How do I get Hibernate to recognize an association without an intervening and extraneous Class? Or, is there some way to create the Criteria, in this case, without using the associated criteria?

Please, say it ain't so


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 19, 2004 1:52 am 
Newbie

Joined: Fri Aug 29, 2003 3:28 am
Posts: 4
Try some hql like:

Code:
from Thing t where t.attributes[?] = ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 19, 2004 3:39 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Criteria queries do not support collections of values. This is documented.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.