【问题标题】:Creating inner query in hibernate在休眠中创建内部查询
【发布时间】:2013-08-22 12:28:23
【问题描述】:

如何在hibernate的内部查询中添加set parameter()方法?

我尝试过这样做,但已经出现错误

这是我的代码

Query query=session.createQuery("select eq.euipmentName,eq.type from Euipment eq where eq.id in(select euipment from Quotation qt where qt. supQuotation=:ids)");          
query.setParameter("ids",id);
list = (List<Euipment>)query.list();

【问题讨论】:

  • 我的错误是“-----Hibernate: select eq.euipmentName,eq.type from Euipment eq where eq.id in(select euipment from Quotation qt where qt.supQuotation=?) Unknown column 'where 子句'中的'qt.supQuotation' ---- "
  • 我的错误是 --Hibernate: select eq.euipmentName,eq.type from Euipment eq where eq. id in(select euipment from Quotation qt where qt.supQuotation=?) 'where 子句'中的未知列 'qt.supQuotation'--
  • 在您的数据库控制台上测试您的查询......然后尝试使用 Hibernate

标签: java hibernate


【解决方案1】:

我对您的查询进行了一些更正: 1. 夸脱。 supQuotation 有空格,我已删除 2.你子查询中的euipment没有别名,我加qt

String hql = 
    "select eq.euipmentName,eq.type " +
    " from Euipment eq " +
    " where eq.id in (select qt.euipment from Quotation qt where qt.supQuotation = :ids)";

Query query = session.createQuery(hql);          
query.setParameter("ids",id);
list = (List<Euipment>)query.list();

告诉我,如果可以的话

如果不正确,请在此处发布错误,并检查您是否已将 hibernate 映射文件放入您的类

【讨论】:

    【解决方案2】:

    来自Hibernate Documentation:

    本机 SQL 查询的执行是通过 SQLQuery 控制的 接口,通过调用Session.createSQLQuery()获得。

    • createQuery() 使用 HQL 语法创建 Query 对象。
    • createSQLQuery() 使用原生 SQL 语法创建 Query 对象。

    因此,将 createQuery 替换为 createSQLQuery 以进行本机 SQL 查询。

    【讨论】:

    • 是什么让查询成为 SQL 而不是 HQL?
    • 亲,我试过了,但也有例外,我不知道如何使用引号和括号,对吗?
    • @Stewart:查看此链接:stackoverflow.com/questions/4162838/…
    • @tharaka:请更新您的问题,但遇到异常。
    • @tharaka:您是否使用 DB Console 测试过您的查询?
    【解决方案3】:

    使用标准尝试

    Criteria c = getSession().createCriteria(Euipment.class, "e"); 
    c.createAlias("e.quotation", "q"); // inner join by default 
    c.add(Restrictions.eq("q.supQuotation", id));       
    list = (List<Euipment>)c.list();
    

    【讨论】:

      猜你喜欢
      • 2013-01-11
      • 2012-01-26
      • 1970-01-01
      • 2018-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多