<s:convertEntity> / org.jboss.seam.ui.EntityConverter fails to
return correct id when entity is wrapped Hibernate proxy (JavassistLazyInitializer)
--------------------------------------------------------------------------------------------------------------------------------------------------
Key: JBSEAM-1215
URL:
http://jira.jboss.com/jira/browse/JBSEAM-1215
Project: JBoss Seam
Issue Type: Bug
Components: JSF
Affects Versions: 1.2.1.GA
Environment: JBoss Seam 1.2.1, JBoss 4.0.5GA, Hibernate 3.2.1, JSF RI 1.2,
Facelets 1.1.12
Reporter: Kahli Burke
Assigned To: Pete Muir
Fix For: 1.3.0.ALPHA
In certain circumstances the <s:entityConverter> handler
org.jboss.seam.ui.EntityConverter will fail to return the correct id for the entity when
getAsString() is called. I have seen this occur when one of my components called
entityManager.clear(), and then on a subsequent page load a query was made. In this case,
the actual object used in getIdFromEntity() was a proxied object. The object was proxied
by Hibernate's JavassistLazyInitializer.
The getIdFromEntity() method looks for a field annotated with @Id, which this entity has
(we are using field annotations as opposed to method annotations), and then attempts to
get the id from the object using direct field access (through Reflections.get(field,
entity)). Because the object is wrapped, this returns the default value for the class.
In my case it returns 0 for the id since this entity uses a long for the id field.
If the getter method had been called instead, the correct non-zero value would have been
returned. And usually, this works because the object is an actual instance of the entity
class and has the correct value for the field.
My workaround for now is to create a subclass of EntityConverter (and using the very
useful @Install precedence, thank you!) that calls the superclass' implementation and
in cases where it looks like it might be incorrect, looks for a getter method and calls
that instead. There is probably a better way to do it, but I'm not familiar with the
Hibernate proxies or how to properly get at the underlying object.
@Override
protected Object getIdFromEntity(UIComponent cmp, FacesContext facesContext, Object
entity, Class entityClass) {
Object id = super.getIdFromEntity(cmp, facesContext, entity, entityClass);
// If the id looks like it came out incorrect from the field access
if ((id instanceof Number && ((Number) id).longValue() == 0) || (id ==
null)) {
List<Field> fields = Reflections.getFields(entityClass, Id.class);
if (fields.size() == 1) {
Field field = fields.get(0);
Method getter = null;
try {
getter = Reflections.getGetterMethod(entityClass, field.getName());
}
catch (IllegalArgumentException e) {
if (log.isDebugEnabled())
log.debug("Id #0 looked incorrect, but unable to find getter
method for field #1", id, field.getName());
return id;
}
if (getter != null) {
try {
id = Reflections.invoke(getter, entity);
}
catch (Exception e) {
log.error("Error invoking getter method #0, returning
original id #1", e, getter.getName(), id);
}
}
}
}
return id;
}
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: