Return-Path: Delivered-To: apmail-ibatis-user-java-archive@www.apache.org Received: (qmail 77724 invoked from network); 27 Aug 2009 06:13:21 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 27 Aug 2009 06:13:21 -0000 Received: (qmail 39708 invoked by uid 500); 27 Aug 2009 06:13:20 -0000 Delivered-To: apmail-ibatis-user-java-archive@ibatis.apache.org Received: (qmail 39684 invoked by uid 500); 27 Aug 2009 06:13:20 -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 39673 invoked by uid 99); 27 Aug 2009 06:13:20 -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 06:13:20 +0000 X-ASF-Spam-Status: No, hits=2.2 required=10.0 tests=HTML_MESSAGE,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of clinton.begin@gmail.com designates 209.85.212.172 as permitted sender) Received: from [209.85.212.172] (HELO mail-vw0-f172.google.com) (209.85.212.172) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 27 Aug 2009 06:13:10 +0000 Received: by vws2 with SMTP id 2so591726vws.4 for ; Wed, 26 Aug 2009 23:12:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:content-type; bh=9dJjzaNB1QBqBSvIYygPTUL9o93hN9Hl5heeYjVXWYM=; b=vcnKEK0AnJt7trueamZpwluKvdEzl5Yi/THykn8V5ElB2M62lS+fXnHHEawA8HYJlh 0SY70m2f1oEikTz7NtsS6gXr/NtJ0sIwMb0Im9tH7iEG8VaPTjYA9aIFLwZMbdUzcZpY ++D/nhEEiBlx7mGPWICaBticaO9pQCedrSMzM= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=MEA2WZBJeNlKdODIEi8Icd3ObnKousPsC3pjZ6TwG/9yWUqfqSJTi3aJGiZqwPpCje rVxgATHrdmlf9aErvgrXhg8z5ywXwHROr+0tF0zyqpgcjv4/xrRWDOBXVD7rKwMZbReh PDfhbizqEWJhgbr5XO/xssoYjKLT62kEbEH/0= MIME-Version: 1.0 Received: by 10.220.42.141 with SMTP id s13mr12124454vce.95.1251353569460; Wed, 26 Aug 2009 23:12:49 -0700 (PDT) In-Reply-To: <583d4dff0908261007w1b9fc364ye2fc892634ded90f@mail.gmail.com> References: <7.1.0.9.0.20090826164434.019cbe78@arasoft.de> <0MKuxg-1MgKK40Yaa-000RKs@mrelayeu.kundenserver.de> <583d4dff0908261007w1b9fc364ye2fc892634ded90f@mail.gmail.com> Date: Thu, 27 Aug 2009 00:12:49 -0600 Message-ID: <16178eb10908262312i17f36c2cl8b271e4b33d342b7@mail.gmail.com> Subject: Re: iBATIS 3.0 selectOne SessionException if no data found From: Clinton Begin To: user-java@ibatis.apache.org Content-Type: multipart/alternative; boundary=0016364993c3ff01800472197408 X-Virus-Checked: Checked by ClamAV on apache.org --0016364993c3ff01800472197408 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Rick's suggestion is correct. That was the idea. Honestly, we don't have to throw the exception, but it is a good in practic= e IMHO... But you all tell me, do you really want to return null? Clinton On Wed, Aug 26, 2009 at 11:07 AM, Rick 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 wr= ote: > >> >> 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 i= ssue? >>> 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@ibatis.apache.org >> For additional commands, e-mail: user-java-help@ibatis.apache.org >> >> > > > -- > Rick R > --0016364993c3ff01800472197408 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Rick's suggestion is correct.=A0 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?=A0
Clinton

On Wed, Aug 26, 2009 at 11:07 AM,= Rick <rickcr@gmai= l.com> wrote:
Maybe a bit of a hack but ...

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

Keep the dao method the same...

public long getUserId(Strin= g email) {

but just do a quick check in there on the size of the Li= st 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:

=A0`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
=A0`email` varchar(120) NOT NULL,

In UserMapper.java I have the following method

public interface UserMapper {
=A0public long getUserId(String email);
}

In UserMapper.xml I have this:

<select id=3D"getUserId" parameterType=3D"string" re= sultType=3D"long">
SELECT id FROM users WHERE email =3D #{email}
</select>

My DAO client code looks like this

public long getUserId(String email) throws SQLException {
=A0 =A0 =A0 =A0SqlSession session =3D sqlSessionFactory.openSession(true);=
=A0 =A0 =A0 =A0try {
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0UserMapper mapper =3D session.getMapper(Use= rMapper.class);
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0long userId =3D mapper.getUserId(email.toLo= werCase());
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return userId;
=A0 =A0 =A0 =A0} catch (SessionException sex) {
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0throw new SQLException("No data found.= ", "02000");
=A0 =A0 =A0 =A0} finally {
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0session.close();
=A0 =A0 =A0 =A0}
}

What I am unhappy about is that in order to differentiate between the "= ;no data found" situation and other cases, I =A0would have to check th= e 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 trus= t that string to never change...

Is there another way to deal with the situation (without an additional SELE= CT 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) ca= ses, 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

--0016364993c3ff01800472197408--