struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Toni Lyytikäinen" <tol...@gmail.com>
Subject Re: persistence and doubts on usage of hibernate in struts 2
Date Fri, 29 Feb 2008 12:49:23 GMT
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
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message