-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Save vs. Update in Struts Action
PostPosted: Tue Aug 24, 2004 9:14 am 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
Hello All,

Architecture of my first prod Hibernate app has gone surprisingly well so far, thanks to everyone surrounding this project for the contributions!

I'm stumbling on something that should be easy, but I'm just not seeing it. I searched this forum but found nothing. If this is answered in a Wiki and I missed it, please feel free to pelt me with rocks and garbage.

I'm using Struts DispatchActions for my basic CRUD operations, and because Hibernate support for saveOrUpdate() seems reliable, I have combined the Create & Update operations into one action method (and one DAO method). However, Hibernate knows the difference based on the state of the ID field (unsaved value) which in my case is a Long, and unsaved entities are set to null.

On my struts form, if I use a hidden field to carry the ID through to the Action, then it works for Updates because it is there, but it breaks for Creates because "" is posted rather than null, and I get funky results.

My workaround is to use a little logic in my form that puts the <html:hidden/> there only if this is an Update.

I'd like to avoid this in favor of something cleaner. I'm sure there is a best practice out there for this but I sure can't seem to locate it.

Thanks in Advance,

Chris


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 24, 2004 11:20 am 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
This isn't really a Hibernate question but I know how frustrating it can be looking for answers in different forums. I would recommend posting such questions in the Struts forum.

However, here's what I do with Struts. I set the primary key as a java.lang.String in my ActionForm. In your DispatchAction method retrieve the parameter and convert it to a Long (using some sort of Utility object with try-catch). Just go ahead and set that to your object and call saveOrUpdate() in Hibernate. If the Long value is null, the value will be saved, otherwise it is updated.

Hope that helps.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.