Hello all.
I am using Appfuse M5, Spring MVC + Hibernate. I have an object,
Application, that as a 1-many relationship with Place. The Application Pojo
looks like this:
private Set<Place> places;
@OneToMany (fetch = FetchType.EAGER)
@JoinTable(
name="applicationplace",
joinColumns = { @JoinColumn( name="idApplication") },
inverseJoinColumns = @JoinColumn( name="idPlace")
)
public Set<Place> getPlaces(){
return places;
}
In the database I have 3 tables:
Application
Place
ApplicationPlace
When I create a Application I can assign 0-n Places to that Application.
When I save the Application I have to use a Custom Property Editor to tell
Spring how to convert each id in the Places multiple-Select into a Place
object, so a reference to it can be saved in the ApplicationPlace table.
So far so good. I created a PlacePropertyEditor which looks like this:
public class PlacePropertyEditor extends PropertyEditorSupport
{
private GenericManager<Place, Long> placeManager = null;
public void setPlaceManager(GenericManager<Place, Long> placeManager) {
this.placeManager = placeManager;
}
public void setAsText(String id) {
Place place = placeManager.get(new Long(id));
setValue(place);
}
}
In my ApplicationFormController I have a setPlaceManager() method and it is
properly wired in my dispatcher-servlet.xml. Also in
ApplicationFormController I have overrided initBinder:
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder)
{
PlacePropertyEditor ppe = new PlacePropertyEditor();
ppe.setPlaceManager(placeManager);
binder.registerCustomEditor(Place.class, ppe);
}
But when I run testSave() in ApplicationFormControllerTest it fails.
public void testSave() throws Exception
{
MockHttpServletRequest request = newGet("/applicationform.html");
request.addParameter("id", "1");
ModelAndView mv = c.handleRequest(request, new MockHttpServletResponse());
Application application = (Application)
mv.getModel().get(c.getCommandName());
assertNotNull(application);
request = newPost("/applicationform.html");
super.objectToRequestParameters(application, request);
request.addParameter("applicationCode", "updated application code");
//request.addParameter("places", "1");
mv = c.handleRequest(request, new MockHttpServletResponse());
Errors errors = (Errors) mv.getModel().get(BindException.MODEL_KEY_PREFIX
+ "application");
assertNull(errors);
assertNotNull(request.getSession().getAttribute("successMessages"));
}
The line that fails is assertNull(errors); If I comment out the Custom
Property Editor it works. (But in the web page, without a Custom Property
Editor I get the typical error that I can't covert a Set to String) I have
tried adding request.addParameter("places", "1") to see if it was failing
because no idPlace was being sent in the request, but it fails either way.
It seems I am missing a step when defining my Custom Property Editor. Do I
need to wire my PlacePropertyEditor in the application-context.xml?
According to Chapter 5 of the Spring 2.0 Reference Manual I do. But I don't
see that Appfuse does this with its Custom Property Editors;
BaseFormController's initBinder() method registers Custom Property Editors
for Integer, Long, byte[] and Date but they aren't wired anywhere.
I am not sure exactly what is failing. I've tried putting log.debug()
statements in the code but they don't appear in the console or in my
surefire reports.
Anyone have an idea as to what I might be doing wrong?
Thanks,
Bob
--
View this message in context:
http://www.nabble.com/testSave-fails-using-custom-property-editor-tf4278929s2369.html#a12179185
Sent from the AppFuse - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]