-->
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: createQuery - count(*) returning null
PostPosted: Wed Mar 30, 2005 12:48 pm 
Beginner
Beginner

Joined: Fri Oct 01, 2004 7:13 am
Posts: 20
Hibernate version:2.1.3

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:postgres 8.0.0

The generated SQL (show_sql=true):select count(*) as x0_0_ from contacts contact0_ where (UserId=1893 )

I have a very simple count query. I have run the following :
Code:
        String sql = "from Contact where UserId = 1893";
       
        Query query = sess.createQuery(sql);
       
        Long counter = (Long) query.uniqueResult();

Which correctly complains that it is not a unique result as there are two rows returned. To obtain the count I have tried to execute the following but it returns null from the uniqueResult().
Code:
        String sql = "SELECT count(*) from Contact where UserId = 1893";
       
        Query query = sess.createQuery(sql);
       
        Long counter = (Long) query.uniqueResult();

The resultant sql below correctly returns a count of 2.

select count(*) as x0_0_ from contacts contact0_ where (UserId=1893 )

This seems so simple! Any idea what I am missing?

Thanks[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 30, 2005 12:52 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Use of aliases is required in HB2


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 30, 2005 1:15 pm 
Beginner
Beginner

Joined: Fri Oct 01, 2004 7:13 am
Posts: 20
Thanks Gavin, I was not aware of that.

I have updated the sql to be

[code] String sql = "String sql = "SELECT count(*) from Contact contact where contact.user = 1893";[code]

The resultant sql is now :

select count(*) as x0_0_ from contacts contact0_ where (contact0_.userid=1893 )

but the result is still null? But executes correctly in raw sql as 2.

Getting closer.

Thanks


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.