You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Johannes Klose <ex...@calitrix.de> on 2006/11/07 19:39:10 UTC

Lazily loading user defined types

Hi,

i'm trying to do lazy loading on user defined properties, e.q. beans on
1:1 relationships. However, it isn't working as i'm not using cglib. I
found a message in another thread in this list, stating that lazy
loading would work on user defined properties if they are typed as
interfaces. But this didn't worked, too. I tried several things,
starting from defining the bean properties as interface types and ending
with an own ResultObjectFactory which instanciates the propper concrete
class if a resultClass attribute in a sql map is set to an interface
type. None of it worked.

I don't really know (yet) what this proxy stuff on interfaces means as
i'm seeing it the first time now, but from the code in
com.ibatis.sqlmap.engine.mapping.result.loader i figured out that this
proxy stuff is only done on collection types, not user defined ones.

Is there a way to get lazy loading working

a) without writing own lazy loaders in the data model beans
b) withoug using cglib?

Is it possible to extend the LazyResultLoader to create the proxies not
only on collection types, or can this be only done on these?
Or am i just doing something wrong? ;)

Thanks,

Johannes Klose


Re: Lazily loading user defined types

Posted by Johannes Klose <ex...@calitrix.de>.
I've done some hacking on the LazyResultLoader class and came to a 
working solution. I created an interface named "Deferrable" which is 
basically just a marker interface. Properties which can be lazily loaded 
on user defined types are typed as interfaces which extend Deferrable. 
If a result loader for such a type is requested, the LazyResultLoader 
will create a proxy for this type and lazy loading works.

Here is the modified code, maybe it is useful for someone else too.

Deferrable:

package com.ibatis.sqlmap.engine.mapping.result.loader;

public interface Deferrable {

}

loadResult() in LazyResultLoader:

public Object loadResult() throws SQLException {
    if (Collection.class.isAssignableFrom(targetType)) {
      InvocationHandler handler = new LazyResultLoader(client, 
statementName, parameterObject, targetType);
      ClassLoader cl = targetType.getClassLoader();
      return Proxy.newProxyInstance(cl, LIST_INTERFACES, handler);
    } else if(targetType.isInterface() && 
Deferrable.class.isAssignableFrom(targetType)) {
        InvocationHandler handler = new LazyResultLoader(client, 
statementName, parameterObject, targetType);
        ClassLoader cl = targetType.getClassLoader();
        return Proxy.newProxyInstance(cl, new Class[] { targetType }, 
handler);
      } else {
          return ResultLoader.getResult(client, statementName, 
parameterObject, targetType);
      }
  }