如何在hibernate中创建复杂的查询

时间:2013-04-21 22:55:29

标签: java hibernate

我对hibernate很新,想知道是否有人可以帮助我。我几乎写了命令我将如何在SQL中执行它我尝试了几个变化没有工作,想知道是否有人可以指出如何在休眠中执行这样的命令。

Query query = session.createQuery("from customer where customer_city = Harrison" 
            + " AND where customer_street = main" + " AND where customer_name = Hayes"); 

1 个答案:

答案 0 :(得分:0)

我看到一些可能导致问题的事情。首先,where子句在每and之后重复。其次,你的HQL查询中有文字值,据我所知,这是不受支持的。通常,执行此类查询的代码如下所示:

String queryString
    = "from customer "
    + "where customer_city = ? "
    + "and customer_street = ? "
    + "and customer_name = ? ";
Query query = session.createQuery(queryString);
query.setString(0, "Harrison");
query.setString(1, "main");
query.setString(2, "Hayes");
List<Customer> customers = (List<Customer>) query.list();