I am not sure it is a good idea, but it avoids this error:
Code:
public static Factory getProxyFactory(final Class persistentClass, Class[] interfaces) throws HibernateException {
//note: interfaces is assumed to already contain HibernateProxy.class
try {
MethodInterceptor interceptor = new MethodInterceptor(){
public Object intercept(final Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
if(method.getName().equals("getHibernateLazyInitializer")){
return new LazyInitializer(){
public void initialize() throws HibernateException {
}
public Serializable getIdentifier() {
return null;
}
public void setIdentifier(Serializable id) {
}
public String getEntityName() {
return "<<" + persistentClass.getName() + " TEMPLATE >>";
}
public Class getPersistentClass() {
return persistentClass;
}
public boolean isUninitialized() {
return false;
}
public SessionImplementor getSession() {
return null;
}
public void setSession(SessionImplementor s) throws HibernateException {
}
public Object getImplementation() {
return obj;
}
public Object getImplementation(SessionImplementor s) throws HibernateException {
return obj;
}
};
}
return proxy.invokeSuper(obj,args);
}
};
return (Factory) Enhancer.create(
(interfaces.length==1) ?
persistentClass :
null,
interfaces,
interceptor
);
}
catch (Throwable t) {
LogFactory.getLog(BasicLazyInitializer.class).error(
"CGLIB Enhancement failed: " +
persistentClass.getName(),
t
);
throw new HibernateException( "CGLIB Enhancement failed: " + persistentClass.getName(), t );
}
}