You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by David Erickson <de...@cmcflex.com> on 2003/12/17 19:05:35 UTC

Strategy for out of date items through forms using hibernate?

Hi I am using Struts with Hibernate in a webapplication.. we are using forms
etc. The problem I am currently trying to decide how to handle is thus:

Assume user 1 loads up an object in a form and is modifying it.
Assume user 2 loads up the same object in a form and is also modifying it.
User 1 submits the modified object.
User 2 also submits the object, however his is out of date and I would like
the webapp to tell him that and show the differences that exist.

What is the best way to use hibernate to deal with this? What I have
attempted is using the built in timestamp feature in MySQL, I have a field
in my object that is timestamp but does not insert or update, thus mysql
controls its value. Then when the user submits the object I loaded another
copy of that object from the DB and tried to compare their dates.. however
this gave me an error saying:

net.sf.hibernate.NonUniqueObjectException: a different object with the same
identifier value was already associated with the session: 1, of class:
cmcflex.salesweb.model.prospect.Prospect

because of the object I loaded to compare.

What do do? Is there a better strategy for tackling this? I tried using the
actual <timestamp> in the properties for my object, but then when I tried to
submit the object the generated sql said "when id=? AND timestamp=?" meaning
it would not update if the timestamp is different, and I'm unsure if I could
even determine if it did or did not update.

Thanks in advance,
David


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Strategy for out of date items through forms using hibernate?

Posted by Jason Lea <ja...@kumachan.net.nz>.
With versioning hibernate increments the version number each time.  I 
believe this is prefrered to the timestamp method, but I think hibernate 
supports both.

(off the top of my head) I have a struts form which holds the fields etc 
plus the object I loaded from Hibernate.  I use DispatchLookUp actions 
so that all of my actions are selected from the button pressed eg it 
adds a dispatch=save, or dispatch=overwrite to decide which action to 
perform.  The save action would normally reconnect the hibernate 
session, start a hibernate transaction, get the object from the form, 
populate the object properties from the form fields, and end the 
transaction.

To overwrite (dispatch=overwrite), I think I throw the current hibernate 
session away, create a new one, load a new object using the objects id 
(this should get the latest version from the db), start transaction and 
do the same as the save above.


David Erickson wrote:

>>I have done this.  Hibernate supports versioning (using a version number
>>column), if you use this Hibernate can make sure the changes are not
>>overwritten.
>>
>>The basic process is:
>>1. Hibernate session A loads object A1 (with identifier 1234)
>>2. Hibernate session B loads object B1 (also with identifier 1234)
>>3. Session A modifies object A1 and saves it
>>4. B modifies object B1 and attempts to save it
>>5. Hibernate will detect the version number for the object has changed
>>and generate an exception (StaleObjectStateException)
>>6. Catch this exception, close the hibernate session, return an error
>>message to the user using ActionErrors etc
>>
>>I also have a refresh button so that at any time the user can press
>>refresh which reloads the object from hibernate so it is up to date.
>>When I return my stale object error I also allow the user to either
>>refresh (loses the users changes), cancel (do nothing, return to parent
>>menu), or overwrite.  Overwrite can attempt to overwrite the changes by
>>reloading the new object, but use the posted field changes to update and
>>then save the object.
>>    
>>
>
>Yes I figured this out just barely, rather I used timestamp instead of
>version (are there any drawbacks to that?).  I am interested in doing
>exactly what you did, but how did you go about overwriting the things that
>have been changed?
>
>Thanks,
>David
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>
>  
>
-- 

Jason Lea



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Strategy for out of date items through forms using hibernate?

Posted by David Erickson <de...@cmcflex.com>.
> I have done this.  Hibernate supports versioning (using a version number
> column), if you use this Hibernate can make sure the changes are not
> overwritten.
>
> The basic process is:
> 1. Hibernate session A loads object A1 (with identifier 1234)
> 2. Hibernate session B loads object B1 (also with identifier 1234)
> 3. Session A modifies object A1 and saves it
> 4. B modifies object B1 and attempts to save it
> 5. Hibernate will detect the version number for the object has changed
> and generate an exception (StaleObjectStateException)
> 6. Catch this exception, close the hibernate session, return an error
> message to the user using ActionErrors etc
>
> I also have a refresh button so that at any time the user can press
> refresh which reloads the object from hibernate so it is up to date.
> When I return my stale object error I also allow the user to either
> refresh (loses the users changes), cancel (do nothing, return to parent
> menu), or overwrite.  Overwrite can attempt to overwrite the changes by
> reloading the new object, but use the posted field changes to update and
> then save the object.

Yes I figured this out just barely, rather I used timestamp instead of
version (are there any drawbacks to that?).  I am interested in doing
exactly what you did, but how did you go about overwriting the things that
have been changed?

Thanks,
David


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Strategy for out of date items through forms using hibernate?

Posted by Jason Lea <ja...@kumachan.net.nz>.
I have done this.  Hibernate supports versioning (using a version number 
column), if you use this Hibernate can make sure the changes are not 
overwritten.

The basic process is:
1. Hibernate session A loads object A1 (with identifier 1234)
2. Hibernate session B loads object B1 (also with identifier 1234)
3. Session A modifies object A1 and saves it
4. B modifies object B1 and attempts to save it
5. Hibernate will detect the version number for the object has changed 
and generate an exception (StaleObjectStateException)
6. Catch this exception, close the hibernate session, return an error 
message to the user using ActionErrors etc

I also have a refresh button so that at any time the user can press 
refresh which reloads the object from hibernate so it is up to date.  
When I return my stale object error I also allow the user to either 
refresh (loses the users changes), cancel (do nothing, return to parent 
menu), or overwrite.  Overwrite can attempt to overwrite the changes by 
reloading the new object, but use the posted field changes to update and 
then save the object.

You should check out versioning on the hibernate web site for more info 
on that.


David Erickson wrote:

>Hi I am using Struts with Hibernate in a webapplication.. we are using forms
>etc. The problem I am currently trying to decide how to handle is thus:
>
>Assume user 1 loads up an object in a form and is modifying it.
>Assume user 2 loads up the same object in a form and is also modifying it.
>User 1 submits the modified object.
>User 2 also submits the object, however his is out of date and I would like
>the webapp to tell him that and show the differences that exist.
>
>What is the best way to use hibernate to deal with this? What I have
>attempted is using the built in timestamp feature in MySQL, I have a field
>in my object that is timestamp but does not insert or update, thus mysql
>controls its value. Then when the user submits the object I loaded another
>copy of that object from the DB and tried to compare their dates.. however
>this gave me an error saying:
>
>net.sf.hibernate.NonUniqueObjectException: a different object with the same
>identifier value was already associated with the session: 1, of class:
>cmcflex.salesweb.model.prospect.Prospect
>
>because of the object I loaded to compare.
>
>What do do? Is there a better strategy for tackling this? I tried using the
>actual <timestamp> in the properties for my object, but then when I tried to
>submit the object the generated sql said "when id=? AND timestamp=?" meaning
>it would not update if the timestamp is different, and I'm unsure if I could
>even determine if it did or did not update.
>
>Thanks in advance,
>David
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>
>  
>


-- 
Jason Lea



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: OT: Strategy for out of date items through forms using hibernate?

Posted by Vic Cekvenich <Ce...@baseBeans.com>.
I avoid timestamps.

Just make your where class = to the selct cluase.
In case any colum is changed, it fails.

(if you really need this)

.V


David Erickson wrote:

> Hi I am using Struts with Hibernate in a webapplication.. we are using forms
> etc. The problem I am currently trying to decide how to handle is thus:
> 
> Assume user 1 loads up an object in a form and is modifying it.
> Assume user 2 loads up the same object in a form and is also modifying it.
> User 1 submits the modified object.
> User 2 also submits the object, however his is out of date and I would like
> the webapp to tell him that and show the differences that exist.
> 
> What is the best way to use hibernate to deal with this? What I have
> attempted is using the built in timestamp feature in MySQL, I have a field
> in my object that is timestamp but does not insert or update, thus mysql
> controls its value. Then when the user submits the object I loaded another
> copy of that object from the DB and tried to compare their dates.. however
> this gave me an error saying:
> 
> net.sf.hibernate.NonUniqueObjectException: a different object with the same
> identifier value was already associated with the session: 1, of class:
> cmcflex.salesweb.model.prospect.Prospect
> 
> because of the object I loaded to compare.
> 
> What do do? Is there a better strategy for tackling this? I tried using the
> actual <timestamp> in the properties for my object, but then when I tried to
> submit the object the generated sql said "when id=? AND timestamp=?" meaning
> it would not update if the timestamp is different, and I'm unsure if I could
> even determine if it did or did not update.
> 
> Thanks in advance,
> David



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org