Java 在休眠中创建内部查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18380437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating inner query in hibernate
提问by Tharaka
how to add a set parameter() metheod inside the inner query in hibernate?
如何在 hibernate 的内部查询中添加 set parameter() 方法?
I have try to do like this but already have a errors
我尝试这样做,但已经有错误
this is my code
这是我的代码
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();
回答by Ankur Lathi
Execution of native SQL queries is controlled via the SQLQuery interface, which is obtained by calling Session.createSQLQuery().
本机 SQL 查询的执行是通过 SQLQuery 接口控制的,该接口通过调用 Session.createSQLQuery() 获得。
createQuery()
creates Query object using the HQL syntax.createSQLQuery()
creates Query object using the native SQL syntax.
createQuery()
使用 HQL 语法创建 Query 对象。createSQLQuery()
使用本机 SQL 语法创建 Query 对象。
So replace createQuery
with createSQLQuery
for native SQL query.
因此,更换createQuery
与createSQLQuery
原生SQL查询。
回答by Joe Taras
I've done some corrections about your query: 1. qt. supQuotation has a space, I've removed 2. euipment in you sub query haven't alias, I add qt
我对您的查询做了一些更正: 1. qt。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();
Tell me, if it's OK
告诉我,如果可以的话
If not OK, please post here the error, and check if you have put in hibernate mappping file your classes
如果不行,请在此处发布错误,并检查您是否已将您的类放入休眠映射文件中
回答by Prabhakaran Ramaswamy
Try with Criteria
尝试使用标准
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();