简体   繁体   中英

HQL search within child SET with IN keyword

i really can not find enough documentation on hibernate IN keyword, when applied on search within a collection of some object. I have strange problem, I have a hql query:

    FROM Contact co, IN (co.categories)categories WHERE categories.name = ?

In i was expecting list of Contacts ofcourse. But something is wrong with it, because it is not returning list of Contact objects, but list of Object[]????? Is it syntax or this is totally wrong??

Here are mapping parts:

     <set lazy="false" name="categories" table="ContactCategory">
        <key column="id" foreign-key="fk_contact_category" />
        <many-to-many class="Category" column="catid"
            foreign-key="fk_contact_category2" />
    </set>

     <class name="Category">

    <id column="catid" name="Id" type="long">
        <generator class="sequence" />
    </id>
    <property length="50" name="name" type="string" />  
</class>

Important thing to mention: This query is made with the query builder. This is printout of one of generated queries where its failing. Very weird is that - i am getting the correct number of objects in this list, I check the database and number is correct with given parameters, but I dont get Contact objects, but some Object arrays in the List.

Appreciate all the help

You need to add SELECT co

so that your Query is SELECT co FROM Contact co, IN (co.categories)categories WHERE categories.name =?

The SELECT co is necessary, to tell Hibernate which one item it should return per result set line.


SELECT co FROM Contact co LEFT JOIN co.categories cat WHERE cat.name =? I have seen the IN keyword only in the Where - clause so far. In Stuff like this,

FROM catagories cat WHERE cat.name IN ('HALLO', 'WORLD')

I dont like answering my question, without really understanding why this SELECT is neccessary, but this works as a charm. If someone explains me reasoning, I would be happy to vote his answer.

  **SELECT co** FROM Contact co, IN (co.categories)categories WHERE categories.name = ?

Thank you all

Julia, try printing the class and value of each object in the array of your original query like this:

List<Object[]> results = // code to fetch your query ;
// just the first, or you can print every entry with a outer loop
Object[] firstObject = results.get(0);
for (Object o : firstObject) {
  System.out.println(o.getClass() + " - " + o);
}

My guess is that hibernate is inferring either a Contact object and a separate Category list or is bringing columns returned from the query as primitive wrappers. Anyway, the problem seems to be that Hibernate could not figure out what you were expecting to fetch from the the list of columns that were returned by the DBMS. In the second query you narrowed it down to an alias of the specific type you wanted, so everything worked as expected.

This worked for me in Grails 2.1.4 HQL

Task.executeQuery(" select task from Task task join task.tags tag where tag.name = 'duplicate' ")

assuming the entity Task.groovy has

static hasMany = [tags: Tag]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM