2013-08-22 55 views
2

如何在休眠状态下在内部查询中添加一个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(); 
+1

什么是你的错误尝试? – Alex

+0

我的错误是“----- Hibernate:select eq.euipmentName,eq.type from Euipment eq where eq。id in(select Quotes qt where qt。supQuotation =?的设备) 未知列'qt.supQuotation'in 'where clause'----“ – Dinesh

+0

我的错误是--Hibernate:选择eq.euipmentName,eq.type from Euipment eq where eq。 ID in(从QT中选择设备,其中qt。supQuotation =?) 'where子句'中的未知列'qt.supQuotation' - – Dinesh

回答

2

Hibernate Documentation:

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

  • createQuery()创建使用HQL语法查询对象。
  • createSQLQuery()使用本机SQL语法创建Query对象。

所以更换createQuerycreateSQLQuery对本地SQL查询。

+0

什么使得查询为SQL而不是HQL? – Stewart

+0

尊敬的先生,我尝试过,但也有例外,我不知道如何使用引号和括号,它是rihgt? – Dinesh

+0

@Stewart:检查此链接:http://stackoverflow.com/questions/4162838/hql-vs-sql-hibernate-netbeans-hql-editor –

3

我对你的查询做了一些更正: 1. qt。 supQuotation有一个空间,我已经删除 2.设备NASDAQ在你的子查询中没有别名,我添加了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(); 

告诉我,如果它是OK

如果不正确,请张贴在这里的错误,并检查是否已处于休眠mappping文件的类

0

与标准

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();