Hello. I am having a problem with the usage of hibernate and its persistence mechanism.

I have an Action with a method for creation of an entity type and other method for updating. This action implements a Preparable and in its method prepare() I verify if the entity is being created or updated, depending if an ID came from the jsp form.


public void prepare() throws Exception {
if (getId() == null) {
operador = new Operador();
} else {
operador = getSegurancaService().carregarOperador(id);
}
}

(This Operador is a model with a corresponding table on the database.)

Before saving, I have a condition to verify. I need to verify if there is another row in the table Operador in the database with the same value in the field Operador.email, for instance. In my previous systems working with another languages, I did the following in this case:

- fetch object from database
- update its properties with the form values
- made the necessary validations, and IF the validations pass, save the object (for instance calling a method Object.save). I am doing manual validations in the action.

However, for hibernate this does not work, because it considers the updated object as part of the database. So when I try to fetch in the database to see if there is already a row with Operador.email field with the value entered in the form, the Hibernate automatically saves the current operador before fetching and will ever find a row (the current) with the desired value for e-mail field.


Is there some way to disable this auto-saving behaviour in Hibernate?

I thought about ever creating a new object to receive the form data, after that make the validations I need to, and after the validations pass, get the object I want to update from the database and do a kind of merge between the new object with the form data and the object from the database;

What do you do in these situations? What do you recommend? thanks.




Vinicius Cubas Brand
DATAPROM®
Software para Clientes
www.dataprom.com

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to