posted 17 years ago
Hi, i'm having trouble populating an object into a form for editing it and updating it in my database.
The problem is that i'm using hibernate and it seems that hibernate knows when i receive an object from the form that this object isn't the same that i retrieved from my db and because of that it saves the same objects as two different rows in the db.
I know that this post might go at the ORM forum, but i'd like to know if there is a way to populate an object into the form and extract it (for updating) as the same one.
To clarify thing a bit, here is some code of my action dispatcher.
public ActionForward setUpForInsertOrUpdate (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception
{
DynaValidatorActionForm forumForm = (DynaValidatorActionForm form;
Transaction tx = TransactionFactory.begin();
ForumHibernateDao fdao=(ForumHibernateDao)DaoRegistry.get(Forum.class);
if (isUpdate(request, form)) {
Long id = ((Forum)forumForm.get("forum")).getId();
Forum f=fdao.find(id);
forumForm.set("forum", f);
}
return mapping.findForward("showadd");
}
public ActionForward save (ActionMapping mapping,
ActionForm form,
HttpServletRequest req,
HttpServletResponse res){
DynaValidatorActionForm forumForm = (DynaValidatorActionForm)form;
ActionErrors errors = forumForm.validate(mapping, req);
if (errors.isEmpty())
{
Transaction tx = TransactionFactory.begin();
ForumHibernateDao fdao=(ForumHibernateDao)DaoRegistry.get(Forum.class);
Forum f=(Forum)forumForm.get("forum");
fdao.save(f);
tx.commit();
return mapping.findForward("success");
}else{
saveMessages(req, errors);
isUpdate(req,form);
return mapping.findForward("showadd");
}
}
With this code i get the two Forums saved with different ids because the forums setted with forumForm.set("forum", f); and the one getted with Forum f=(Forum)forumForm.get("forum"); are different objects.
Thanks in advance and excuse me for my english
EDIT: I'm using request scope in my action mapping otherwise when i try to create a new forum (wich uses the same form) it's already populated with the previous edited one.
Of couse I can use session scope and after an update of insert i can use something like this: req.getSession().removeAttribute("forumForm"); but if think thats not nice.
[ October 11, 2007: Message edited by: Mariano Javier ]