-->
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: Hibernate 4 and stored procedures for inserting records
PostPosted: Sun Feb 26, 2012 6:18 am 
Newbie

Joined: Sat Feb 18, 2012 11:31 am
Posts: 5
hello all, I need help with a stored procedure for inseting records in a postgres table:
i have the mapping file whith this configuration:
<hibernate-mapping>
<class name="blom.log.model.xhostclass" table="x_hosts">
<id name="iId" column="pk_codehost" type="int" unsaved-value="-1">
<generator class="identity">
</generator>
</id>
<property name="sHost" type="java.lang.String" access="field">
<column name="d_host" />
</property>

<sql-insert callable="true" check="none">
{call insert_host(:dHost)}
</sql-insert>


</class>
</hibernate-mapping>


My problem is that i don't know how to refer that in java code, how to execute the procedure. I'm trying something like

Transaction tx = null;
tx = session.beginTransaction();

Query query = session.getNamedQuery("insert_host");
query.setString("dHost", "host1");
query.executeUpdate();

but the error is Named query not known: insert_host

Any help please???
thanks in advance.


Top
 Profile  
 
 Post subject: Re: Hibernate 4 and stored procedures for inserting records
PostPosted: Mon Feb 27, 2012 4:27 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi acalle,

named queries are something completely different..

Your stored procedure should be called automatically when you persist (and then flush) new xhost instances:

Code:
xhostclass xhost = new xhostclass();
session.persist(xhost);
session.flush();


Furthermore you must declare some parameter (? placeholders) for your stored procedure,
otherwise how your your procedure know which values for pk_codehost and d_host to use ?!


Top
 Profile  
 
 Post subject: Re: Hibernate 4 and stored procedures for inserting records
PostPosted: Mon Feb 27, 2012 2:25 pm 
Newbie

Joined: Sat Feb 18, 2012 11:31 am
Posts: 5
hi expert, thanks for your reply, I know, whit that insert I am overwriting the default method for insert records.

But, I have another question, If I have a stored procedure (postgresql) that looks for a record description, If it isn't in the BBDD, then the function insert it and returns an integer with the id, something like:

Code:
CREATE OR REPLACE FUNCTION insert_host5(description character varying)
  RETURNS integer AS
$BODY$
BEGIN
   IF NOT EXISTS
                (SELECT h.pk_codehost from public.x_hosts h WHERE h.d_host=description) THEN
                               INSERT INTO public.x_hosts
                               (
                                               d_host
                               )
                               VALUES
                               (
                                               description
                               );
                               
                
                               
         END IF;
       RETURN CURRVAL('x_hosts_pk_codehost_seq');

END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION insert_host5(character varying)
  OWNER TO postgres;

in my hbm.xml I have the next:

Code:
<sql-query name="callHostStoreProcedure">
      <return-scalar column="pk_codehost" type="integer"/>
      {? = call insert_host5(:description)}
      <!--{? = Select * from insert_host5(:description) as t1}-->
    </sql-query>

But when I execute it from java:

Code:
[b]Query query = session.getNamedQuery("callHostStoreProcedure2");
query.setString("description", sHost);
query.setResultTransformer(Transformers.aliasToBean(blom.log.model.xhost_sp.class));
List something = query.list();[/b]


I gives me an error:
org.hibernate.QueryException: Expected positional parameter count: 1, actual parameters: [] [{? = Select * from insert_host5(:description)}]

Any help please, for execute that function in hibernate

Thanks in advance


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.