-->
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: Any way to tell hibernate that to_char is valid?
PostPosted: Thu Aug 26, 2004 11:44 am 
Senior
Senior

Joined: Sun Oct 26, 2003 5:05 am
Posts: 139
I have a query that uses the function to_char(). I know it's not standard, but I can't figure out how to sort by year/month in reporting queries. This is the query I'm trying to use. Actually, the sql that it generates is valid as it works in my database admin tool (phpPgAdmin) for postgres 7.5.

Code:
               Query query = session.createQuery(
                   "select new com.upfactor.rns.resource.hibernate.report.TransactionCount( " +
                  "   to_char(history.date, 'YYYY-MM'), count(distinct rnsTransaction), count(history) ) " +
                  "from Administrator administrator " +
                  "   inner join administrator.businesses business " +
                  "   inner join administrator.persons person " +
                  "   inner join business.rnsTransactions rnsTransaction " +
                  "   inner join rnsTransaction.histories history " +
                  "where " +
                  "   business.id = :businessId and " +
                  "   person.id = :personId " +
                  "group by to_char(history.date, 'YYYY-MM')"
               );


Here is the postgres equivalent that does work:

Code:
select to_char(histories5_.date , 'YYYY-MM'), count(distinct rnstransac4_.id) as x0_0_, count(histories5_.id) as x1_0_ from administrator administra0_ inner join business businesses1_ on administra0_.id=businesses1_.administrator_id inner join administrator_to_person persons2_ on administra0_.id=persons2_.administrator_id inner join person person3_ on persons2_.person_id=person3_.id inner join rns_transaction rnstransac4_ on businesses1_.id=rnstransac4_.business_id inner join rns_transaction_history histories5_ on rnstransac4_.id=histories5_.rns_transaction_id where (businesses1_.id=2 ) group by  to_char(histories5_.date , 'YYYY-MM')


Anyway to tell hibernate that to_char() is okay?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 26, 2004 12:19 pm 
Senior
Senior

Joined: Sun Oct 26, 2003 5:05 am
Posts: 139
I fixed it. You guys might want to add the postgres functions to the dialect :) It all works now =)

Code:
      registerFunction( "to_char",
         new SQLFunction() {
            public Type getReturnType( Type type, Mapping mapping ) throws QueryException {
               return Hibernate.STRING;
            }

            public boolean hasArguments() {
               return true;
            }

            public boolean hasParenthesesIfNoArguments() {
               return true;
            }
         }
      );


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 27, 2004 8:11 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Submit a JIRA feature request (best include a patch file)


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.