简体   繁体   English

如何制作HQL查询对象而不是db?

[英]How to make HQL query object instead of db?

I am new to HQL and this thing confuses the heck out of me. 我是HQL的新手,这件事让我感到困惑。 So, this is the function I have: 所以,这是我的功能:

public List<Map<String, Object>> getMyQuery(List<Country> country){
    String hql = "SELECT DISTINCT new map(a as accounts) ";
    hql += "FROM Account a WHERE a.location_id in "
                + "( "
                + "SELECT location_id FROM Location l WHERE l.address_id in "
                + "("
                + "SELECT id FROM Address adr WHERE adr.country_id in (:country)))";

    }
    Query query = getSession().createQuery(hql);
    if(country != null){
        query.setParameterList("country", country);
    }
    return query.list();
}

In my log files, I saw that the query could not be resolved due to: 在我的日志文件中,我看到由于以下原因无法解析查询:

 Caused by: org.hibernate.QueryException: could not resolve property: country_id of: com.mypackage.myobjects.Address [SELECT id FROM com.mypackage.myobjects.Address adr WHERE adr.country_id in (:country)]

When I run the similar query on the using SQL db, everything is fine, but this one breaks. 当我在使用SQL数据库上运行类似的查询时,一切都很好,但是这个查询中断了。 I was wondering why is that all of a sudden outputs package in the folders? 我想知道为什么文件夹中的所有突然输出包? It seems that the query is being executed on the objects, not the database. 似乎正在对象而不是数据库上执行查询。 What am I doing wrong? 我究竟做错了什么?

Thank you in advance 先感谢您

As error log says, hibernate is unable to find property named "country_id" in class "com.mypackage.myobjects.Address". 正如错误日志所述,hibernate无法在类“com.mypackage.myobjects.Address”中找到名为“country_id”的属性。 May be country_id is name of column from address table. 可以是country_id是地址表中列的名称。

Hql works on Pojo's, try mapping property from Address class that is mapped with counrty_id column. Hql适用于Pojo,尝试使用COUNrty_id列映射的Address类的映射属性。 Please refer to 请参阅

If Address class is,
Class Address{

 @Column(name="country_id")
 private int countryId;

  ....
}

then replace "country_id" in hql query with "countryId" 然后用“countryId”替换hql查询中的“country_id”

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

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