Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Why does myBatis not set value to POJO when query return null of a field?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  15 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Chan Wai Lun  
View profile  
 More options Jun 18 2012, 12:25 am
From: Chan Wai Lun <fatlun1...@gmail.com>
Date: Sun, 17 Jun 2012 21:25:55 -0700 (PDT)
Local: Mon, Jun 18 2012 12:25 am
Subject: Why does myBatis not set value to POJO when query return null of a field?

Hi all,

Recently I have been working on a project to change web-base system from
old-fashion JDBC to myBatis. MyBatis is great but because I have to keep
all the sql the same as before to minimize the change I have to make.

Then I have something like this:
pojo.setFieldA(rs.getInt("FieldA")==1?"Y":"N");

As in the table 'FieldA' allows null, so when the result is null then I
will have 'N' in fieldA in the pojo.

So, I have created a custom type handler and do something like this:
public String getNullableResult(ResultSet paramResultSet, String
paramString)
throws SQLException {
int ret = paramResultSet.getInt(paramString);
return ret==1?"Y":"N";

}

But finally it does not work because in BaseTypeHandler, I found something
like this:

public T getResult(ResultSet rs, String columnName) throws SQLException {
     Object result = getNullableResult(rs, columnName);
     if (rs.wasNull()) {
       return null;
     }
     return result;
   }

So, as the result returned by the query is null, then it return null
directly instead of the value returned by the custom type handler.

I have done something other to achieve my goal... but I am quite curious
about why it has to check rs.wasNull() in BaseTypeHandler... or I have seen
in a wrong place???

Any comments are welcome... thx.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eduardo Macarron  
View profile  
 More options Jun 18 2012, 10:34 am
From: Eduardo Macarron <eduardo.macar...@gmail.com>
Date: Mon, 18 Jun 2012 16:34:18 +0200
Local: Mon, Jun 18 2012 10:34 am
Subject: Re: Why does myBatis not set value to POJO when query return null of a field?
MyBatis does not call the POJO's setter when the value is null.

The main problem is, how should we deal with primitives?

If a field is an int it will not accept a null but we cannot assume
that null means 0.

We could bypass setting null if the property is a primitive but...
that hurts consistence and brings complexity.

So... unless I find a consistent way to handle nulls I would say it is
better to bypass them as MB does now.

thoughts?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dridi Boukelmoune  
View profile  
 More options Jun 18 2012, 10:39 am
From: Dridi Boukelmoune <dridi.boukelmo...@zenika.com>
Date: Mon, 18 Jun 2012 16:39:03 +0200
Local: Mon, Jun 18 2012 10:39 am
Subject: Re: Why does myBatis not set value to POJO when query return null of a field?

Hi,

How about a method containing the wasNull part, web could then override it
and tune the behavior.

Dridi
http://www.zenika.com/
Le 18 juin 2012 16:34, "Eduardo Macarron" <eduardo.macar...@gmail.com> a
écrit :


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eduardo Macarron  
View profile   Translate to Translated (View Original)
 More options Jun 18 2012, 10:39 am
From: Eduardo Macarron <eduardo.macar...@gmail.com>
Date: Mon, 18 Jun 2012 16:39:25 +0200
Local: Mon, Jun 18 2012 10:39 am
Subject: Re: Why does myBatis not set value to POJO when query return null of a field?
Sorry, I meant unless "we" find, not me :) I do not know how to solve it.

BTW, hibernate fails when mapping a null to a primitive. That is
indeed a consistent solution but I am fairly sure that bypassing nulls
will create less issues.

One possible solution is letting the user choose if he wants nulls to
be set or not with a new global configuration property like
"callSettersOnNulls". In that case it will call always to the setter
wether it is a primitive or not.

2012/6/18 Eduardo Macarron <eduardo.macar...@gmail.com>:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eduardo Macarron  
View profile  
 More options Jun 18 2012, 10:40 am
From: Eduardo Macarron <eduardo.macar...@gmail.com>
Date: Mon, 18 Jun 2012 16:40:55 +0200
Local: Mon, Jun 18 2012 10:40 am
Subject: Re: Why does myBatis not set value to POJO when query return null of a field?
You mean in the TypeHandler?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dridi Boukelmoune  
View profile  
 More options Jun 18 2012, 10:42 am
From: Dridi Boukelmoune <dridi.boukelmo...@zenika.com>
Date: Mon, 18 Jun 2012 16:42:09 +0200
Local: Mon, Jun 18 2012 10:42 am
Subject: Re: Why does myBatis not set value to POJO when query return null of a field?

Yes in thé abstract built-in typehandler .
Le 18 juin 2012 16:40, "Eduardo Macarron" <eduardo.macar...@gmail.com> a
écrit :


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eduardo Macarron  
View profile  
 More options Jun 18 2012, 10:46 am
From: Eduardo Macarron <eduardo.macar...@gmail.com>
Date: Mon, 18 Jun 2012 16:46:55 +0200
Local: Mon, Jun 18 2012 10:46 am
Subject: Re: Why does myBatis not set value to POJO when query return null of a field?
Not sure it is needed. You can already replace null by another value
using current getResult method from TypeHandler interface or overiding
it from BaseTypeHandler.

In fact, that is what Chan Wai Lun should do to fix the issue with current code.

2012/6/18 Dridi Boukelmoune <dridi.boukelmo...@zenika.com>:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ravi Thapa  
View profile  
 More options Jun 18 2012, 11:08 am
From: Ravi Thapa <ravithap...@gmail.com>
Date: Mon, 18 Jun 2012 20:38:02 +0530
Local: Mon, Jun 18 2012 11:08 am
Subject: Re: Why does myBatis not set value to POJO when query return null of a field?

Even i faced the same issue, when a null value is returned then i do get
the exception. So my question is how to handle null value the in Ibatis.

Please can you show any example it would be grateful for me, as i am still
learning Ibatis.

On Mon, Jun 18, 2012 at 8:16 PM, Eduardo Macarron <

--
Thanks and Regards

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eduardo Macarron  
View profile  
 More options Jun 18 2012, 11:22 am
From: Eduardo Macarron <eduardo.macar...@gmail.com>
Date: Mon, 18 Jun 2012 17:22:23 +0200
Local: Mon, Jun 18 2012 11:22 am
Subject: Re: Why does myBatis not set value to POJO when query return null of a field?
Hi Ravi, could you post the stacktrace?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chan Wai Lun  
View profile  
 More options Jun 18 2012, 11:05 pm
From: Chan Wai Lun <fatlun1...@gmail.com>
Date: Mon, 18 Jun 2012 20:05:14 -0700 (PDT)
Local: Mon, Jun 18 2012 11:05 pm
Subject: Re: Why does myBatis not set value to POJO when query return null of a field?

Hi Edurado,

Thank you very much, now I can understand the reason of doing so.

Yes I agree that is what I should do it... but why I asked this question
was because in my mind, when I read the documentation and examples,
extending baseTypeHandler and implement the getNullableResult should let me
have the control on how the result should be return to myBatis... just
like, if in some case I have to write some code to turn the result from
query to something other before it send to myBatis for further process... I
did not expect myBatis will check the original value that the query
returned instead of checking the value returned by the custom type
handler... maybe i missed it somewhere in the documentation... haha.

Thanks and Regards,
Wai Lun

Eduardo於 2012年6月18日星期一UTC+8下午10時46分55秒寫道:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ravi Thapa  
View profile  
 More options Jun 21 2012, 1:04 pm
From: Ravi Thapa <ravithap...@gmail.com>
Date: Thu, 21 Jun 2012 22:34:11 +0530
Local: Thurs, Jun 21 2012 1:04 pm
Subject: Re: Why does myBatis not set value to POJO when query return null of a field?

Can anyone tell how to use ibatis framework in java with relation to
creating a connection pool in jboss server. I am not getting any suitable
example. If anyone can tell me how to configure and how i can use it java.
Waiting for your repsonse..

On Tue, Jun 19, 2012 at 8:35 AM, Chan Wai Lun <fatlun1...@gmail.com> wrote:

--
Thanks and Regards

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eduardo Macarron  
View profile  
 More options Jun 21 2012, 1:05 pm
From: Eduardo Macarron <eduardo.macar...@gmail.com>
Date: Thu, 21 Jun 2012 19:05:39 +0200
Local: Thurs, Jun 21 2012 1:05 pm
Subject: Re: Why does myBatis not set value to POJO when query return null of a field?
Ravi, the setting up of the pool in JBoss is the same than for JDBC code.

In MyBatis set the transaction factory to JNDI

http://www.mybatis.org/core/configuration.html

2012/6/21 Ravi Thapa <ravithap...@gmail.com>:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stephen Friedrich  
View profile  
 More options Aug 16 2012, 11:19 am
From: Stephen Friedrich <stephen.friedr...@fortis-it.de>
Date: Thu, 16 Aug 2012 08:19:17 -0700 (PDT)
Local: Thurs, Aug 16 2012 11:19 am
Subject: Re: Why does myBatis not set value to POJO when query return null of a field?

I think this behavior is a Bad Thing: It violates the principle of least
surprise.

When I read an object the natural assumption is that it holds exactly the
values returned from the mapped statement.
When the object has primitive fields, but null is returned from the db,
then a natural way to handle it would be to throw an exception. It's
clearly a programmer error to use a primitive field for a nullable column.

This probably cannot be changed in a minor version, but a configuration
option would be very welcome!
IMHO for the next major version this natural behavior should be the default.

For motivation here's a real scenario that just happened to me.
I had a lot of "fun" debugging this very strange and surprising error:
We got a complaint that a user was not able to log-in. He had just changed
his password, but was pretty sure that the entered password was correct.
When debugging this I noticed that my "user" DTO read with mybatis _always_
contained a salt value for the password hash, even if it was null in the
db. (Happens for users that were created before we added password salting).
I asked a colleague to come over and help me debug this under the
assumption that there is a dumb error in the code that I just don't notice,
because I am too familiar with the code.
But we both were stumped when we looked at the object returned from mybatis
versus the same query executed directly on the database.
We checked if we were looking at the same db for both versions, that all
transactions were commited and just looked clueless at the code.
The solution to the riddle was that somebody noticed code duplication and
refactored it: Everywhere a "user" dto was created a random salt was set
afterwards. So a better, more DRY way seemed to move the salt
initialization to the field of the user dto. :-(


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eduardo Macarron  
View profile  
 More options Aug 16 2012, 12:27 pm
From: Eduardo Macarron <eduardo.macar...@gmail.com>
Date: Thu, 16 Aug 2012 18:27:57 +0200
Local: Thurs, Aug 16 2012 12:27 pm
Subject: Re: Why does myBatis not set value to POJO when query return null of a field?

Hi Stephen. Your post makes a lot sense. I will try to add that config
param for 3.2. Thanks for sharing your point of view.

2012/8/16 Stephen Friedrich <stephen.friedr...@fortis-it.de>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Guy Rouillier  
View profile   Translate to Translated (View Original)
 More options Aug 16 2012, 6:26 pm
From: Guy Rouillier <guy.rouill...@gmail.com>
Date: Thu, 16 Aug 2012 18:26:29 -0400
Local: Thurs, Aug 16 2012 6:26 pm
Subject: Re: Why does myBatis not set value to POJO when query return null of a field?
On 8/16/2012 11:19 AM, Stephen Friedrich wrote:

> When I read an object the natural assumption is that it holds exactly
> the values returned from the mapped statement.
> When the object has primitive fields, but null is returned from the db,
> then a natural way to handle it would be to throw an exception. It's
> clearly a programmer error to use a primitive field for a nullable column.

I don't agree with the last statement.  One of the strong points of
MyBatis (the reason we adopted it) was it functions well in the presence
of "impedance" mismatch.  We have many legacy databases that are not
well normalized and missing a lot of referential integrity checks.
MyBatis continues to provide a useful mapping in this environment, while
a more strict solution like Hibernate would simply not work.

Having said all that, you obviously can't stuff a null into an int.  So,
the problem is not using a primitive field for a nullable column, but
failing to account for it in the SQL.

--
Guy Rouillier


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »