Hibernate and Unexpected end of Subtree exception

后端 未结 4 1436
后悔当初
后悔当初 2021-01-07 17:51

I\'m a newbie to Hibernate.

I have an Item POJO which contains a Set consisting of labels. The labels are contained on anothe

相关标签:
4条回答
  • 2021-01-07 18:08

    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.

    0 讨论(0)
  • 2021-01-07 18:17

    Based on the comments on the bug HHH-5209, which is about the same exception being thrown from a similar JPQL query, i believe the correct form here is:

    select item from Item item where 'hello' in elements(item.labels)
    

    The elements function there is the key. This is perhaps slightly simpler than Yuri's suggestion, because it avoids the explicit join.

    0 讨论(0)
  • 2021-01-07 18:27

    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 :)

    0 讨论(0)
  • 2021-01-07 18:35

    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

    0 讨论(0)
提交回复
热议问题