-->
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.  [ 5 posts ] 
Author Message
 Post subject: Working Oracle stored procedures?
PostPosted: Mon Dec 20, 2010 1:31 pm 
Regular
Regular

Joined: Thu Oct 19, 2006 12:07 pm
Posts: 75
Hi!

I googled the entire afternoon, but could not find a single working example of using stored procedures with Hibernate.

I use Hibernate 3.6.0 and Oracle 10g Express Edition (v10.2.0.1.0).

Basically I am trying to implement loader, sql-insert, sql-update, sql-delete and generic named queries that use stored procedures, as we do not want to allow SQL execution on the database, but only accessing stored procedures.

Did anyone succeeded doing this with the mentioned software versions?
I managed to do it a while ago using PostgreSQL, but of course Oracle is a bit different.

Many thanks for any clues!

Regards,
David


Top
 Profile  
 
 Post subject: Re: Working Oracle stored procedures?
PostPosted: Wed Dec 22, 2010 4:17 pm 
Regular
Regular

Joined: Wed Feb 15, 2006 9:09 pm
Posts: 76
Try extending the Oracle10gDialect class that ships with Hibernate and specify your class as your dialect. Then you can override methods there and add your own functions. For more complicated things (e.g. stored procs that don't return anything so you can't use them in a select statement), you're probably going to need to use some trickery with createSQLQuery() and SQLQuery's addScalar() method.


Top
 Profile  
 
 Post subject: Re: Working Oracle stored procedures?
PostPosted: Mon Jan 03, 2011 6:16 am 
Regular
Regular

Joined: Thu Oct 19, 2006 12:07 pm
Posts: 75
So the examples in the docs and various books were never actually tested... Nice going.


Top
 Profile  
 
 Post subject: Re: Working Oracle stored procedures?
PostPosted: Tue Jan 04, 2011 9:28 am 
Regular
Regular

Joined: Thu Oct 19, 2006 12:07 pm
Posts: 75
Oops!

I found I had a missing comma in my SQL which caused trouble.
Now I have functioning stored procedures. I will post more info later.


Top
 Profile  
 
 Post subject: Re: Working Oracle stored procedures?
PostPosted: Thu Jan 13, 2011 11:13 am 
Regular
Regular

Joined: Thu Oct 19, 2006 12:07 pm
Posts: 75
If it helps anyone, I successfully used the following:

Code:
<class name="Star" table="STARS">
    <id name="id" column="id"/> <!-- Long -->
    <property name="name"/> <!-- String -->
    <property name="mass"/> <!-- Double -->

    <loader query-ref="loadStarSP"/>
    <sql-insert check="none" callable="true">{call USER1.createStarHB (?, ?, ?)}</sql-insert>
    <sql-update check="none" callable="true">{call USER1.updateStarHB(?,?,?)}</sql-update>
    <sql-delete check="none" callable="true">{call USER1.deleteStarHB(?)}</sql-delete>
</class>

<sql-query name="loadStarSP"  callable="true">
    <return class="Star"/>
    { ? = call USER1.loadStarHB(?)}
</sql-query>

<sql-query name="selectAllStars"   callable="true" >
    <return  class="Star"/>
    { ? = call USER1.selectAllStars()}
</sql-query>


Code:
CREATE OR REPLACE FUNCTION loadStarHB(  in_id in STARS.ID%type)
RETURN SYS_REFCURSOR  AS 
    st_cursor SYS_REFCURSOR; 
BEGIN 
  OPEN st_cursor FOR 
    SELECT *  FROM STARS WHERE  ID = in_id; 
  RETURN  st_cursor; 
END; 

CREATE OR REPLACE PROCEDURE createStarHB(
    in_name in STARS.NAME%type,
    in_mass in STARS.MASS%type,
    in_id in STARS.ID%type)
AS
BEGIN
  INSERT INTO STARS (NAME, MASS, ID) VALUES ( in_name, in_mass, in_id );
END;

CREATE OR REPLACE PROCEDURE updateStarHB(
    in_name in STARS.NAME%type,
    in_mass in STARS.MASS%type,
    in_id in STARS.ID%type)
AS
BEGIN
  update STARS set
    NAME=in_name, MASS=in_mass
  where ID=in_id;
END;

CREATE OR REPLACE PROCEDURE deleteStarHB(
  in_id in STARS.ID%type
) AS
BEGIN
  delete from STARS where ID=in_id;
END;

CREATE OR REPLACE FUNCTION selectAllStars()
    RETURN SYS_REFCURSOR
AS
    st_cursor SYS_REFCURSOR;
BEGIN
    OPEN st_cursor FOR
SELECT *
FROM STARS;
      RETURN  st_cursor;
END;


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