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!