3

how to add a set parameter() metheod inside the inner query in hibernate?

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();
3
  • my error is "-----Hibernate: select eq.euipmentName,eq.type from Euipment eq where eq. id in(select euipment from Quotation qt where qt. supQuotation=?) Unknown column 'qt.supQuotation' in 'where clause' ---- " Commented Aug 22, 2013 at 12:45
  • my error is --Hibernate: select eq.euipmentName,eq.type from Euipment eq where eq. id in(select euipment from Quotation qt where qt. supQuotation=?) Unknown column 'qt.supQuotation' in 'where clause'-- Commented Aug 22, 2013 at 12:46
  • Test your query on your database console....Then try using Hibernate Commented Aug 22, 2013 at 12:49

3 Answers 3

5

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

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

Sign up to request clarification or add additional context in comments.

Comments

1

From Hibernate Documentation:

Execution of native SQL queries is controlled via the SQLQuery interface, which is obtained by calling Session.createSQLQuery().

  • createQuery() creates Query object using the HQL syntax.
  • createSQLQuery() creates Query object using the native SQL syntax.

So replace createQuery with createSQLQuery for native SQL query.

6 Comments

What gives the query away as being SQL rather than HQL ?
Dear sir I tried, but also have a exception, I dont know how to use a quotation and parentheses, is it rihgt?
@Stewart: Check this link: stackoverflow.com/questions/4162838/…
@tharaka: Please update your question with exception you are getting.
@tharaka: Have you tested your query with DB Console?.
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.