lundi 2 mars 2015

DataBinder in Spring 4

I have a Controller which receive a map of values. One of this value is a ID of object which I should select from db (I have hiberante, sessionFactory). This is my create method:



Object newObject = getClazz().getConstructor().newInstance();
DataBinder db = new DataBinder(newObject);
MutablePropertyValues mpv = new MutablePropertyValues(values);
db.bind(mpv);
newObject = db.getBindingResult().getTarget();
getSession().saveOrUpdate(newObject);
getSession().refresh(newObject);


but it bind only primitive values like int or String. In user entity I have a many to many relation with group entity.


I know that I should write some converters but how? When I rewrite that create method to:



Object newObject = getClazz().getConstructor().newInstance();
BeanWrapper db = new BeanWrapperImpl(newObject);
db.setPropertyValues(values);
newObject = db.getWrappedClass();
getSession().saveOrUpdate(newObject);
getSession().refresh(newObject);


I have an exception:



Failed to convert property value of type 'java.util.ArrayList' to required type 'java.util.List' for property 'groups';
nested exception is java.lang.IllegalStateException: Cannot convert value of type
[java.lang.String] to required type [pl.flomedia.springtpl.models.Group]
for property 'groups[0]': no matching editors or conversion strategy found

Aucun commentaire:

Enregistrer un commentaire