Return-Path: Delivered-To: apmail-ibatis-user-java-archive@www.apache.org Received: (qmail 47278 invoked from network); 27 Aug 2009 14:06:07 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 27 Aug 2009 14:06:07 -0000 Received: (qmail 69819 invoked by uid 500); 27 Aug 2009 14:06:06 -0000 Delivered-To: apmail-ibatis-user-java-archive@ibatis.apache.org Received: (qmail 69805 invoked by uid 500); 27 Aug 2009 14:06:06 -0000 Mailing-List: contact user-java-help@ibatis.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user-java@ibatis.apache.org Delivered-To: mailing list user-java@ibatis.apache.org Received: (qmail 69797 invoked by uid 99); 27 Aug 2009 14:06:06 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 27 Aug 2009 14:06:06 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of Christian.Poitras@ircm.qc.ca designates 207.162.51.9 as permitted sender) Received: from [207.162.51.9] (HELO MGATE.ircm.qc.ca) (207.162.51.9) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 27 Aug 2009 14:05:56 +0000 Received: from HERMES.ircm.priv ([172.16.9.26]) by hubcli1.ircm.priv ([172.16.9.31]) with mapi; Thu, 27 Aug 2009 10:05:35 -0400 From: Poitras Christian To: "'user-java@ibatis.apache.org'" Date: Thu, 27 Aug 2009 10:05:34 -0400 Subject: RE: iBATIS 3.0 selectOne SessionException if no data found Thread-Topic: iBATIS 3.0 selectOne SessionException if no data found Thread-Index: AconHGVN4xC4VwlLRImdMQ96EhBO9gAAr96w Message-ID: <93B0CB1886D4CC48823246865D80DC94039FA09E4764@HERMES.ircm.priv> References: <7.1.0.9.0.20090826164434.019cbe78@arasoft.de> <0MKuxg-1MgKK40Yaa-000RKs@mrelayeu.kundenserver.de> <583d4dff0908261007w1b9fc364ye2fc892634ded90f@mail.gmail.com> <16178eb10908262312i17f36c2cl8b271e4b33d342b7@mail.gmail.com> <0MKuxg-1MgfFu1aNz-000Qw2@mrelayeu.kundenserver.de> In-Reply-To: <0MKuxg-1MgfFu1aNz-000Qw2@mrelayeu.kundenserver.de> Accept-Language: fr-FR, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: acceptlanguage: fr-FR, en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Virus-Checked: Checked by ClamAV on apache.org How about subclassing SessionException with something like NoResultFoundExc= eption? That way, it would be easy to catch the exception (the user mispelled his e= mail, 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 differentiat= e between an expected situation (no data for the given key) and other ("rea= l") 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 ><rickcr@gmail.com> wrote: >Maybe a bit of a hack but ... > >public interface UserMapper { > public List 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 <tgs@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: > > > >My DAO client code looks like this > >public long getUserId(String email) throws SQLException { > SqlSession session =3D sqlSessionFactory.openSession(true); > try { > UserMapper mapper =3D session.getMapper(UserMapper.class); > long userId =3D 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=B4d 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@i >batis.apache.org >For additional commands, e-mail: >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