-->
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.  [ 1 post ] 
Author Message
 Post subject: Executing a SQL Server Stored Procedure from Hibernate
PostPosted: Thu Apr 05, 2012 7:55 am 
Newbie

Joined: Thu Apr 05, 2012 7:46 am
Posts: 1
I have been able to execute a Stored Procedure in hibernate to map the result query into a Java Bean.

For example, I have this file called Profesor.hbm.xml:

Code:

<hibernate-mapping>
        <class name="model.Profesor" table="Profesor" catalog="dbo">
            <id name="idProfesor" type="int">
                <column name="idProfesor" />
                <generator class="assigned" />
            </id>
        <property name="Nombre" type="string">
            <column name="Nombre"  length="25" not-null="false" />
        </property>
        <property name="ApellidoP" type="string">
            <column name="ApellidoP" length="20" not-null="false" />
        </property>

        <property name="ApellidoM" type="string">
            <column name="ApellidoM" length="20" not-null="false" />
        </property>
            </class>

        <sql-query name="getProfesors" callable="true">
        <return alias="getProfesors" class="model.Profesor">

        <return-property name="idProfesor" column="idProfesor"/>
        <return-property name="Nombre" column="Nombre"/>
        <return-property name="ApellidoP" column="ApellidoP"/>
        <return-property name="ApellidoM" column="ApellidoM"/>
        </return>
        exec getProfesors :idTT
        </sql-query>
</hibernate-mapping>


And then in my Profesor class I have the following code:

Code:
SessionFactory sf = new Configuration().configure().buildSessionFactory();
session = sf.getCurrentSession();

public List <Profesor> getProfesors(String idTT){
    session.beginTransaction();

    query.setString("idTT", idTT);

return query.list();
}


This works perfectly, I have no problem, the stored procedure executes and it populates my model Class named Profesor with the results.

Now I have another stored procedure which INSERTs data to create a new Profesor in the Database.

I tried something like this with no success:

Code:
session.beginTransaction();
Connection c = session.connection();
CallableStatement cs = c.prepareCall( "{call addProfesor(?,?,?)}" ) ;
cs.setString(1, "George");
cs.setString(2, "Williams");
cs.setString(3, "Mathematics");

cs.execute(); // Which always returns false



It doesn't even show me an error message, the data just won't insert. I was also reading that just like there is a <"sql-query"> tag, there is a <"sql-insert"> but then I can't see the way to call that <"sql-insert"> because it doesn't have a "name" attribute just like sql-query does.

With <"sql-query"> we could do:

Code:
Query query =session.getNamedQuery("getProfesors");


Just like I showed before, but since sql-insert doesnt have that attribute name I don't know how to do it. I'm also forced to use Stored procedures since it's a very special requirement, otherwise I would have used other Hibernate persistance features.


Any help would be really really appreciated!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.