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 "Thomas G. Schuessler" <tg...@arasoft.de> on 2009/08/26 17:21:57 UTC

iBATIS 3.0 selectOne SessionException if no data found

>In my MySQL db, I have a table 'users' with (amongst others) these fields:
>
>   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
>   `email` varchar(120) NOT NULL,
>
>In UserMapper.java I have the following method
>
>public interface UserMapper {
>   public long getUserId(String email);
>}
>
>In UserMapper.xml I have this:
>
><select id="getUserId" parameterType="string" resultType="long">
>SELECT id FROM users WHERE email = #{email}
></select>
>
>My DAO client code looks like this
>
>public long getUserId(String email) throws SQLException {
>         SqlSession session = sqlSessionFactory.openSession(true);
>         try {
>                 UserMapper mapper = session.getMapper(UserMapper.class);
>                 long userId = mapper.getUserId(email.toLowerCase());
>                 return userId;
>         } catch (SessionException sex) {
>                 throw new SQLException("No data found.", "02000");
>         } finally {
>                 session.close();
>         }
>}
>
>What I am unhappy about is that in order to 
>differentiate between the "no data found" 
>situation and other cases, I  would have to 
>check the SessionException for the string 
>"Expected one result to be returned by selectOne(), but found: 0".
>This is somehow unsatisfactory (at least to me) 
>since I do not like to trust that string to never change...
>
>Is there another way to deal with the situation 
>(without an additional SELECT COUNT...)? Maybe I 
>have not found the optimal way to solve the issue?
>Otherwise, I´d like either a specific exception 
>for this (and similar) cases, or a field with an 
>error code in SessionException.
>
>Thank you for any input on this,
>Thomas


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


RE: iBATIS 3.0 selectOne SessionException if no data found

Posted by Douglas Bell <DB...@boingo.com>.
Ditto.

 

From: Zoran Avtarovski [mailto:zoran@sparecreative.com] 
Sent: Thursday, August 27, 2009 4:25 PM
To: iBatis Java Mail List; Clinton Begin
Subject: Re: iBATIS 3.0 selectOne SessionException if no data found

 

I for one would prefer null. I like the idea of testing for null rather than using try catch

Z.


Rick's suggestion is correct.  That was the idea.

Honestly, we don't have to throw the exception, but it is a good in practice IMHO...

But you all tell me, do you really want to return null?  

Clinton

On Wed, Aug 26, 2009 at 11:07 AM, Rick <ri...@gmail.com> wrote:

Maybe a bit of a hack but ...

public interface UserMapper {
  public List<Long> getUserIds(String email);
}

Keep the dao method the same...

public long getUserId(String email) { 

but just do a quick check in there on the size of the List returned, and do what you want with it (throw you own exception if over size 1?)

and then just always return list.get(0) 


On Wed, Aug 26, 2009 at 11:21 AM, Thomas G. Schuessler <tg...@arasoft.de> wrote:

	 

	In my MySQL db, I have a table 'users' with (amongst others) these fields:
	
	  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
	  `email` varchar(120) NOT NULL,
	
	In UserMapper.java I have the following method
	
	public interface UserMapper {
	  public long getUserId(String email);
	}
	
	In UserMapper.xml I have this:
	
	<select id="getUserId" parameterType="string" resultType="long">
	SELECT id FROM users WHERE email = #{email}
	</select>
	
	My DAO client code looks like this
	
	public long getUserId(String email) throws SQLException {
	        SqlSession session = sqlSessionFactory.openSession(true);
	        try {
	                UserMapper mapper = session.getMapper(UserMapper.class);
	                long userId = mapper.getUserId(email.toLowerCase());
	                return userId;
	        } catch (SessionException sex) {
	                throw new SQLException("No data found.", "02000");
	        } finally {
	                session.close();
	        }
	}
	
	What I am unhappy about is that in order to differentiate between the "no data found" situation and other cases, I  would have to check the SessionException for the string "Expected one result to be returned by selectOne(), but found: 0".
	This is somehow unsatisfactory (at least to me) since I do not like to trust that string to never change...
	
	Is there another way to deal with the situation (without an additional SELECT COUNT...)? Maybe I have not found the optimal way to solve the issue?
	Otherwise, I´d like either a specific exception for this (and similar) cases, or a field with an error code in SessionException.
	
	Thank you for any input on this,
	Thomas



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

 


Re: iBATIS 3.0 selectOne SessionException if no data found

Posted by Zoran Avtarovski <zo...@sparecreative.com>.
I for one would prefer null. I like the idea of testing for null rather than
using try catch

Z.
> 
> Rick's suggestion is correct.  That was the idea.
> 
> Honestly, we don't have to throw the exception, but it is a good in practice
> IMHO...
> 
> But you all tell me, do you really want to return null? 
> 
> Clinton
> 
> On Wed, Aug 26, 2009 at 11:07 AM, Rick <ri...@gmail.com> wrote:
>> Maybe a bit of a hack but ...
>> 
>> public interface UserMapper {
>>   public List<Long> getUserIds(String email);
>> }
>> 
>> Keep the dao method the same...
>> 
>> public long getUserId(String email) {
>> 
>> but just do a quick check in there on the size of the List returned, and do
>> what you want with it (throw you own exception if over size 1?)
>> 
>> and then just always return list.get(0)
>> 
>> 
>> On Wed, Aug 26, 2009 at 11:21 AM, Thomas G. Schuessler <tg...@arasoft.de>
>> wrote:
>>> 
>>>> In my MySQL db, I have a table 'users' with (amongst others) these fields:
>>>> 
>>>>   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
>>>>   `email` varchar(120) NOT NULL,
>>>> 
>>>> In UserMapper.java I have the following method
>>>> 
>>>> public interface UserMapper {
>>>>   public long getUserId(String email);
>>>> }
>>>> 
>>>> In UserMapper.xml I have this:
>>>> 
>>>> <select id="getUserId" parameterType="string" resultType="long">
>>>> SELECT id FROM users WHERE email = #{email}
>>>> </select>
>>>> 
>>>> My DAO client code looks like this
>>>> 
>>>> public long getUserId(String email) throws SQLException {
>>>>         SqlSession session = sqlSessionFactory.openSession(true);
>>>>         try {
>>>>                 UserMapper mapper = session.getMapper(UserMapper.class);
>>>>                 long userId = mapper.getUserId(email.toLowerCase());
>>>>                 return userId;
>>>>         } catch (SessionException sex) {
>>>>                 throw new SQLException("No data found.", "02000");
>>>>         } finally {
>>>>                 session.close();
>>>>         }
>>>> }
>>>> 
>>>> What I am unhappy about is that in order to differentiate between the "no
>>>> data found" situation and other cases, I  would have to check the
>>>> SessionException for the string "Expected one result to be returned by
>>>> selectOne(), but found: 0".
>>>> This is somehow unsatisfactory (at least to me) since I do not like to
>>>> trust that string to never change...
>>>> 
>>>> Is there another way to deal with the situation (without an additional
>>>> SELECT COUNT...)? Maybe I have not found the optimal way to solve the
>>>> issue?
>>>> Otherwise, I´d like either a specific exception for this (and similar)
>>>> cases, or a field with an error code in SessionException.
>>>> 
>>>> Thank you for any input on this,
>>>> Thomas
>>> 
>>> 
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
>>> For additional commands, e-mail: user-java-help@ibatis.apache.org
>>> 
>> 
>> 


RE: iBATIS 3.0 selectOne SessionException if no data found

Posted by Poitras Christian <Ch...@ircm.qc.ca>.
How about subclassing SessionException with something like NoResultFoundException?
That way, it would be easy to catch the exception (the user mispelled his email, probably) and show a simple error to the user.

Christian

-----Original Message-----
From: Thomas G. Schuessler [mailto:tgs@arasoft.de]
Sent: Thursday, August 27, 2009 9:43 AM
To: user-java@ibatis.apache.org
Subject: Re: iBATIS 3.0 selectOne SessionException if no data found

1. @Rick: Thank you for your solution, I will use it.
2. @Clinton: No, I don't want NULL, an exception is the right approach. But the exception needs to have reliable information about the specific cause, just like the SqlState field in SqlException, so that one can differentiate between an expected situation (no data for the given key) and other ("real") errors (database disappeared...).

BTW, I love the 3.0 version so far...



At 08:12 2009-08-27, Clinton Begin wrote:
>Rick's suggestion is correct.  That was the idea.
>
>Honestly, we don't have to throw the exception, but it is a good in
>practice IMHO...
>
>But you all tell me, do you really want to return null?
>
>Clinton
>
>On Wed, Aug 26, 2009 at 11:07 AM, Rick
><<m...@gmail.com> wrote:
>Maybe a bit of a hack but ...
>
>public interface UserMapper {
>  public List<Long> getUserIds(String email); }
>
>Keep the dao method the same...
>
>public long getUserId(String email) {
>
>but just do a quick check in there on the size of the List returned,
>and do what you want with it (throw you own exception if over size 1?)
>
>and then just always return list.get(0)
>
>
>On Wed, Aug 26, 2009 at 11:21 AM, Thomas G.
>Schuessler <<m...@arasoft.de> wrote:
>
>In my MySQL db, I have a table 'users' with (amongst others) these fields:
>
>  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,  `email` varchar(120)
> NOT NULL,
>
>In UserMapper.java I have the following method
>
>public interface UserMapper {
>  public long getUserId(String email);
>}
>
>In UserMapper.xml I have this:
>
><select id="getUserId" parameterType="string" resultType="long"> SELECT
>id FROM users WHERE email = #{email} </select>
>
>My DAO client code looks like this
>
>public long getUserId(String email) throws SQLException {
>        SqlSession session = sqlSessionFactory.openSession(true);
>        try {
>                UserMapper mapper = session.getMapper(UserMapper.class);
>                long userId = mapper.getUserId(email.toLowerCase());
>                return userId;
>        } catch (SessionException sex) {
>                throw new SQLException("No data found.", "02000");
>        } finally {
>                session.close();
>        }
>}
>
>What I am unhappy about is that in order to differentiate between the
>"no data found"
>situation and other cases, I  would have to check the SessionException
>for the string "Expected one result to be returned by selectOne(), but
>found: 0".
>This is somehow unsatisfactory (at least to me) since I do not like to
>trust that string to never change...
>
>Is there another way to deal with the situation (without an additional
>SELECT COUNT...)? Maybe I have not found the optimal way to solve the
>issue?
>Otherwise, I´d like either a specific exception for this (and similar)
>cases, or a field with an error code in SessionException.
>
>Thank you for any input on this,
>Thomas
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail:
><ma...@ibatis.apache.org>user-java-unsubscribe@i
>batis.apache.org
>For additional commands, e-mail:
><ma...@ibatis.apache.org>user-java-help@ibatis.apache.o
>rg
>
>
>
>
>--
>Rick R
>


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


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


Re: iBATIS 3.0 selectOne SessionException if no data found

Posted by "Thomas G. Schuessler" <tg...@arasoft.de>.
1. @Rick: Thank you for your solution, I will use it.
2. @Clinton: No, I don't want NULL, an exception 
is the right approach. But the exception needs to 
have reliable information about the specific 
cause, just like the SqlState field in 
SqlException, so that one can differentiate 
between an expected situation (no data for the 
given key) and other ("real") errors (database disappeared...).

BTW, I love the 3.0 version so far...



At 08:12 2009-08-27, Clinton Begin wrote:
>Rick's suggestion is correct.  That was the idea.
>
>Honestly, we don't have to throw the exception, 
>but it is a good in practice IMHO...
>
>But you all tell me, do you really want to return null?
>
>Clinton
>
>On Wed, Aug 26, 2009 at 11:07 AM, Rick 
><<m...@gmail.com> wrote:
>Maybe a bit of a hack but ...
>
>public interface UserMapper {
>  public List<Long> getUserIds(String email);
>}
>
>Keep the dao method the same...
>
>public long getUserId(String email) {
>
>but just do a quick check in there on the size 
>of the List returned, and do what you want with 
>it (throw you own exception if over size 1?)
>
>and then just always return list.get(0)
>
>
>On Wed, Aug 26, 2009 at 11:21 AM, Thomas G. 
>Schuessler <<m...@arasoft.de> wrote:
>
>In my MySQL db, I have a table 'users' with (amongst others) these fields:
>
>  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
>  `email` varchar(120) NOT NULL,
>
>In UserMapper.java I have the following method
>
>public interface UserMapper {
>  public long getUserId(String email);
>}
>
>In UserMapper.xml I have this:
>
><select id="getUserId" parameterType="string" resultType="long">
>SELECT id FROM users WHERE email = #{email}
></select>
>
>My DAO client code looks like this
>
>public long getUserId(String email) throws SQLException {
>        SqlSession session = sqlSessionFactory.openSession(true);
>        try {
>                UserMapper mapper = session.getMapper(UserMapper.class);
>                long userId = mapper.getUserId(email.toLowerCase());
>                return userId;
>        } catch (SessionException sex) {
>                throw new SQLException("No data found.", "02000");
>        } finally {
>                session.close();
>        }
>}
>
>What I am unhappy about is that in order to 
>differentiate between the "no data found" 
>situation and other cases, I  would have to 
>check the SessionException for the string 
>"Expected one result to be returned by selectOne(), but found: 0".
>This is somehow unsatisfactory (at least to me) 
>since I do not like to trust that string to never change...
>
>Is there another way to deal with the situation 
>(without an additional SELECT COUNT...)? Maybe I 
>have not found the optimal way to solve the issue?
>Otherwise, I´d like either a specific exception 
>for this (and similar) cases, or a field with an 
>error code in SessionException.
>
>Thank you for any input on this,
>Thomas
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: 
><ma...@ibatis.apache.org>user-java-unsubscribe@ibatis.apache.org
>For additional commands, e-mail: 
><ma...@ibatis.apache.org>user-java-help@ibatis.apache.org
>
>
>
>
>--
>Rick R
>


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


Re: iBATIS 3.0 selectOne SessionException if no data found

Posted by Clinton Begin <cl...@gmail.com>.
Rick's suggestion is correct.  That was the idea.

Honestly, we don't have to throw the exception, but it is a good in practice
IMHO...

But you all tell me, do you really want to return null?

Clinton

On Wed, Aug 26, 2009 at 11:07 AM, Rick <ri...@gmail.com> wrote:

> Maybe a bit of a hack but ...
>
> public interface UserMapper {
>  public List<Long> getUserIds(String email);
> }
>
> Keep the dao method the same...
>
> public long getUserId(String email) {
>
> but just do a quick check in there on the size of the List returned, and do
> what you want with it (throw you own exception if over size 1?)
>
> and then just always return list.get(0)
>
>
> On Wed, Aug 26, 2009 at 11:21 AM, Thomas G. Schuessler <tg...@arasoft.de>wrote:
>
>>
>>  In my MySQL db, I have a table 'users' with (amongst others) these
>>> fields:
>>>
>>>  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
>>>  `email` varchar(120) NOT NULL,
>>>
>>> In UserMapper.java I have the following method
>>>
>>> public interface UserMapper {
>>>  public long getUserId(String email);
>>> }
>>>
>>> In UserMapper.xml I have this:
>>>
>>> <select id="getUserId" parameterType="string" resultType="long">
>>> SELECT id FROM users WHERE email = #{email}
>>> </select>
>>>
>>> My DAO client code looks like this
>>>
>>> public long getUserId(String email) throws SQLException {
>>>        SqlSession session = sqlSessionFactory.openSession(true);
>>>        try {
>>>                UserMapper mapper = session.getMapper(UserMapper.class);
>>>                long userId = mapper.getUserId(email.toLowerCase());
>>>                return userId;
>>>        } catch (SessionException sex) {
>>>                throw new SQLException("No data found.", "02000");
>>>        } finally {
>>>                session.close();
>>>        }
>>> }
>>>
>>> What I am unhappy about is that in order to differentiate between the "no
>>> data found" situation and other cases, I  would have to check the
>>> SessionException for the string "Expected one result to be returned by
>>> selectOne(), but found: 0".
>>> This is somehow unsatisfactory (at least to me) since I do not like to
>>> trust that string to never change...
>>>
>>> Is there another way to deal with the situation (without an additional
>>> SELECT COUNT...)? Maybe I have not found the optimal way to solve the issue?
>>> Otherwise, I´d like either a specific exception for this (and similar)
>>> cases, or a field with an error code in SessionException.
>>>
>>> Thank you for any input on this,
>>> Thomas
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
>> For additional commands, e-mail: user-java-help@ibatis.apache.org
>>
>>
>
>
> --
> Rick R
>

Re: iBATIS 3.0 selectOne SessionException if no data found

Posted by Rick <ri...@gmail.com>.
Maybe a bit of a hack but ...

public interface UserMapper {
 public List<Long> getUserIds(String email);
}

Keep the dao method the same...

public long getUserId(String email) {

but just do a quick check in there on the size of the List returned, and do
what you want with it (throw you own exception if over size 1?)

and then just always return list.get(0)


On Wed, Aug 26, 2009 at 11:21 AM, Thomas G. Schuessler <tg...@arasoft.de>wrote:

>
>  In my MySQL db, I have a table 'users' with (amongst others) these fields:
>>
>>  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
>>  `email` varchar(120) NOT NULL,
>>
>> In UserMapper.java I have the following method
>>
>> public interface UserMapper {
>>  public long getUserId(String email);
>> }
>>
>> In UserMapper.xml I have this:
>>
>> <select id="getUserId" parameterType="string" resultType="long">
>> SELECT id FROM users WHERE email = #{email}
>> </select>
>>
>> My DAO client code looks like this
>>
>> public long getUserId(String email) throws SQLException {
>>        SqlSession session = sqlSessionFactory.openSession(true);
>>        try {
>>                UserMapper mapper = session.getMapper(UserMapper.class);
>>                long userId = mapper.getUserId(email.toLowerCase());
>>                return userId;
>>        } catch (SessionException sex) {
>>                throw new SQLException("No data found.", "02000");
>>        } finally {
>>                session.close();
>>        }
>> }
>>
>> What I am unhappy about is that in order to differentiate between the "no
>> data found" situation and other cases, I  would have to check the
>> SessionException for the string "Expected one result to be returned by
>> selectOne(), but found: 0".
>> This is somehow unsatisfactory (at least to me) since I do not like to
>> trust that string to never change...
>>
>> Is there another way to deal with the situation (without an additional
>> SELECT COUNT...)? Maybe I have not found the optimal way to solve the issue?
>> Otherwise, I´d like either a specific exception for this (and similar)
>> cases, or a field with an error code in SessionException.
>>
>> Thank you for any input on this,
>> Thomas
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
> For additional commands, e-mail: user-java-help@ibatis.apache.org
>
>


-- 
Rick R