-->
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: How to use {fn ...} and "undefined alias" problem
PostPosted: Tue Feb 03, 2004 12:05 pm 
Beginner
Beginner

Joined: Wed Dec 03, 2003 10:59 am
Posts: 47
Hello,

I am trying to select a list of unique dates from a table, and I want those dates rounded to a day.
Example:
2004
2003
....

I am trying to use the following HQL:

Code:
            Query q = ses.createQuery("select "
                + " {fn date_part('year',  table.addDate)}"
                + " from uus "
                + " join uus.userInfo.foos as foos"
                + " where uus.bar.id =:barId"
                + " order by"
                + " {fn date_part('year',  table.addDate)} desc");


This is run against a PostgreSQL database, which provides date_part() function.
The above query does not work in Hibernate, though, because of bad syntax:

Caused by: net.sf.hibernate.QueryException: undefined alias: {fn select {fn date_part(........

I thought, after reading/searching this forum, that {fn ....} would do it.
How do I run queries that use database functions from Hibernate?
I found a few posts that mentioned {fn ...} syntax, but this does not seem to work for me (Hibernate 2.1 final).
I also saw Christian's post from October 2003 that says this is in the docs, but I haven't been able to find it.

Can you see something wrong with my query?

Thank you,
Otis


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 03, 2004 12:59 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You can use custom database functions everywhere in your HQL except in the select part by just using them as you would in SQL. Don't know about the {fn} stuff though. (should not be too difficult to do the year-part in java manually)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 03, 2004 1:30 pm 
Beginner
Beginner

Joined: Wed Dec 03, 2003 10:59 am
Posts: 47
The problem is that I cannot use 'order by XXX' if I don't have XXX in the select part.

I have tried working around that by using Session's createSQLQuery method, like this:

Code:
            Query q = ses.createSQLQuery("select distinct"
                + " date_part('year', uu.add_date) as year from uus inner join user_info ui on uus.user_id=ui.id inner join uu on ui.id=uu.user_id where (uus.suid=1 ) order by  date_part('year', uu.add_date)", "addDates", Calendar.class);


The problem here is the 'Calendar.class' part.
According to the Javadocs, this has to be a persistable class.
Using java.util.Calendar class there (or any other regular Java class) results in:

Caused by: net.sf.hibernate.MappingException: No persister for: java.util.Calendar


However, I don't want that. I just want a Collection of 'YYYY' Strings back. :)

Is there any way to do this with Hibernate?

Thanks,
Otis


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.