Pregunta

i have a Hibernate APP we are trying to implement Open Session in View Pattern with ZK i have a question Hibernate creates a session per thread and closes it but we have severals operations with [criteria and save or update] methods in one session we started to implement using update method we very quickly received the infamous

NONUNIQUEOBJECTEXCEPTION 

we have changed update() to merge() my question if the following.

if i have a Student class and trying to update the the student using update[making sure is not in session avoid the exception] but later in other method the student is required to updated again differently from the previous update this time we should merging and merge() method is called. i was wondering when [flush][commit] method is called the changes to the first student is loss? the changes to the both students is OK? what happens??

i am afraid the changes in the first student is loss! what can i do in this case?

thanks a lot a best regards.

¿Fue útil?

Solución

you should take a look at locking algorithm. There are 2 popular ones: optimistic and pessimistic. For web application optimistic locking usually works better. Long story short each revision of entity object is given a version label (often a bigint or string db column). When someone pre-empt your update and your version became stale, hibernate will throw optimistic lock exception, in which case you can capture and ask the user what to do (or take other appropriate measure)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top