-->
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.  [ 2 posts ] 
Author Message
 Post subject: How to get year from a date field
PostPosted: Fri Feb 18, 2011 3:18 pm 
Newbie

Joined: Fri Feb 18, 2011 3:04 pm
Posts: 2
Hello,

I've got a entity named "pj" and I want to extract only the year of one field and get all the years on a list. How to do that using detachedcriteria? I'm using postgre. See the sql:

Code:
select extract(year from pj.dt_criacao) as year
from tb_pj pj
group by extract(year from pj.dt_criacao)
order by year desc


I was starting using this...
Code:
DetachedCriteria criteria = DetachedCriteria.forClass(Pj.class, "pj");
criteria.setProjection(Projections.groupProperty("extract(year from dt_criacao)")); --> from this point I don't know how to do...
List<Integer> lista = getDao().getEntityManager().findByCriteria(criteria);


Thanks a lot


Top
 Profile  
 
 Post subject: Re: How to get year from a date field
PostPosted: Tue Feb 22, 2011 3:46 pm 
Newbie

Joined: Fri Feb 18, 2011 3:04 pm
Posts: 2
Well,

I resolved that problem using this method:

DetachedCriteria dc = DetachedCriteria.forClass(Pj.class,"pj");
dc.add(Restrictions.sqlRestriction(" extract(year from {alias}.dt_criacao) = ? ", YearParam,Hibernate.INTEGER));
... findByCriteria(dc);


Thanks anyway...


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