Dashboard > iBATIS DataMapper > Home > Frequently Asked Questions > Why can't I get selecyKey and sequences to work properly with Oracle
Why can't I get selecyKey and sequences to work properly with Oracle
Added by Mario Alberto Montoya Martínez, last edited by Mario Alberto Montoya Martínez on Apr 17, 2006  (view change)
Labels: 
(None)


It seems there's an omition in the iBATIS SQL Maps Documentation. According to it the selectKey stanza would have the following syntax:

<selectKey resultClass="int">
  SELECT SEQUENCENAME.NEXTVAL AS ID FROM DUAL
</selectKey>

But it won't work as it always inserts zero in your insert table in the id column. This would cause a Primary Key constraint violated, if you defined one.

The correct syntax is:

<selectKey resultClass="int" keyProperty="id">
  SELECT SEQUENCENAME.NEXTVAL AS ID FROM DUAL
</selectKey>

With this new attribute (keyProperty) your're telling iBATIS that "id" is the name for you inline-parameter in your insert query. For example:

<insert id="insertProduct-ORACLE" parameterClass="com.domain.Product">
  <selectKey resultClass="int" keyProperty="id">
    SELECT STOCKIDSEQUENCE.NEXTVAL AS ID FROM DUAL
  </selectKey>
  insert into PRODUCT (PRD_ID,PRD_DESCRIPTION)
  values (#id#,#description#)
</insert>

Here, #id# is the inline-parameter.

Please, correct me if I'm wrong, since I'm not an iBATIS developer, just a Java user.

Regards!

Site running on a free Atlassian Confluence Open Source Project License granted to OSS. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.5 Build:#811 Jul 25, 2007) - Bug/feature request - Contact Administrators