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