ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dan Bradley <debrad...@gmail.com>
Subject Re: Can't accurately evaluate null when using proxies?
Date Wed, 21 Sep 2005 19:01:13 GMT
Since I posted, I did discover that setting the config to
enhancementEnabled="false" solves the problem. The consequences of not
using enhancement are unclear to me though. The documentation says
only that "This setting enables runtime bytecode enhancement to
facilitate optimized JavaBean property access as well as enhanced lazy
loading." And of course it would be nice if null checks worked as
expected even when using "enhancement".


On 9/21/05, Niels Beekman <n.beekman@wis.nl> wrote:
> Hi,
>
> I looked at the source code, and I believe you are right, iBATIS
> performs null-checking, but only when invoking methods; i.e.
> entity.getFoo().getX(), when the proxied (lazily loaded) result is null,
> null is returned as method-result. The proxy, however, is created
> immediately.
>
> The reason for this is pretty obvious when you think about it some more,
> if iBATIS wouuld check for a null-result on beforehand, lazy would not
> be lazy anymore :)
>
> Apart from this explanation I'm afraid I cannot offer you much, the only
> thing I can think of is implementing an extra check which depends on a
> default value:
>
> class Foo {
>  int id = Integer.MIN_VALUE;
>
>  int getId() {
>    return id;
>  }
> }
>
> Foo foo = entity.getFoo();
> System.out.println(foo == null || foo.getId() == Integer.MIN_VALUE);
>
> I think this is a really dirty trick and requires extensive knowledge of
> the underlying persistence-method, not so OO... JDO and Hibernate solve
> this by bytecode-enhancement.
>
> Anyone else?
>
> Niels
>
> -----Original Message-----
> From: Dan Bradley [mailto:debradley@gmail.com]
> Sent: woensdag 21 september 2005 20:15
> To: user-java@ibatis.apache.org
> Subject: Can't accurately evaluate null when using proxies?
>
> I'm running into odd conditions, apparently because of the way iBATIS
> uses CGLIB for proxying and I'm wondering if it's just something I'm
> doing wrong or if there's a workaround.
>
> While an entity may *appear* to return null from a getter (null is
> actually the proxied result), (entity.getFoo() == null) surprisingly
> evaluates to false, because getFoo() returns the proxy, which is
> non-null, not the proxied result.
>
> Some sample code may make this a little clearer:
>
> // Entity has a relation to Foo, which may be null - foo is lazily
> loaded
> Entity entity = dao.getById(1);
>
> // In the debugger the following evaluates to
> Foo$$EnhancerByCGLIB$$12345678
> // with a value of null:
> entity.getFoo();
>
> // And then...
> entity.getFoo() == null; // ...evaluates to false!
>
> // And so does this:
> Foo foo = entity.getFoo();
> foo == null; // false
>
> Is my approach wrong? Is there a way around it, other than not using
> lazy loading? Thanks for any help.
>

Mime
View raw message