Inserting / Updating NULL values in iBatis

Inserting or Updating nulls in iBatis should be done in a crewked way. One obvious fix is to put the isNotNull tag before the fields which can be null. But this will make the sqlmap file cumbersome or hard to read. For eg:, the below insert query will throw error, if you are trying to insert a null value for, say, infoId.

<insert id=”insertInformation” parameterClass=”Information”>
insert into info (Key, Name, InfoId)
values (#key#, #name#, #infoId#)
</insert>

We can put isNotNull tage like this :

<insert id=”insertInformation” parameterClass=”Information”>
insert into info (Key, Name, InfoId)
values (#key#, #name#
<isNotNull property=”infoId”>
, #infoId#
</isNotNull>
)
</insert>

Now doesn’t that became too much. So here comes the ultimate fix. You just need to put the Oracle datatype of the column separated by a colon in the statment like this :

#infoId:VARCHAR#

The complete sql tag will become like this :

<insert id=”insertInformation” parameterClass=”Information”>
insert into info (Key, Name, InfoId)
values (#key#, #name#, #infoId:VARCHAR#)
</insert>

Similarly the update statement also can be modified like this :

<update id=”updateInformation” parameterClass=”Information”>
update Info set
Key = #key#,
Name = #name#,
infoId = #infoId:VARCHAR#
where
id = #id#
</update>

About these ads

Tags: , , , , , , , ,

One Response to “Inserting / Updating NULL values in iBatis”

  1. Mao Duy Says:

    Thansk your article, it saves me. Very clear to understand!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

Join 95 other followers

%d bloggers like this: