title: How to execute SQL Server Stored Procedure for INSERT from Hibernate?
body: <p>I have been able to execute a Stored Procedure in hibernate to map the result query into a Java Bean.</p>
  
  <p>For example, I have this file called Profesor.hbm.xml:</p>
  
  <pre><code>&lt;hibernate-mapping&gt;
          &lt;class name="model.Profesor" table="Profesor" catalog="dbo"&gt;
              &lt;id name="idProfesor" type="int"&gt;
                  &lt;column name="idProfesor" /&gt;
                  &lt;generator class="assigned" /&gt;
              &lt;/id&gt;
          &lt;property name="Nombre" type="string"&gt;
              &lt;column name="Nombre"  length="25" not-null="false" /&gt;
          &lt;/property&gt;
          &lt;property name="ApellidoP" type="string"&gt;
              &lt;column name="ApellidoP" length="20" not-null="false" /&gt;
          &lt;/property&gt;
  
          &lt;property name="ApellidoM" type="string"&gt;
              &lt;column name="ApellidoM" length="20" not-null="false" /&gt;
          &lt;/property&gt;
              &lt;/class&gt;
  
          &lt;sql-query name="getProfesors" callable="true"&gt;
          &lt;return alias="getProfesors" class="model.Profesor"&gt;
  
          &lt;return-property name="idProfesor" column="idProfesor"/&gt;
          &lt;return-property name="Nombre" column="Nombre"/&gt;
          &lt;return-property name="ApellidoP" column="ApellidoP"/&gt;
          &lt;return-property name="ApellidoM" column="ApellidoM"/&gt;
          &lt;/return&gt;
          exec getProfesors :idTT
          &lt;/sql-query&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>And then in my Profesor class I have the following code:</p>
  
  <pre><code>SessionFactory sf = new Configuration().configure().buildSessionFactory();
  session = sf.getCurrentSession();
  
  public List &lt;Profesor&gt; getProfesors(String idTT){
      session.beginTransaction();
  
      query.setString("idTT", idTT);
  
  return query.list();
  }
  </code></pre>
  
  <p>This works perfectly, I have no problem, the stored procedure executes and it populates my model Class named Profesor with the results.</p>
  
  <p>Now I have another stored procedure which INSERTs data to create a new Profesor in the Database.</p>
  
  <p>I tried something like this with no success:</p>
  
  <pre><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.executeUpdate();
  </code></pre>
  
  <p>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 &lt;"sql-query"> tag, there is a &lt;"sql-insert">
  but then I can't see the way to call that &lt;"sql-insert"> because it doesn't have a "name" attribute just like sql-query does.</p>
  
  <p>With &lt;"sql-query"> we could do:</p>
  
  <pre><code> Query query =session.getNamedQuery("getProfesors");
  </code></pre>
  
  <p>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.</p>
  
  <p>Any help would be really appreciated.</p>