Tharaka
Tharaka

Reputation: 199

Creating inner query in hibernate

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

Upvotes: 3

Views: 17128

Answers (3)

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26084

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

Upvotes: 0

Joe Taras
Joe Taras

Reputation: 15399

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

Upvotes: 5

Ankur Lathi
Ankur Lathi

Reputation: 7836

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.

Upvotes: 1

Related Questions