-->
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.  [ 5 posts ] 
Author Message
 Post subject: [Newbie] How to query on a collection of Strings?
PostPosted: Tue Feb 23, 2010 6:49 pm 
Newbie

Joined: Tue Feb 23, 2010 6:11 pm
Posts: 1
Hi folks,

I have a class that looks like this:

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
abstract class Person {

@Id
private String id;

@CollectionOfElements(fetch=FetchType.EAGER)
private Set<String> aliases = new TreeSet<String>();

}

Now, when Hibernate pushes that out to the DB, I end up with a table "PERSON" and the 'child table' (containing the aliases for each PERSON) of PERSON_ALIASES. The PERSON_ALIASES table contains the columns PERSON_ID and ELEMENT.

My question is this: Without using a wrapper class (that is, a set of "Alias" objects where each object contains a String), how do I go about querying on an alias?

For example: I want to say, "Hey Mr. Hibernate, get me all the People who have the the Alias of 'spiderman'!"

It seems like I would do something like this:
"List spiderPeople = sess.createCriteria(Person.class)
.createCriteria("aliases")
.add( Restrictions.eq("ELEMENT", "spiderman") )
.list();

Unfortunately, that doesn't work, because it doesn't know what "ELEMENT" is. :(

I am not using any XML, so please don't use that in showing me how... You'll just confuse me. :D Also, I want to avoid using HQL, because once again, I'm afraid that will have me in over my head.

Oh, one last thing, I can't use the Example API (at least I don't think I can) because Person is an abstract class, so I can't always pass in a Person as an example. :/.

Thanks for helping me become a little less newb-ish,
Newawd


Top
 Profile  
 
 Post subject: Re: [Newbie] How to query on a collection of Strings?
PostPosted: Thu Feb 25, 2010 10:19 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
My question is this: Without using a wrapper class (that is, a set of "Alias" objects where each object contains a String), how do I go about querying on an alias?


I fear that is not possible.
I believe that @CollectionOfElements was not designed for being queried with HQL or Criteria.
(Of course you could realize it with a native join SQL by using sess.createSQLQuery() but you mentioned that you don't intend to use it.)


Top
 Profile  
 
 Post subject: Re: [Newbie] How to query on a collection of Strings?
PostPosted: Thu Feb 25, 2010 11:03 am 
Beginner
Beginner

Joined: Tue Aug 25, 2009 11:42 am
Posts: 49
session.createQuery("from Person p where 'spiderman' in elements(p.contents)").list();
Now maybe someone can tell me how to write this as a Criterion.


Top
 Profile  
 
 Post subject: Re: [Newbie] How to query on a collection of Strings?
PostPosted: Thu Feb 25, 2010 11:05 am 
Beginner
Beginner

Joined: Tue Aug 25, 2009 11:42 am
Posts: 49
Actually @CollectionOfElements was created much after HQL or Criteria and they are pretty powerful:
http://docs.jboss.org/hibernate/core/3. ... ryhql.html


Top
 Profile  
 
 Post subject: Re: [Newbie] How to query on a collection of Strings?
PostPosted: Thu Feb 25, 2010 11:44 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
session.createQuery("from Person p where 'spiderman' in elements(p.contents)").list();


Thank you tseringshrestha.

With a little adaption

Code:
sess.createQuery("from Person p where 'spiderman' in elements(p.aliases)").list();


it works and it produces following query (with subquery)
Code:
  select   p0_.*   from  Person a0_
    where
        'spiderman' in (
            select
                aliases1_.element
            from
                Person_aliases aliases1_
            where
                p0_.id=aliases1_.Person_id
        )


Also I don't know how to translate if in Criteria


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