简体   繁体   English

使用 IN 关键字在子 SET 内进行 HQL 搜索

[英]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.当应用在一些 object 的集合中搜索时,我真的找不到关于 hibernate IN 关键字的足够文档。 I have strange problem, I have a hql query:我有一个奇怪的问题,我有一个 hql 查询:

    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[]?????但是它有问题,因为它不是返回 Contact 对象列表,而是 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.非常奇怪的是 - 我在这个列表中获得了正确数量的对象,我检查了数据库,并且给定参数的数量是正确的,但是我没有得到联系人对象,但是列表中有一些 Object arrays。

Appreciate all the help感谢所有帮助

You need to add SELECT co您需要添加SELECT co

so that your Query is SELECT co FROM Contact co, IN (co.categories)categories WHERE categories.name =?这样您的查询是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是必要的,它告诉 Hibernate 每个结果集行应该返回哪一项。


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.到目前为止,我只在 Where - 子句中看到了IN关键字。 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.我不喜欢回答我的问题,没有真正理解为什么这个 SELECT 是必要的,但这很有吸引力。 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: Julia,尝试在原始查询的数组中打印 class 和每个 object 的值,如下所示:

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.我的猜测是 hibernate 正在推断 Contact object 和单独的类别列表,或者将从查询返回的列作为原始包装器。 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.无论如何,问题似乎是 Hibernate 无法弄清楚您期望从 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这在 Grails 2.1.4 HQL 中对我有用

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

assuming the entity Task.groovy has假设实体 Task.groovy 有

static hasMany = [tags: Tag]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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