-->
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: Query with criteria : problem with aggregation
PostPosted: Wed Nov 01, 2006 7:02 am 
Newbie

Joined: Thu Oct 19, 2006 9:37 am
Posts: 12
Hi everybody,

I'm testing the power of hibernate and the query building with criteria.
I built a test database with Book (id, title), Author(id, name), and an association table because some Book are written by several Author !
I mapped and logically, the class Book.java and Author.java contain respectively a Set of Author and Book.
I wrote this piece of code :

Code:
Session session = LibraryUtil.currentSession();
Criteria crit = session.createCriteria(Book.class);
crit.setFetchMode("authors", FetchMode.JOIN);
crit.setProjection(Projections.projectionList().add(Projections.count("authors")).add( Projections.groupProperty("title")));
List list = crit.list();
     
Iterator it = list.iterator();
while(it.hasNext())
{
   Object[] a = (Object[]) it.next();
   for(int i=0; i<a.length; i++)
   {
      System.out.println("-> "+a[1]+" : "+a[0]);
   }
}


I just want to get the number of author for each book and it gave me 1 for each. I did the contrary as well : the number of books for each author but it gave also the number 1 for each author.
I don't find any interesting topic on it on internet.
Do you have any idea ?
thanks a lot


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 4:52 am 
Newbie

Joined: Thu Oct 19, 2006 9:37 am
Posts: 12
up

Nobody ever tried this kind of request ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 5:45 am 
Newbie

Joined: Thu Oct 19, 2006 9:37 am
Posts: 12
please,

I just want to request with criteria this simple SQL code:

select book.title, count(writtenby.author_id)
from book, writtenby
where book.id=writtenby.book_id
group by 1


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.