-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to query single elements in mapped maps
PostPosted: Wed Jan 18, 2006 5:50 am 
Newbie

Joined: Wed Jan 18, 2006 5:26 am
Posts: 1
Hibernate version: 2.18

Mapping documents:

Code:
   <class name="com.example..User" table="user_core" >
      <id name="id" type="java.lang.Long" unsaved-value="null">
         <generator class="native"/>
      </id>

      <property name="name" type="string" unique="true" />
      <property name="password" type="string"/>

      <map name="textData" lazy="false" cascade="all-delete-orphan">
         <key column="userid"/>
         <index column="property" type="string"/>
         <element column="data" type="string"/>
      </map>
   </class>


Code:
table user_core
--------------------------
id | name | password
--------------------------
1  |  bob    | builder
2  |  bill     | 1234

table textData
------------------------------------
userid | property            | data
------------------------------------
1        | 'phonenumber'   | '012-3456789'
1        | 'faxnumber'       | '012-3456789'
2        | 'phonenumber'   | '014-5678901'


I would like to know how to query (in HQL) the user with phonenumber 012-3456789

I would do something like this:
select this from com.example.User this
join this.textData textData
where textData.key = 'phoneNumber' and textData.value = '012-3456789'

But that doesn't work. I can't user 'key' and 'value' as properties of the Map object. Nor can i use 'index' or 'element'. The only option is to use indices and elements, but i don't want to match multiple elements, I want to match a single key value pair. Is this possible in HQL or am I restricted to native SQL?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 22, 2006 9:49 am 
Newbie

Joined: Sat Oct 22, 2005 9:26 pm
Posts: 11
I'm working on the same thing, if I figure something out I'll reply.

One thought, I've been playing around with joins a bit, and I've been getting some (inefficient) results by joining the map and selecting on the results ...

ie something like ... (I'm pretty sure this won't work)

select user from User
left outer join user.textData
where user.textData.data = ?

I'm confused by the elements in your map though, I think they are strings but hard to tell.

I am trying to avoid the joins though, so I'm working on matching the id's a bit better. My map goes to items that are joined subclasses, so it's a bit confusing.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 22, 2006 10:53 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
select this
from com.example.User this
where this.textData['phoneNumber'] = '012-3456789'


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 22, 2006 7:05 pm 
Newbie

Joined: Sat Oct 22, 2005 9:26 pm
Posts: 11
Thank you Steve.

Is there a way to do it if you don't know the property name ? That is the hard part for me, since I have a composite type in the map with properties.

Regards,
John.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 10:19 pm 
Beginner
Beginner

Joined: Tue Sep 21, 2004 4:04 pm
Posts: 25
Location: Oldsmar, FL
Any further answer to this?
How to query a simple map element when you don't know (or care) about the index?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 12, 2007 1:13 pm 
Newbie

Joined: Thu Nov 13, 2003 12:03 pm
Posts: 18
Location: France
Just cross this post....
I guess you could do that

select this
from com.example.User this
join this.textData map
where this.textData[index(map)] = '012-3456789'


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.