My solution looks like this:

        public Foo getModel() {
                foo = (Foo) session.get("Foo");
                if (foo == null) {
                        foo = new Foo();
                        session.put("Foo", foo);
                }
                return (foo);
        }

Then, someone selects a particular record in the form and I populate:
foo = facade.findById(fromForm.getId());
session.put("Foo", foo);

Now, when I do the save, the getModel set foo up so that when I call the 
save method, I have a Hibernate proxy enabled object that Hibernate knows 
that it needs to update versus adding a new record. I just have to rejoin 
it to the session.

If it was a new record, then the getModel gives me an instantiated Foo (or 
pull from DI, such as Spring) and save causes Hibernate to add a row.

Is this best practice? I don't know, but this is what I do. I suspect that 
there is a better (more Struts standard) place to put the object instead 
of the session?

- Eric



From:
"CRANFORD, CHRIS" <chris.cranf...@setech.com>
To:
"Struts Users Mailing List" <user@struts.apache.org>
Date:
04/25/2011 11:52 PM
Subject:
ModelDriven Entity Update




In a form; I have a select box defined as:
<s:select name="propertyOfModel.id" list="myListOfValues" listKey="id"
listValue="description" />

In the model I have the propertyOfModel annotated as @ManyToOne as
follows:
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="PROPERTY_ID",nullable=true,insertable=true,updatable=t
rue)
public PropertyOfModel getPropertyOfModel() {
  // ...
}

Now the form gets submitted to a Struts2 Action which implements model
driven and uses the prepare() method to setup the entity model before
setting the values from the form by querying the service tier for the
object by ID.  The problem is that when the interceptor sets the values
on the model for propertyOfModel.id; if the original propertyOfModel
object was null; then there is no issue but if the object wasn't null
when queried from the database, then I get a transient exception because
it is as if the original object is having it's primary ID changed.

How have others worked around this?  One option I explored was for
setting propertyOfModel object by it's id; I simply set a property on
the action called propertyOfModelId and then use the ID to query the
object from its associated service and then set the object instance or
set to null if ID was null. 

Any other alternatives?

Chris


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



Reply via email to