You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Guy Rouillier <gu...@burntmail.com> on 2009/10/05 06:14:41 UTC

Select one set null instead of throwing exception

Currently, performing a single row select throws an exception if zero or 
  > 1 rows are returned.  While this is certainly workable, an 
alternative that would be easier on the programmer would be to set the 
result to null if no rows are found, and only throw an exception if > 1 
rows are found.

The documentation suggests doing a count to determine what situation you 
are in, then issuing the appropriate select once you know.  But that 
will result in an extra trip to the DB.  In a common scenario where you 
are querying a primary key, you know that you will get zero or one row 
back.  So rather than having to put the single row select into a 
try-catch block, a more natural paradigm would be to assign the result 
and then check the result for null.

-- 
Guy Rouillier

---------------------------------------------------------------------
To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
For additional commands, e-mail: user-java-help@ibatis.apache.org


Re: Select one set null instead of throwing exception

Posted by Clinton Begin <cl...@gmail.com>.
We've removed that exception anyway.  :-)

Popular community vote.

On Mon, Oct 5, 2009 at 12:07 AM, Guy Rouillier <gu...@burntmail.com>wrote:

> Ignore this.  I realized after taking a break that it doesn't work with
> primitive types.  I think the thing I find most frustrating with Java is
> that it doesn't treat primitive types in a consistent manner with classes
> derived from Object.
>
>
> Guy Rouillier wrote:
>
>> Currently, performing a single row select throws an exception if zero or
>>  > 1 rows are returned.  While this is certainly workable, an alternative
>> that would be easier on the programmer would be to set the result to null if
>> no rows are found, and only throw an exception if > 1 rows are found.
>>
>> The documentation suggests doing a count to determine what situation you
>> are in, then issuing the appropriate select once you know.  But that will
>> result in an extra trip to the DB.  In a common scenario where you are
>> querying a primary key, you know that you will get zero or one row back.  So
>> rather than having to put the single row select into a try-catch block, a
>> more natural paradigm would be to assign the result and then check the
>> result for null.
>>
>>
>
> --
> Guy Rouillier
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
> For additional commands, e-mail: user-java-help@ibatis.apache.org
>
>

Re: Select one set null instead of throwing exception

Posted by Guy Rouillier <gu...@burntmail.com>.
Ignore this.  I realized after taking a break that it doesn't work with 
primitive types.  I think the thing I find most frustrating with Java is 
that it doesn't treat primitive types in a consistent manner with 
classes derived from Object.

Guy Rouillier wrote:
> Currently, performing a single row select throws an exception if zero or 
>  > 1 rows are returned.  While this is certainly workable, an 
> alternative that would be easier on the programmer would be to set the 
> result to null if no rows are found, and only throw an exception if > 1 
> rows are found.
> 
> The documentation suggests doing a count to determine what situation you 
> are in, then issuing the appropriate select once you know.  But that 
> will result in an extra trip to the DB.  In a common scenario where you 
> are querying a primary key, you know that you will get zero or one row 
> back.  So rather than having to put the single row select into a 
> try-catch block, a more natural paradigm would be to assign the result 
> and then check the result for null.
> 


-- 
Guy Rouillier

---------------------------------------------------------------------
To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
For additional commands, e-mail: user-java-help@ibatis.apache.org