-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Can i Use parametric in Query in HQL
PostPosted: Mon Jul 21, 2008 4:03 pm 
Newbie

Joined: Sat Jul 12, 2008 4:17 am
Posts: 9
Hello
Can any buddy tell me i can use parametric in Query in HQL or not


example
select abc.name from User abc where abc.userId in (:user);

Thanks


Top
 Profile  
 
 Post subject: Re: Can i Use parametric in Query in HQL
PostPosted: Mon Jul 21, 2008 4:53 pm 
Newbie

Joined: Mon Apr 21, 2008 5:07 pm
Posts: 6
haroonob wrote:
Hello
Can any buddy tell me i can use parametric in Query in HQL or not


example
select abc.name from User abc where abc.userId in (:user);

Thanks


hello haroonob

It seems like it should be possible. When you look into chapter 14 at page http://www.hibernate.org/hib_docs/refer ... ryhql.html
then you can find an example I modified a little bit and it should work ;)

Code:
String hql = "select order.id, sum(price.amount), count(item)
from Order as order
    join order.lineItems as item
    join item.product as product,
    Catalog as catalog
    join catalog.prices as price
where order.paid = false
    and order.customer = :customer
    and price.product = product
    and catalog = :currentCatalog
group by order
having sum(price.amount) > :minAmount
order by sum(price.amount) desc";

Query query = session.createQuery(hql);
query.setEntity("customer",customer);
query.setEntity("currentCatalog",currentCatalog);


Look also: http://www.hibernate.org/hib_docs/v3/ap ... Query.html

Here you can also find an example:
http://www.java2s.com/Code/Java/Hiberna ... meters.htm

Hope it helped you ..
see you


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 21, 2008 10:33 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Take a look at this little tutorial on mastering Hibernate Query Language, HQL:

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=08masteringhqlandnamedqueries

From that example, you see how a hard coded HQL Query:

Code:
Session session = HibernateUtil.beginTransaction();
String hql="from User where loginName = 'mj' ";
Query query = session.createQuery(hql);
Object o  = query.uniqueResult();
User u = (User)o;
System.out.println(u.getLoginName());
System.out.println("\n\n");
HibernateUtil.commitTransaction();


Can instead be parameterized and initialized, very similar to how a typical SQL PreparedStatement might do in in JDBC:

Code:
Session session = HibernateUtil.beginTransaction();
String loginName = "mj";
String hql="from User where loginName = :name";
Query query = session.createQuery(hql);
query.setString("name", loginName);
Object o = query.uniqueResult();
User user = (User)o;
System.out.println(user.getLoginName());
HibernateUtil.commitTransaction();


The tutorial even shows how you can do it as a Hibernate3 NamedQuery:

Code:
@NamedQuery(name="user.findByLoginName",   
     query="from User where loginName = :name")


Hope that helps you Hibernate!

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.