-->
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.  [ 2 posts ] 
Author Message
 Post subject: Postgresql function
PostPosted: Fri Apr 19, 2013 8:51 am 
Newbie

Joined: Fri Apr 19, 2013 8:45 am
Posts: 1
Hi,

I created a function named CreateTable.

Code:
-- Function: createtable(text)

-- DROP FUNCTION createtable(text);

CREATE OR REPLACE FUNCTION createtable(text)
  RETURNS numeric AS
$BODY$
BEGIN

EXECUTE 'Create table '||$1||'()';
return 0;   
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION createtable(text)
  OWNER TO postgres;



If I execute " createTable('XYZ') " in postgresql, it will create an empty XYZ table,

However if I run using java hibernate code, see below

Code:
String hql= "select createTable() from XTable ";
        Query query = session.createQuery(hql);
       
        List list = query.list();
       
        String nextValue = (String)list.get(0).toString();
        System.out.println(nextValue);


it won't work. Any idea?


Top
 Profile  
 
 Post subject: Re: Postgresql function
PostPosted: Sun Apr 21, 2013 11:40 pm 
Newbie

Joined: Sun Apr 21, 2013 12:35 pm
Posts: 4
In case you are using hibernate 3, please use callablestatement to execute your function like this:

Code:
Connection con = session.connection();  // obtain JDBC connection from Session object
        CallableStatement cs = con.prepareCall("{? = call createTable('XYZ') }");
        cs.execute();  // call function


Let me know if it works.

Regards,
Shomit


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