views:

664

answers:

3

Hi, all

I'm a newbie to Hibernate, so please bear with me if this is obvious or something.

I have an Item POJO which contains a Set<String> consisting of labels. The labels are contained on another Database table from the Item table, so I do a join to populate the pojo.

I'm trying to run a simple example query from the book "Java Persistance with Hibernate" where I query from Item item where 'hello' member of item.labels. Only, for some reason I am getting a

 `org.hibernate.hql.ast.QuerySyntaxException: unexpected end of subtree[from /*qualified class path*/.Item item where 'hello' member of item.labels]`

Does anyone know what might be causing this issue?

Thanks for your help, and here are my POJOs:

public class Item
       private int uuid;
       private Set<String>labels = new HashSet<String>();

       @Id
       public int getUuid(){
          return uuid; 
       }

       @CollectionOfElements
       @JoinTable(name="labels", joinColumns=@JoinColumn(name="uuid"))
       @Column(name="label")
       public Set<String> getLabels(){
            return labels;
       }
 }
A: 

From googling around, it appears that your parameter collection may be empty. I'd add an empty check before calling the query.

The lesson is that Google is your friend. When you can't figure out an error message, try typing it into Google (or your favorite engine.) You are unlikely to be the first person to have been confused by it.

PanCrit
however, the query "from Item item" returns exactly correct, with non-empty collections. Also, the query "from Item item where size(item.labels)>0" returns correctly, so I don't think it's a size issue
Scott Fines
A: 

The member of command in the HQL is reserved for the use of non-primitive objects. There are two things you can do. You can either create a SQLQuery as follows:

SQLQuery sQuery = session.createSQLQuery("select * 
                                          from item_table it 
                                          inner join label_table lt 
                                          where it.id = lt.item_id 
                                          and lt.label = 'hello'");
sQuery.list();

Or you can create a class called Label and do the following in your HQL:

from Item item, Label label
where label member of item.labels
      and label.label = 'hello'

Hope this helps :)

jtbradle
aww man, I was hoping it wouldn't come to SQL ;) Thanks though, your solution worked great!
Scott Fines
No prob, Scott. :)
jtbradle
A: 

For primitives collections you should use HQL query like this:

from Item item join item.labels lbls where 'hello' in (lbls)

PS: 'join' is required because 'labels' is OneToMany or ManyToMany variant, parentheses are required because 'lbls' is a collection

Yuri.Bulkin