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: select count(*) returns multiple rows......
PostPosted: Wed Nov 02, 2005 10:08 pm 
Newbie

Joined: Wed Nov 02, 2005 9:55 pm
Posts: 1
<class name="Person" table="PERSON">
<id name="id"/>
<property name="sex"/>
<property name="weight"/>
</class>

<class name="Employee" table="EMPLOYEE">
<id name="keyid"/>
<property name="skills"/>
</class>

==============
public class Person
{
String id = null;
String sex = null;
String weight = null;
/* getters and setters */
}

public class Employee extends Person
{
String keyid = null;
String skills = null;
/* getters and setters */
}

===============
When I perform the following query:
Query q = sess.createQuery("select count(*) from Person");
int total = ((Integer)q.uniqueResult()).intValue();

Above HQL statement throws a NonUniqueObjectException. It actually returns two records..... The first record is 0 and the second record is the correct total number of rows.
However if I removed the Employee definition in the mapping file, it works fine without problem.

Any Idea?
I am very new to hibernate, any info would be grateful.
Thanks.


Top
 Profile  
 
 Post subject: show_sql
PostPosted: Wed Nov 02, 2005 10:22 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Can you set show_sql="true" and post the sql output?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 02, 2005 11:42 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Sure, Hibernate issues two seperate selects (one for Person, one for Employee). Try maybe using union-subclass to map this inheritence instead.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 1:44 am 
Beginner
Beginner

Joined: Sat Oct 22, 2005 11:16 pm
Posts: 40
Essentially Hibernate is doing:

Code:
select count(*) from person;
select count(*) from employee;


and returning these as lists. SQL has tables, and select count(*) from a table in SQL will always return exactly one row, but HQL is based on objects, which don't always correspond with tables.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 1:46 am 
Beginner
Beginner

Joined: Sat Oct 22, 2005 11:16 pm
Posts: 40
To put it another way, if you did a HQL query like this:
Code:
select Object o;


you would select every object in the system. Of course that query would involve every table in the system. You are basically doing
Code:
select Person p
which must look at both Person and Employee.


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.