• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

DynaValidatorActionForm not preserving object reference

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One possible solution might be to use the Hibernate session's merge method rather than the save method. The merge method will always update the record with the same key if found rather than adding a new one.

If you have an object for which the key is automatically generated, you may not know what the key value is. In that case you may want to query for the object first by some other criteria so that you have the key of the object to be merged.
 
Mariano Javier
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot, i'll go with te merge by creating a new method for updates in my DAO. I has hoping not to do this and trying to get the same object from the form that i previously populated.
 
The airline is called "Virgin"? Don't you want a plane to go all the way? This tiny ad will go all the way:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic