-->
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.  [ 6 posts ] 
Author Message
 Post subject: question about join-type functionality
PostPosted: Mon Aug 08, 2005 11:57 am 
Regular
Regular

Joined: Fri Jan 28, 2005 3:11 am
Posts: 81
A coworker of mine asked me the following question:

I am trying to just do a simple inner join on a table through hibernate to pull a count of how many rows there are. I had myeclipse create all my business objects and configuration mappings. My table structure is like this:

SdtProgramTable – primary key = ProgramID

IdmsPartProgTable – foreign key = ProgramID

In the SdtProgramTable mapping file eclipse created the following set for this relationship.
<set name="idmsPartProgTableSet" inverse="true">
<key column="PROGRAM_ID"/>
<one-to-many class="IdmsPartProgTable"/>
</set>

For the IdmsPartProgTable, eclipse created a IdmsPartProgTable class and a IdmsPartProgTableKey class with the following mappings.

<composite-id name="id" class="IdmsPartProgTableKey">
<key-many-to-one name="idmsPartTable" column="PART_ID" class="IdmsPartTable"/>
<key-many-to-one name="sdtProgramTable" column="PROGRAM_ID" class="SdtProgramTable"/>
</composite-id>

So I am not sure how to do a HQL query that will be like this in sql:

Select count(*) from IdmsPartProgTable, SdtProgramTable
Where sdtProgramTable.programId = idmsPartProgTable.programID;


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 08, 2005 12:24 pm 
Beginner
Beginner

Joined: Thu Mar 31, 2005 5:59 pm
Posts: 34
I'd think you'd just be able to do:

Code:
Query r = session.createQuery("select count(*) from IdmsPartProgTable");
int count = ((Integer)r.uniqueResult()).intValue();


Does that not work?

You can turn the generated SQL on and see what it actually does...

If that doesn't work, you can try adding the WHERE clause to the end of the select...


Top
 Profile  
 
 Post subject: sql?
PostPosted: Mon Aug 08, 2005 1:15 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Well, you could use SQL query like this

SQLQuery sqlQ = s.createSQLQuery( "select count(c.id) as cnt from ParentTable p, ChildTable c " +
"where c.u_id = p.id ");
sqlQ.addScalar("cnt", Hibernate.LONG);
Long o = (Long) sqlQ.uniqueResult();

I do not think that this has any portability implications since SQL is very standard and that classes are not going to be mapped to different tables in different databases.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 08, 2005 1:27 pm 
Regular
Regular

Joined: Fri Jan 28, 2005 3:11 am
Posts: 81
thanks, I passed on the info - She said the first one gets her what she was looking for - being that join is implicit when you do the mapping


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 08, 2005 1:30 pm 
Beginner
Beginner

Joined: Thu Mar 31, 2005 5:59 pm
Posts: 34
That's what I thought. Cool, glad she got it working!


Top
 Profile  
 
 Post subject: sql count
PostPosted: Wed Sep 28, 2005 10:32 am 
Newbie

Joined: Tue May 17, 2005 3:39 am
Posts: 3
How do I return count of resultset using createSQLQuery in Hibernate 2.x? The solution given above
viz,
SQLQuery sqlQ = s.createSQLQuery( "select count(c.id) as cnt from..")

doesnt work in Hibernate 2.x.


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