-->
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.  [ 12 posts ] 
Author Message
 Post subject: how to do a query on a collection of strings
PostPosted: Fri Mar 04, 2005 12:28 pm 
Newbie

Joined: Fri Mar 04, 2005 12:11 pm
Posts: 1
I have a class User containing a Set of Strings:

Code:
class User
{
        private Set theKeywords = new HashSet(); // of String

        void setKeywords(Set aSet)
        {
                theKeywords = aSet;
        }
        Set getKeywords()
        {
                return theKeywords;
        }
}

The mapping file for the User class looks something like:

Code:
<class name="User" table="USER">
   <id name="Id" column="ID" type="long">
      <generator class="increment"/>
   </id>
   <property name="Name" column="NAME" type="string"/>
   <set name="Keywords" table="KEYWORDS">
      <key column="USER_ID"/>
      <element column="VALUE" type="string"/>
   </set>      
</class>

Now I want to perform a query where I search all users that have a certain keyword. I can't seem to get it to work (not with HQL nor with Criteria objects). Can anyone help me here; the query I would like to execute is something like:

Code:
select u
from User u, ??? k
where k.user_id = u.id
   and k.value = 'text'

The problem is that the Keyword is just an ordinary String and not a specific class for which I have defined a custom mapping.

All help is greatly appreciated, Hugo.

PS: I'm using hibernate 2.1.7c.


Top
 Profile  
 
 Post subject: Same problem
PostPosted: Mon Jan 16, 2006 4:02 pm 
Newbie

Joined: Fri Dec 30, 2005 5:25 am
Posts: 13
I am having exactly the same issue... Did you figure out a solution?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 16, 2006 4:51 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
And so is germuska, on another thread. Have you tried using criteria + Restrictions.sqlRestriction()? I don't know if it'll work, but you can use column names (VALUE) which might be a workaround.

Failing that, a simple Keyword class containing just an id and a string will work.


Top
 Profile  
 
 Post subject: Mapping of Set of Strings
PostPosted: Mon Jan 16, 2006 4:54 pm 
Newbie

Joined: Fri Dec 30, 2005 5:25 am
Posts: 13
Well, I think that my problem is to do the actual mapping of this collection (Set) of String... Embarrassing, I know! So, I'm not even that far...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 16, 2006 5:09 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You mean that your current mapping isn't working? Or that you want to change it so that criteria will do what you want them to do?

germuska has confirmed that Restrictions.sqlRestriction will do what you want, but if you'd prefer to change your mapping so that plain vanilla queries/criteria work, then the solution is to create a new Keyword class, primary key USER_ID, single property VALUE, then change the element member of your set to be a one-to-many to the Keyword class.


Top
 Profile  
 
 Post subject: my example
PostPosted: Mon Jan 16, 2006 5:15 pm 
Newbie

Joined: Fri Dec 30, 2005 5:25 am
Posts: 13
This is the simple mapping I have and that doesn't work.
Obviously, I have a parent object (Project) that has a java.util.Set<String>. It has the needed getters/setters.

When I create a new project and add a few items to the collection and I look in the database, it is not persisted. I have looked at multiple examples and nothing seems to be wrong.

The only thing I think might be the problem is that I used the JBoss IDE plugins for eclipse to generate the DDLs. Therefore, there might be something wrong with the table itself.

But this is probably far beyond the original posting of this thread...

Code:
<set name="workforces" table="WORKFORCE" cascade="save-update" >
   <key column="PROJECT_ID" />
   <element type="string" column="workforce" not-null="true" />
</set>


Generated table (MySQL)
Code:
+------------+--------------+------+-----+---------+-------+
| Field      | Type         | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| PROJECT_ID | bigint(20)   |      | PRI | 0       |       |
| workforce  | varchar(255) |      | PRI |         |       |
+------------+--------------+------+-----+---------+-------+


Thanks a ton for your help!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 16, 2006 10:03 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The <key column="PROJECT_ID"/> refers to a column in the parent table. Do you have a PROJECT_ID column in your parent table? The column in the WORKFORCE table that hibernate will join to defaults to the column with the same name; if you want to change it, you use property-ref.

In your first post, you used USER_ID as the column. Which is correct?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 4:33 am 
Newbie

Joined: Fri Dec 30, 2005 5:25 am
Posts: 13
Well, this is how my parent table look like, as you can see... there's a PROJECT_ID column (which is actually the PK). Is that wrong?

Code:
+-----------------+--------------+------+-----+---------+----------------+
| Field           | Type         | Null | Key | Default | Extra          |
+-----------------+--------------+------+-----+---------+----------------+
| PROJECT_ID      | bigint(20)   |      | PRI | NULL    | auto_increment |
| name            | varchar(255) |      |     |         |                |
| location        | varchar(255) | YES  |     | NULL    |                |
| size            | varchar(255) | YES  |     | NULL    |                |
| confidential    | tinyint(1)   | YES  |     | NULL    |                |
+-----------------+--------------+------+-----+---------+----------------+


Thanks a lot for your help, tenwit!

PS: I don't think I ever used USER_ID, that was from the original post.


Top
 Profile  
 
 Post subject: maybe some help
PostPosted: Tue Jan 17, 2006 5:14 am 
Regular
Regular

Joined: Tue Nov 29, 2005 12:31 pm
Posts: 75
Hi,

The thread starter indicated correctly that you cannot make a search in the collection because the class for the list is not specified. You can see more details on this link http://forum.hibernate.org/viewtopic.php?t=943792&highlight=collection+association+criteria or al least this one helped me.

Here is how I would solve this one:

- added in the child table a many-to-one relation pointing to father
- your hql should be

select
child.parent
from
child
where
child.value = 'Rate me :)'

Hope that was helpfull.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 5:18 am 
Newbie

Joined: Fri Dec 30, 2005 5:25 am
Posts: 13
WEll, that most certainly answer the original post of this topic. I have however drifted away from the original posting (bad, I know!!) ;-)

Hey, by the way, how do we rate answers?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 3:43 pm 
Newbie

Joined: Fri Dec 30, 2005 5:25 am
Posts: 13
I think I have found the problem... I used AbstractTransactionalDataSourceSpringContextTests which is a Spring provided class to do this kind of Unit Tests. I think it does something behind the scenes with the cache and therefore not all the results are written to the database...

Thanks for your help.

Let me know how I can rate you and I'll be happy to do so.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 4:49 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
There's a link at the bottom of everyone's response (except your own): click it if the post helped. You can only rate three posts per thread, so figure out which ones actually helped, don't just click them all :)


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