Friday, December 22, 2006

Using a Stored Procedure to Insert Records with Spring & Hibernate

It took a little research to discover how to integrate a stored procedure for inserting records into my application. However, once I found the right piece of information, it became easy. Here is what I am using:

<hibernate-mapping>
   <class name="com.codebits.vo.SuperSimpleRecord" table="super_simple">
       <id name="id" type="string" length="32" unsaved-value="null">
           <generator class="uuid.hex" />
       </id>
       <property name="name" type="string" length="100" not-null="true" />
       <property name="count" type="integer" not-null="false" />
       <property name="created" type="date" not-null="true" insert="false" />
       <sql-insert callable="true" check="none">
         {call insert_record(?, ?, ?)}
       </sql-insert>
   </class>
</hibernate-mapping>

Notice the check attribute? That's what took me awhile to find, I think it was introduced in Hibernate 3.2. Just for completeness, here is my stored procedure:

create or replace procedure insert_record(
  in_name in varchar2, in_count in number, in_id in varchar2
) as begin
  insert into super_simple
  values(in_id, in_name, in_count, sysdate);
  commit;
end insert_record;
Post a Comment