I'm saving a 'Post', like a blog post, and JPA's merge function doesn't
update the object in place but rather, returns an object reflecting the new
database fields (like "id" if it were set).

In my application, I'd like to manage insert / update by setting this new
'Post' as the model for my form. In this case, Updates have ids ... but the
following gives me grief since the Form<?> is not typed. I can't cast to
IModel<?> - so not sure of the right thing to do here. Thoughts?


Here is the form field:

            final TextField<String> *postId* = new TextField<String>("id");
            postId.setOutputMarkupId(true);
            add(postId);


Here is my save handler:

                @Override
                protected void onSubmit(final AjaxRequestTarget target,
                                        final Form<?> form)
                {
                    ...
// works fine
                    post = (Post) form.getModelObject();
                    final Post postWithId = postService.save(post);
// works fine
                    final IModel<Post> newModel = new
CompoundPropertyModel<Post>(postWithId);

*// 1. I want to use this new 'Post' object but neither of these compile*
                    form.setModel(newModel);
                    form.setModelObject(postWithId);

ERROR: The method setModelObject(capture#6-of ?) in the type
Form<capture#6-of ?> is not applicable for the arguments (Post)



*2. This compiles fine but doesn't actually update the id field on screen:*

                    form.setDefaultModelObject(postWithId);
                    target.addComponent(*postId*);



*3. Or, instead resetting the model, I can manually set the id field on the
existing model - which is all I really want to accomplish in this case:
*
                    post = (Post) form.getModelObject();
                    final Post postWithId = postService.save(post);
                    post.setId(postWithId.getId());

                    feedbackPanel.add(new SimpleAttributeModifier("class",
"info"));
                    info(getString("m-successfully-saved"));

                    target.addComponent(feedbackPanel);
                    target.addComponent(postId);

This seems to work, updates the id on screen and causes subsequent JPA
'merge' invocations to update instead of insert. As you can see, being an
AJAX button, I did have to explicitly render the new id field .

So, is there a well-defined approach for this sort of thing?

1 - is there a way I can actually reset the model?
2 - when I reset the DefaultModelObject - the the id isn't rendering to
screen - which implies I don't know what the DefaultModelObject actually is.
3 - Is it appropriate to use the Form fields to help discern between updates
and inserts - or is there a more appropriate way to manage this within
Wicket that I'm missing. My next step is to HIDE the id on the screen - but
wanted to make sure that it was indeed a well-received way to even manage
this type of update.

Thanks,

-Luther

Reply via email to