-->
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.  [ 6 posts ] 
Author Message
 Post subject: Runtime parameters in formula property mapping
PostPosted: Tue Feb 21, 2006 12:43 pm 
Newbie

Joined: Tue Feb 21, 2006 12:36 pm
Posts: 3
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

version 3.0.5

Is there a way to create a formula property mapping where I can pass runtime parameters to the formula?

I want to call an oracle function that takes in a longitude and latitude and calculates the distance. Here is the mapping of the field:

Code:
<property name="distance" formula="DISTANCE(?, ?, addr.longitude, addr.latitude) distance" />


It's easy enough to create hql/sql but I would like to use the criteria object to create my where clause instead of creating stringbuffer and appending the sql together.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 21, 2006 11:40 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
No, but you can refer to columns accessible in the query, or put a subselect in there.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 22, 2006 12:24 am 
Newbie

Joined: Tue Feb 21, 2006 11:30 pm
Posts: 3
Location: Argentina
Try turning on the show-sql attribute into your hibernate configuration and then lookup into the console for the hsql alias for your table, generaly it is class_name0_ (calssname+zero+underscore) and use it into your formula, but dont use wildcards (?).

<property name="show_sql">true</property>


<property name="distance" formula="DISTANCE(ADDR0_.longitude, ADDR0_.latitude)" />

If you dont use the hsql alias, you can put a subselect which is executed directly on the db.

By Exaple:

<property name="date" formula="(SELECT SYSDATE FROM DUAL)" />

_________________
Alejandro Vidal Quiroga


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 22, 2006 12:31 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You mean HQL, right? dstockmann didn't say what DBMS he's using, but it's probably not HSQLDB. HSQL is HSQLDB's dialect of SQL. HQL is Hibernate's query language.

Don't try to guess HQL-generated SQL aliases. They are allowed change without warning, and you'll be left stumped trying to fix something like that, if the change happens next year, long after you've forgotten what awful hackery you did that long ago.

You are allowed to use column names from any joined table, so long as they're not ambiguous (if you join two tables that have a column called "description", then you can't use "description" in your SQL formula),


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 22, 2006 11:01 am 
Newbie

Joined: Tue Feb 21, 2006 12:36 pm
Posts: 3
I am using Oracle, unfortunately I can not use another column in that distance function. Both the question marks (?) are place holders for variables that I need to pass the sql, similar to a preparedstatement.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 22, 2006 4:34 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Then it looks like you'll have to use JDBC directly for that part. i imagine that the function call is completely read-only, so there's no downside to just getting the session's JDBC connection and creating your own PreparedStatement with it. Don't close the connection afterwards, let the session do that.


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