-->
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: Count using Criteria
PostPosted: Sun May 23, 2010 12:11 am 
Beginner
Beginner

Joined: Sat Oct 03, 2009 1:51 pm
Posts: 26
Hello!
How to transform this query using criteria?
Code:
public Long getTotalPhotos(Long idUser) {
      Long count = (Long) session.createQuery("select count(id_user) from Photo where user.id = :id")
      .setParameter("id", idUser)
      .uniqueResult();
      return count;
   }

Thanks for help!


Top
 Profile  
 
 Post subject: Re: Count using Criteria
PostPosted: Mon May 24, 2010 1:49 pm 
Beginner
Beginner

Joined: Sat Oct 03, 2009 1:51 pm
Posts: 26
Nobody? =/


Top
 Profile  
 
 Post subject: Re: Count using Criteria
PostPosted: Wed May 26, 2010 11:38 am 
Beginner
Beginner

Joined: Sat Oct 03, 2009 1:51 pm
Posts: 26
UP!


Top
 Profile  
 
 Post subject: Re: Count using Criteria
PostPosted: Thu May 27, 2010 1:45 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
Criteria crit = session.createCriteria(Photo.class);
crit.add(Restrictions.eq("id", id);
crit.setProjection(Projections.rowCount());
int count = ((Integer) crit.uniqueResult()).intValue();


Top
 Profile  
 
 Post subject: Re: Count using Criteria
PostPosted: Tue Jul 20, 2010 9:37 pm 
Beginner
Beginner

Joined: Sat Oct 03, 2009 1:51 pm
Posts: 26
I try, but not work.

This ID is for Photo?

Quote:
crit.add(Restrictions.eq("id", id);


This is the criteria:

Code:
public Integer getTotalPhotos(User user) {   
      
      Criteria crit = session.createCriteria(Photo.class);
      crit.add(Restrictions.eq("idUser", user.getIdUser()));
      crit.setProjection(Projections.rowCount());
       int count = ((Integer) crit.uniqueResult()).intValue();
      return count;
   }


The problem is the "id user" where is a FK (foreign key) in table Photo.

This is the message:
Quote:
root cause

org.hibernate.QueryException: could not resolve property: idUser of: br.com.imobiliaria.bean.Photo


How get total photos of the user with idUser or another column on Photo table?
Thanks!!


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.