I'm trying to generate classes with cglib that allow lazy loading. I am doing this because I need to generate classes at runtime that deal with our content tables, and our content items can have very large image and text data associated with them.
As such, it would be a big performance win to not load the associated text or image data when loading the item's metadata.
I took the implementation from the InstrumentTask and I've tried to attach that to cglib when generating my runtime property classes. The problem is that lazy loading doesn't work properly. The lazy properties are not loaded (good) but don't load on access (bad).
Hibernate does seem to believe I've done the right thing. Unfortunately cglib is not well documented - the javadoc is largely devoid of class or method descriptions - which makes this rather hard to debug.
Here's how I attach the transform. I create the filter:
Code:
public static class HibInterceptFieldFilter implements InterceptFieldFilter
{
public boolean acceptRead(Type owner, String name)
{
return true;
}
public boolean acceptWrite(Type owner, String name)
{
return true;
}
public boolean acceptRead(net.sf.cglib.asm.Type arg0, String arg1)
{
return true;
}
public boolean acceptWrite(net.sf.cglib.asm.Type arg0, String arg1)
{
return true;
}
};
static InterceptFieldTransformer ms_xform =
new InterceptFieldTransformer(new HibInterceptFieldFilter());
Then I use the filter when creating the class:
Code:
gen.setStrategy(new DefaultGeneratorStrategy() {
protected ClassGenerator transform(ClassGenerator cg) {
return new TransformingClassGenerator(cg, ms_xform);
}
});
Class beanClass = (Class) gen.createClass();
A last, possibly important, detail. I can't call the accessors on the generated class directly, so I'm going through reflection. Does this have any affect on Hibernate?