-->
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: Using custom functions (Oracle) in HQL
PostPosted: Thu Jan 22, 2009 5:01 am 
Newbie

Joined: Wed Jan 14, 2009 8:28 am
Posts: 2
We'd like to do something but I'm not sure this is the best way or Hibernate can already do this at all.

In short: We have a database column which contains a value that can be used as a pattern to check against an input string.
We'd like to search the database and the tricky thing is that the user doesn't provide the pattern but only a value, which should be compared with the pattern stored in our database.

My brief Oracle-Solution:
- write a pl/sql-function with two input parameters (pattern, search_value), result is -1 for lower than pattern, 0 for exact match and +1 for greater than pattern.
- use the function in a query: select * from xy where checkpattern(column1, :param_search_value) = 0

What I'm looking for is a solution in Hibernate, so that we can use the pl/sql-checkpattern-function from HQL.

Let's say we've got the following tables:

Code:
MAIN_TABLE
OID   STATUS NAME
1116019 1  namevalue1
1116026 1  namevalue2
1116027 7  namevalue3
1116550 1  namevalue4
1116551 1  namevalue5
1116552 1  namevalue6

FROM_VALUES
OID  VALUE   POSITION
1116019 1        0
1116026 1   0
1116027 2   0
1116550 1   0
1116551 4   0
1116552 5   0

TO_VALUES
OID  VALUE   POSITION
1116019 9   0
1116026 3   0
1116027 4   0
1116550 15   0
1116551 6   0
1116552 8   0


The Tables MAIN_TABLE and FROM_VALUES/TO_VALUES join using OID (PK).
The column VALUE symbolizes the pattern, to have a simple example its just a numerical value.

Our SQL looks like this:
Code:
SELECT   m.oid, m.name, m.status,
         fv.value AS "from", tv.value AS "to"
    FROM MAIN_TABLE m, FROM_VALUES fv, TO_VALUES tv
   WHERE m.OID = fv.OID
     AND m.OID = tv.OID
     AND CHECK_PATTERN (fv.value, '4') <= 0
     AND CHECK_PATTERN (tv.value, '4') >= 0
ORDER BY m.oid


Code:
RESULTSET:

OID    NAME         STATUS              from          to
607818 namevalue1  1            3             5
707811 namevalue7  3            3             11
1517224      namevalue3  2             2             9
1011164      namevalue4  5            1             27
1011165      namevalue2  4            3             12
1115395      namevalue6  2             2             6



In HQL and POJO it should look like this:

Code:
public class MainTableVO {

private Long id;
private String name = null;
private String status = null;
private List fromValues = null;
private List toValues = null;
....
}


HBM MainTableVO:
Code:
<property name="oid" column="NAME" type="long"/>
<property name="name" column="OID" type="string"/>
<property name="status" type="string"/>


<list name="FromValues" table="FROM_VALUES" lazy="true" cascade="all">
      <key column="OID"/>
      <index column="POSITION"/>
      <composite-element class="com.myproject.utils.PatternValueWildcard">
                        <property name="Value" column="VALUE"/>
      </composite-element>
</list>


<list name="ToValues" table="TO_VALUES" lazy="true" cascade="all">
      <key column="OID"/>
      <index column="POSITION"/>
      <composite-element class="com.myproject.utils.PatternValueWildcard">
                  <property name="Value" column="VALUE"/>
      </composite-element>
</list>


Any ideas how we can make it work in HQL? I'm not forced to use a pl/sql-function but I think it makes sense, by this all expensive search operations are done in the database.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 22, 2009 6:12 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
In this chapter, it is described how to use stored procedures in hibernate. I hope that helps.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 24, 2009 8:58 pm 
Newbie

Joined: Wed Jan 14, 2009 8:28 am
Posts: 2
mmerder wrote:
In this chapter, it is described how to use stored procedures in hibernate. I hope that helps.


Code:
For Oracle the following rules apply:

    * A function must return a result set. The first parameter of a procedure must be an OUT that returns a result set. This is done by using a SYS_REFCURSOR type in Oracle 9 or 10. In Oracle you need to define a REF CURSOR type, see Oracle literature.


a) the function I'm going to use does return an integer value
b) it seems I'm supposed to call the function via native SQL

Does this mean I cannot combine the function and HQL query syntax?


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.