-->
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.  [ 1 post ] 
Author Message
 Post subject: Function calls in Criteria-based queries
PostPosted: Fri Sep 29, 2006 1:10 pm 
Regular
Regular

Joined: Wed Feb 15, 2006 9:09 pm
Posts: 76
Hibernate version: 3.2.0cr2

Mapping documents: (excerpt)

Code:
<property name="activeStart" column="active_start_date" type="date" not-null="true" />
<property name="activeEnd" column="active_end_date" type="date" />


Code between sessionFactory.openSession() and session.close():

Code:
Date now = ... (date with time truncated) ...
Integer resultCount = (Integer)session.createCriteria( MyEntity.class )
  .add( Restrictions.sqlRestriction(
    "date_between_range( ?, {alias}.active_start_date, {alias}.active_end_date ) = ?",
    new Object[] { now, 1 },
    new Type[] { Hibernate.DATE, Hibernate.INTEGER } ) )
  .setProjection( Projections.rowCount() )
  .uniqueResult();


Name and version of the database you are using: Oracle 10gR2

The generated SQL (show_sql=true):

Code:
Hibernate: select count(*) as y0_ from blah this_ where date_between_range( ?, this_.active_start_date, this_.active_end_date ) = ?


Oookie, so here we go. The above solution works, but I'm wondering if there's a substantially better one. Specifically, I need to write the SQL myself (and refer to column names directly from the database). I have a database function that takes 3 arguments: when, start, and end, all dates, and determines whether or not "when" is between "from" and "end" (inclusive, and also accommodating for null values in "end"). It returns 0 or 1 for false and true, respectively.

Actually this wasn't working for a coworker at first, but I got half way through this post and figured out what the problem was (an extraneous closing brace or something silly), but decided to post anyways to see if I was completely missing an existing capability in the Criteria framework. Any ideas?

Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.