ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Niels Beekman" <n.beek...@wis.nl>
Subject RE: Can't accurately evaluate null when using proxies?
Date Wed, 21 Sep 2005 18:56:53 GMT
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