Hibernate version: 3.1
In the Interceptor instantiate method I am attempting to proxy my entities. Any properties whether they are simple value types or other entities throw the following exception when trying to load.
Interceptor Code
Code:
public Object instantiate(String entityName, EntityMode entityMode, Serializable id) throws CallbackException {
if (isTargetEntity(entityName)) {
Object entity = Class.forName(entityName).newInstance();
return Proxy.newProxyInstance(
MyInterface.class.getClassLoader(),
new Class[]{MyInterface.class},
new EmptyStringToNullProxy(entity));
}
return null;
}
Proxy CodeCode:
public class EmptyStringToNullProxy implements InvocationHandler {
private Object obj;
public EmptyStringToNullProxy(Object o) {
obj = o;
}
public Object invoke(Object bean, Method method, Object[] args) throws Throwable {
Object result = null;
try {
if (method.getName().startsWith("set") && args != null &&
args.length == 1 && "".equals(args[0])) {
//Convert Metod name to proper Java Bean property name
String propertyName = method.getName().substring(3);
StringBuffer sb = new StringBuffer(propertyName.substring(0, 1).toLowerCase());
sb.append(propertyName.substring(1, propertyName.length()));
propertyName = sb.toString();
PropertyDescriptor desc = PropertyUtils.getPropertyDescriptor(bean, propertyName);
Method readMethod = desc.getReadMethod();
Object orgValue = readMethod.invoke(bean, new Object[0]);
if (orgValue != null) {
result = method.invoke(obj, args);
}
} else {
result = method.invoke(obj, args);
}
} catch (InvocationTargetException e) {
throw new RuntimeException("Unable to invoke method [" + method + "], with args [" + args
+ "], on object [" + bean + "].", e);
}
return result;
}
}
Stack Trace
14:19:06,074 ERROR [BasicPropertyAccessor] IllegalArgumentException in class: com.my.code.Person, setter method of property: name
14:19:06,074 ERROR [BasicPropertyAccessor] expected type: java.lang.String, actual value: java.lang.String
14:19:06,121 ERROR [HibernateTransactionManager] IllegalArgumentException occurred while calling setter of com.my.code.Person.name
org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of com.my.code.Person.name
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:104)
at org.hibernate.tuple.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:330)
at org.hibernate.tuple.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:188)
at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3232)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:126)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:842)
at org.hibernate.loader.Loader.doQuery(Loader.java:717)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2150)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
at org.hibernate.loader.Loader.list(Loader.java:2024)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:369)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:300)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:146)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1093)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:745)
at com.my.code.PersonDAO.findByReferenceNumber(PersonDAO.java:80)
Are you able to proxy entities in this way? Do I have to register my proxy somewhere?
Any help would be great...
Cheers,
Chris