You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Vinicius Cubas Brand <vi...@dataprom.com> on 2008/02/29 13:23:23 UTC

persistence and doubts on usage of hibernate in struts 2

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: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: persistence and doubts on usage of hibernate in struts 2

Posted by Kedar Choudhary <ke...@helixtechsolutions.com>.
I think you will get better answer to your question in hibernate mailing
list, as the issue is related to hibernate rather than struts.

To disable 'auto-saving' behavior, you can evict the object from hibernate
session. So in your prepare method, call session.evict(operator).
Then, to save the modifications, you can call saveOrUpdate() or merge.

Regards,
Kedar

-----Original Message-----
From: Toni Lyytikäinen [mailto:tolyen@gmail.com] 
Sent: Friday, February 29, 2008 6:19 PM
To: Struts Users Mailing List
Subject: Re: persistence and doubts on usage of hibernate in struts 2

Hard to say where the problem lies without having a look at the DAO code.
Are you using OpenSessionInViewFilter or similar mechanism for lazy loading
in the view? If yes, try setting the param singleSession to false like this:

<filter-name>OSIVFilter</filter-name>
    <filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
    <init-param>
        <param-name>singleSession</param-name>
        <param-value>false</param-value>
    </init-param>

Also, you can define the field to be unique in the Hibernate mapping. That
way the object can only be persisted to database if the email field is
unique. Otherwise it will throw a ConstraintViolationException that you can
catch. But the recommended approach is to first try to fix the problems in
the DAO layer.


On Fri, Feb 29, 2008 at 2:23 PM, Vinicius Cubas Brand <
vinicius.brand@dataprom.com> wrote:

> 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(R)
> Software para Clientes
> www.dataprom.com
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


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


Re: persistence and doubts on usage of hibernate in struts 2

Posted by Toni Lyytikäinen <to...@gmail.com>.
Hard to say where the problem lies without having a look at the DAO code.
Are you using OpenSessionInViewFilter or similar mechanism for lazy loading
in the view? If yes, try setting the param singleSession to false like this:

<filter-name>OSIVFilter</filter-name>
    <filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
    <init-param>
        <param-name>singleSession</param-name>
        <param-value>false</param-value>
    </init-param>

Also, you can define the field to be unique in the Hibernate mapping. That
way the object can only be persisted to database if the email field is
unique. Otherwise it will throw a ConstraintViolationException that you can
catch. But the recommended approach is to first try to fix the problems in
the DAO layer.


On Fri, Feb 29, 2008 at 2:23 PM, Vinicius Cubas Brand <
vinicius.brand@dataprom.com> wrote:

> 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(R)
> Software para Clientes
> www.dataprom.com
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>