Form backing object retrieved by Hibernate problem
My form backing object has a Map field.
When I create new backing object by "new" operator, form is rendered correct:
form.jsp
--- cut ---
<spring:bind path="myForm.myObject.mapField[${key}].stringField">
<input type="text" name="<c:out value="${status.expression}"/>" value="<c:out value="${status.value}"/>"/>
</spring:bind>
--- end cut ---
but, when I use this form for edit this object retrieved from DB by Hibernate, I has error:
org.springframework.beans.NullValueInNestedPathExc eption: Invalid property 'myObject.mapField[12]' of bean class [tld.domain.myObject]: Value of nested property 'myObject.mapField[12]' is null
org.springframework.beans.BeanWrapperImpl.getNeste dBeanWrapper(BeanWrapperImpl.java:482) org.springframework.beans.BeanWrapperImpl.getBeanW rapperForPropertyPath(BeanWrapperImpl.java:399) org.springframework.beans.BeanWrapperImpl.getBeanW rapperForPropertyPath(BeanWrapperImpl.java:400) org.springframework.beans.BeanWrapperImpl.getPrope rtyValue(BeanWrapperImpl.java:537) org.springframework.validation.BindException.getFi eldValue(BindException.java:265) org.springframework.web.servlet.support.BindStatus .(BindStatus.java:115) org.springframework.web.servlet.tags.BindStatus.(B indStatus.java:38) org.springframework.web.servlet.tags.BindTag.doSta rtTagInternal(BindTag.java:103) org.springframework.web.servlet.tags.RequestContex tAwareTag.doStartTag(RequestContextAwareTag.java:7 0)
at the same time expression <c:out value="${myForm.myObject.mapField[key].stringField}"/> display value of stringField correctly
I did debug application and find the following conclusion: this error throws becaouse map retrived by hibernate from DB has type net.sf.hibernate.collection.Map, but class org.springframework.beans.BeanWrapperImpl on method getPropertyValue has the folowing logic:
--- cut ---
....
import java.util.Map
....
else if (value instanceof Map) {
Map map = (Map) value;
return map.get(key);
}
-- end cut ---
i.e. problem exists because myObject.mapField has type net.sf.hibernate.collection.Map instead java.util.Map after retrieving by Hibernate,
net.sf.hibernate.collection.Map ia a persistent wrapper for a java.util.Map.
I think this problem exists for eny collection mapped to DB with Hibernate.
Any suggestions about elegant solving of this problem ?
sorry, I have mistake about assumption of problem reason
sorry, I have mistake about assumption of problem reason
net.sf.hibernate.collection.Map implements java.util.Map and piece of code "value instanceof Map" can not be reason for exception as I assumed ...
problem is on some other code, I do not know where :(
but Spring bind tags can not extract collection elements retrieved from DB by Hibernate and genrate exception, at the same time it is work for java collections (java.util.HashMap for example) ...
I can not explain this yet :(
//
thanx irbouho