-->
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.  [ 5 posts ] 
Author Message
 Post subject: Hibernate HQL select Query
PostPosted: Tue Jul 11, 2006 2:20 am 
Newbie

Joined: Mon Jul 10, 2006 10:26 am
Posts: 3
Location: Hyderabad
Hi,
I'm getting a java.lang.NullPointerException when i'm executing the following query.
String query ="select XYZBEan from XYZBEan"
+ "where corrd = :corrId"
+" and status = :status";
Query q = sess.createQuery(query);
q.setParameter("corrd ",corrd );
q.setInteger(status,status);
q.executeUpdate();


here the XYZBean is a HibernateBean. I'm new to Hibernate, need some guidance on how to write HQL queries....
Thanks in advance.
Kris


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 2:28 am 
Pro
Pro

Joined: Mon Jan 24, 2005 5:39 am
Posts: 216
Location: Germany
Hi,

shouldnt it be

Quote:
q.setInteger(":status",status);

instead of:

Quote:
q.setInteger(status,status);


?

If not, than you should show us your stacktrace.

_________________
dont forget to rate !


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 4:00 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Have you read this ? : http://www.hibernate.org/hib_docs/refer ... ryhql.html

Quote:
shouldnt it be
q.setInteger(":status",status);

Yes, but without the colon (:)

And about the query itself, don't call executeUpdate() on a select.

Your code should look like :
Code:
String query ="from XYZBEan"
          + "where corrd = :corrId"
          +" and status = :status";
Query q = sess.createQuery(query);
q.setParameter("corrId ",corrd );
q.setInteger("status",status);
List l = q.list();


The list l will contain the result of the query, an instance of XYZBean per iteration.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Last edited by batmat on Wed Jul 12, 2006 2:31 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 4:22 am 
Newbie

Joined: Mon Jul 10, 2006 10:26 am
Posts: 3
Location: Hyderabad
Hi,
Thanks for a quick reply.
I mistyped the code. I actually has a ':' bofore status.
I resolved the problem by using the following code

[i]List corrDetailsList = sess.createCriteria(XYZBean.class,"corrDet").add(Restrictions.sqlRestriction("xId = '"+xId+"' and status = 0")).list();[/i]

Once again thanks for posting your comments for my question.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 8:12 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
kris_b2k wrote:
Hi,
Code:
List corrDetailsList = sess.createCriteria(XYZBean.class,"corrDet").add(Restrictions.sqlRestriction("xId = '"+xId+"' and status = 0")).list();


I feel it's a bit pity that you use sql instead of a non-native one. Is xId the name of your column or the name of your property in your object. If it's the name of the column of the db table, then I think you should try to avoid doing like this.

See Restrictions.and() to create your criteria query.

Please rate the answers...

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.