ModelDriven CRUD validation failure still causes JPA update

View: New views
20 Messages — Rating Filter:   Alert me  
< Prev | 1 - 2 | Next >

ModelDriven CRUD validation failure still causes JPA update

by jon_french :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have a ModelDriven action which controls CRUD operations on JPA managed
Entity E. E has a property called ?name? which maps to a database column
with a NOT NULL constraint. I have added a RequiredStringValidator
annotation to my Action to validate that the ?model.name? property is
non-null. The validation works perfectly.
However, during Struts Interceptor processing of a user entered empty
string for the ?model.name? ServletRequest parameter, entity instance E
has its getName() property set to a null value. Hibernate (my JPA
implementation provider) detects this property state change when the
Hibernate session is flushed at the closing of the JPA EntityManager by
the configured OpenEntityManagerInView ServletFilter. This results in an
attempt by Hibernate to update the ?name? database column to null and
results in a SqlException.
So the problem is that even with appropriate validation on a ModelDriven
action, my Entity moves to an invalid state which Hibernate then tries to
persist to the database at the closing of the session.
Based on what I gleaned from this WebWorkx post:
http://forums.opensymphony.com/thread.jspa?messageID=5315ᓃ
(which seems to be the exact same problem except with the
OpenSessionInView filter instead of my OpenEntityManagerInView filter)
And this spring post:
http://forum.springframework.org/showthread.php?t=35740
 ? I think that what I need to do is set Hibernate?s flush mode to NEVER.
What this means to me is that Hibernate will only flush when I execute a
EntityManager.flush() command and that OpenEntityManagerInView?s closing
of the EntityManager won?t automatically result in a Hibernate session
flush and thus Entity E?s invalid state will not be persisted. This is in
line with what I understand to be the default behavior of Spring?s
OpenSessionInView filter. Unfortunately, I have not yet been able to set
Hibernate's flush mode (nor am I certain that this will solve my issue).
Can anyone shed some light on this problem?
Thanks in advance!

Jon French
Programmer
ASRC Management Services
ECOS Development Team
jon_french@...
970-226-9290

Fort Collins Science Center
US Geological Survey
2150 Centre Ave, Building C
Fort Collins, CO 80526-8116

Re: ModelDriven CRUD validation failure still causes JPA update

by Musachy Barroso :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

If there is a validation error the code on your action shouldn't be
executed at all, so you wouldn't have this problem. Are you applying
the "defaultWorkflow" interceptor to your action?

regards
musachy

On 10/1/07, Jon_French@... <Jon_French@...> wrote:

> I have a ModelDriven action which controls CRUD operations on JPA managed
> Entity E. E has a property called ?name? which maps to a database column
> with a NOT NULL constraint. I have added a RequiredStringValidator
> annotation to my Action to validate that the ?model.name? property is
> non-null. The validation works perfectly.
> However, during Struts Interceptor processing of a user entered empty
> string for the ?model.name? ServletRequest parameter, entity instance E
> has its getName() property set to a null value. Hibernate (my JPA
> implementation provider) detects this property state change when the
> Hibernate session is flushed at the closing of the JPA EntityManager by
> the configured OpenEntityManagerInView ServletFilter. This results in an
> attempt by Hibernate to update the ?name? database column to null and
> results in a SqlException.
> So the problem is that even with appropriate validation on a ModelDriven
> action, my Entity moves to an invalid state which Hibernate then tries to
> persist to the database at the closing of the session.
> Based on what I gleaned from this WebWorkx post:
> http://forums.opensymphony.com/thread.jspa?messageID=5315ᓃ
> (which seems to be the exact same problem except with the
> OpenSessionInView filter instead of my OpenEntityManagerInView filter)
> And this spring post:
> http://forum.springframework.org/showthread.php?t=35740
>  ? I think that what I need to do is set Hibernate?s flush mode to NEVER.
> What this means to me is that Hibernate will only flush when I execute a
> EntityManager.flush() command and that OpenEntityManagerInView?s closing
> of the EntityManager won?t automatically result in a Hibernate session
> flush and thus Entity E?s invalid state will not be persisted. This is in
> line with what I understand to be the default behavior of Spring?s
> OpenSessionInView filter. Unfortunately, I have not yet been able to set
> Hibernate's flush mode (nor am I certain that this will solve my issue).
> Can anyone shed some light on this problem?
> Thanks in advance!
>
> Jon French
> Programmer
> ASRC Management Services
> ECOS Development Team
> jon_french@...
> 970-226-9290
>
> Fort Collins Science Center
> US Geological Survey
> 2150 Centre Ave, Building C
> Fort Collins, CO 80526-8116


--
"Hey you! Would you help me to carry the stone?" Pink Floyd

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


Re: ModelDriven CRUD validation failure still causes JPA update

by jon_french :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

True. The action's "execute" method is not being called. However, in the
ModelDriven pattern, the invalid (in my case Null) request parameters are
set on the Model during request processing. This is opposed to a
non-ModelDriven action where the parameters would be set on Action
properties.

Since my Model is acquired from the Hibernate session (during the
"prepare()" Action method), it is a transactional persistent instance.
Note this from the hibernate documentation:

"Transactional persistent instances (ie. objects loaded, saved, created or
queried by the Session) may be manipulated by the application and any
changes to persistent state will be persisted when the Session  is flushed
(discussed later in this chapter). There is no need to call a particular
method (like update(), which has a different purpose) to make your
modifications persistent."
(http://www.hibernate.org/hib_docs/v3/reference/en/html/objectstate.html#objectstate-modifying)

So when the OpenEntityManagerInView filter closes the EntityManager at the
end of request processing (even when validation fails), Hibernate attempts
to write the invalid model object to the database.

best,

Jon French
Programmer
ASRC Management Services
ECOS Development Team
jon_french@...
970-226-9290

Fort Collins Science Center
US Geological Survey
2150 Centre Ave, Building C
Fort Collins, CO 80526-8116



"Musachy Barroso" <musachy@...>
10/01/2007 02:42 PM
Please respond to
"Struts Users Mailing List" <user@...>


To
"Struts Users Mailing List" <user@...>
cc

Subject
Re: ModelDriven CRUD validation failure still causes JPA update






If there is a validation error the code on your action shouldn't be
executed at all, so you wouldn't have this problem. Are you applying
the "defaultWorkflow" interceptor to your action?

regards
musachy

On 10/1/07, Jon_French@... <Jon_French@...> wrote:
> I have a ModelDriven action which controls CRUD operations on JPA
managed

> Entity E. E has a property called ?name? which maps to a database column
> with a NOT NULL constraint. I have added a RequiredStringValidator
> annotation to my Action to validate that the ?model.name? property is
> non-null. The validation works perfectly.
> However, during Struts Interceptor processing of a user entered empty
> string for the ?model.name? ServletRequest parameter, entity instance E
> has its getName() property set to a null value. Hibernate (my JPA
> implementation provider) detects this property state change when the
> Hibernate session is flushed at the closing of the JPA EntityManager by
> the configured OpenEntityManagerInView ServletFilter. This results in an
> attempt by Hibernate to update the ?name? database column to null and
> results in a SqlException.
> So the problem is that even with appropriate validation on a ModelDriven
> action, my Entity moves to an invalid state which Hibernate then tries
to
> persist to the database at the closing of the session.
> Based on what I gleaned from this WebWorkx post:
> http://forums.opensymphony.com/thread.jspa?messageID=5315ᓃ
> (which seems to be the exact same problem except with the
> OpenSessionInView filter instead of my OpenEntityManagerInView filter)
> And this spring post:
> http://forum.springframework.org/showthread.php?t=35740
>  ? I think that what I need to do is set Hibernate?s flush mode to
NEVER.
> What this means to me is that Hibernate will only flush when I execute a
> EntityManager.flush() command and that OpenEntityManagerInView?s closing
> of the EntityManager won?t automatically result in a Hibernate session
> flush and thus Entity E?s invalid state will not be persisted. This is
in

> line with what I understand to be the default behavior of Spring?s
> OpenSessionInView filter. Unfortunately, I have not yet been able to set
> Hibernate's flush mode (nor am I certain that this will solve my issue).
> Can anyone shed some light on this problem?
> Thanks in advance!
>
> Jon French
> Programmer
> ASRC Management Services
> ECOS Development Team
> jon_french@...
> 970-226-9290
>
> Fort Collins Science Center
> US Geological Survey
> 2150 Centre Ave, Building C
> Fort Collins, CO 80526-8116


--
"Hey you! Would you help me to carry the stone?" Pink Floyd

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



Re: ModelDriven CRUD validation failure still causes JPA update

by newton.dave :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I haven't tried this, but perhaps changing the order
the interceptors are specified is a potential way out.

--- Jon_French@... wrote:

> True. The action's "execute" method is not being
> called. However, in the
> ModelDriven pattern, the invalid (in my case Null)
> request parameters are
> set on the Model during request processing. This is
> opposed to a
> non-ModelDriven action where the parameters would be
> set on Action
> properties.
>
> Since my Model is acquired from the Hibernate
> session (during the
> "prepare()" Action method), it is a transactional
> persistent instance.
> Note this from the hibernate documentation:
>
> "Transactional persistent instances (ie. objects
> loaded, saved, created or
> queried by the Session) may be manipulated by the
> application and any
> changes to persistent state will be persisted when
> the Session  is flushed
> (discussed later in this chapter). There is no need
> to call a particular
> method (like update(), which has a different
> purpose) to make your
> modifications persistent."
>
(http://www.hibernate.org/hib_docs/v3/reference/en/html/objectstate.html#objectstate-modifying)

>
> So when the OpenEntityManagerInView filter closes
> the EntityManager at the
> end of request processing (even when validation
> fails), Hibernate attempts
> to write the invalid model object to the database.
>
> best,
>
> Jon French
> Programmer
> ASRC Management Services
> ECOS Development Team
> jon_french@...
> 970-226-9290
>
> Fort Collins Science Center
> US Geological Survey
> 2150 Centre Ave, Building C
> Fort Collins, CO 80526-8116
>
>
>
> "Musachy Barroso" <musachy@...>
> 10/01/2007 02:42 PM
> Please respond to
> "Struts Users Mailing List" <user@...>
>
>
> To
> "Struts Users Mailing List" <user@...>
> cc
>
> Subject
> Re: ModelDriven CRUD validation failure still causes
> JPA update
>
>
>
>
>
>
> If there is a validation error the code on your
> action shouldn't be
> executed at all, so you wouldn't have this problem.
> Are you applying
> the "defaultWorkflow" interceptor to your action?
>
> regards
> musachy
>
> On 10/1/07, Jon_French@... <Jon_French@...>
> wrote:
> > I have a ModelDriven action which controls CRUD
> operations on JPA
> managed
> > Entity E. E has a property called ?name? which
> maps to a database column
> > with a NOT NULL constraint. I have added a
> RequiredStringValidator
> > annotation to my Action to validate that the
> ?model.name? property is
> > non-null. The validation works perfectly.
> > However, during Struts Interceptor processing of a
> user entered empty
> > string for the ?model.name? ServletRequest
> parameter, entity instance E
> > has its getName() property set to a null value.
> Hibernate (my JPA
> > implementation provider) detects this property
> state change when the
> > Hibernate session is flushed at the closing of the
> JPA EntityManager by
> > the configured OpenEntityManagerInView
> ServletFilter. This results in an
> > attempt by Hibernate to update the ?name? database
> column to null and
> > results in a SqlException.
> > So the problem is that even with appropriate
> validation on a ModelDriven
> > action, my Entity moves to an invalid state which
> Hibernate then tries
> to
> > persist to the database at the closing of the
> session.
> > Based on what I gleaned from this WebWorkx post:
> >
>
http://forums.opensymphony.com/thread.jspa?messageID=5315ᓃ
> > (which seems to be the exact same problem except
> with the
> > OpenSessionInView filter instead of my
> OpenEntityManagerInView filter)
> > And this spring post:
> >
>
http://forum.springframework.org/showthread.php?t=35740

> >  ? I think that what I need to do is set
> Hibernate?s flush mode to
> NEVER.
> > What this means to me is that Hibernate will only
> flush when I execute a
> > EntityManager.flush() command and that
> OpenEntityManagerInView?s closing
> > of the EntityManager won?t automatically result in
> a Hibernate session
> > flush and thus Entity E?s invalid state will not
> be persisted. This is
> in
> > line with what I understand to be the default
> behavior of Spring?s
> > OpenSessionInView filter. Unfortunately, I have
> not yet been able to set
> > Hibernate's flush mode (nor am I certain that this
> will solve my issue).
> > Can anyone shed some light on this problem?
> > Thanks in advance!
> >
> > Jon French
> > Programmer
> > ASRC Management Services
> > ECOS Development Team
> > jon_french@...
> > 970-226-9290
> >
> > Fort Collins Science Center
> > US Geological Survey
> > 2150 Centre Ave, Building C
> > Fort Collins, CO 80526-8116
>
>
> --
> "Hey you! Would you help me to carry the stone?"
> Pink Floyd
>
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> user-unsubscribe@...
> For additional commands, e-mail:
> user-help@...
>
>
>


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


Re: ModelDriven CRUD validation failure still causes JPA update

by Piero Sartini-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> Since my Model is acquired from the Hibernate session (during the
> "prepare()" Action method), it is a transactional persistent instance.
> Note this from the hibernate documentation:
>
> "Transactional persistent instances (ie. objects loaded, saved, created or
> queried by the Session) may be manipulated by the application and any
> changes to persistent state will be persisted when the Session  is flushed
> (discussed later in this chapter). There is no need to call a particular
> method (like update(), which has a different purpose) to make your
> modifications persistent."

I do not know hibernate but you could try the following solutions:

* don't flush the session and destroy the object

* instantiate a temporary bean used for the form only:
BeanUtils.copyProperties(tempBean, yourBean);

* manage your entities with JPA's EntityManager, merge them manually.

Piero

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


Re: ModelDriven CRUD validation failure still causes JPA update

by Musachy Barroso :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> * instantiate a temporary bean used for the form only:
> BeanUtils.copyProperties(tempBean, yourBean);
>

That's what I do.

musachy
--
"Hey you! Would you help me to carry the stone?" Pink Floyd

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


Re: ModelDriven CRUD validation failure still causes JPA update

by jon_french :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

That's an interesting idea Dave. I'm using the paramsPrepareStack. It'll
take some investigation to see if that would fix the issue.

best,

Jon French
Programmer
ASRC Management Services
ECOS Development Team
jon_french@...
970-226-9290

Fort Collins Science Center
US Geological Survey
2150 Centre Ave, Building C
Fort Collins, CO 80526-8116



Dave Newton <newton.dave@...>
10/01/2007 03:32 PM
Please respond to
"Struts Users Mailing List" <user@...>


To
Struts Users Mailing List <user@...>
cc

Subject
Re: ModelDriven CRUD validation failure still causes JPA update






I haven't tried this, but perhaps changing the order
the interceptors are specified is a potential way out.

--- Jon_French@... wrote:

> True. The action's "execute" method is not being
> called. However, in the
> ModelDriven pattern, the invalid (in my case Null)
> request parameters are
> set on the Model during request processing. This is
> opposed to a
> non-ModelDriven action where the parameters would be
> set on Action
> properties.
>
> Since my Model is acquired from the Hibernate
> session (during the
> "prepare()" Action method), it is a transactional
> persistent instance.
> Note this from the hibernate documentation:
>
> "Transactional persistent instances (ie. objects
> loaded, saved, created or
> queried by the Session) may be manipulated by the
> application and any
> changes to persistent state will be persisted when
> the Session  is flushed
> (discussed later in this chapter). There is no need
> to call a particular
> method (like update(), which has a different
> purpose) to make your
> modifications persistent."
>
(
http://www.hibernate.org/hib_docs/v3/reference/en/html/objectstate.html#objectstate-modifying
)

>
> So when the OpenEntityManagerInView filter closes
> the EntityManager at the
> end of request processing (even when validation
> fails), Hibernate attempts
> to write the invalid model object to the database.
>
> best,
>
> Jon French
> Programmer
> ASRC Management Services
> ECOS Development Team
> jon_french@...
> 970-226-9290
>
> Fort Collins Science Center
> US Geological Survey
> 2150 Centre Ave, Building C
> Fort Collins, CO 80526-8116
>
>
>
> "Musachy Barroso" <musachy@...>
> 10/01/2007 02:42 PM
> Please respond to
> "Struts Users Mailing List" <user@...>
>
>
> To
> "Struts Users Mailing List" <user@...>
> cc
>
> Subject
> Re: ModelDriven CRUD validation failure still causes
> JPA update
>
>
>
>
>
>
> If there is a validation error the code on your
> action shouldn't be
> executed at all, so you wouldn't have this problem.
> Are you applying
> the "defaultWorkflow" interceptor to your action?
>
> regards
> musachy
>
> On 10/1/07, Jon_French@... <Jon_French@...>
> wrote:
> > I have a ModelDriven action which controls CRUD
> operations on JPA
> managed
> > Entity E. E has a property called ?name? which
> maps to a database column
> > with a NOT NULL constraint. I have added a
> RequiredStringValidator
> > annotation to my Action to validate that the
> ?model.name? property is
> > non-null. The validation works perfectly.
> > However, during Struts Interceptor processing of a
> user entered empty
> > string for the ?model.name? ServletRequest
> parameter, entity instance E
> > has its getName() property set to a null value.
> Hibernate (my JPA
> > implementation provider) detects this property
> state change when the
> > Hibernate session is flushed at the closing of the
> JPA EntityManager by
> > the configured OpenEntityManagerInView
> ServletFilter. This results in an
> > attempt by Hibernate to update the ?name? database
> column to null and
> > results in a SqlException.
> > So the problem is that even with appropriate
> validation on a ModelDriven
> > action, my Entity moves to an invalid state which
> Hibernate then tries
> to
> > persist to the database at the closing of the
> session.
> > Based on what I gleaned from this WebWorkx post:
> >
>
http://forums.opensymphony.com/thread.jspa?messageID=5315ᓃ
> > (which seems to be the exact same problem except
> with the
> > OpenSessionInView filter instead of my
> OpenEntityManagerInView filter)
> > And this spring post:
> >
>
http://forum.springframework.org/showthread.php?t=35740

> >  ? I think that what I need to do is set
> Hibernate?s flush mode to
> NEVER.
> > What this means to me is that Hibernate will only
> flush when I execute a
> > EntityManager.flush() command and that
> OpenEntityManagerInView?s closing
> > of the EntityManager won?t automatically result in
> a Hibernate session
> > flush and thus Entity E?s invalid state will not
> be persisted. This is
> in
> > line with what I understand to be the default
> behavior of Spring?s
> > OpenSessionInView filter. Unfortunately, I have
> not yet been able to set
> > Hibernate's flush mode (nor am I certain that this
> will solve my issue).
> > Can anyone shed some light on this problem?
> > Thanks in advance!
> >
> > Jon French
> > Programmer
> > ASRC Management Services
> > ECOS Development Team
> > jon_french@...
> > 970-226-9290
> >
> > Fort Collins Science Center
> > US Geological Survey
> > 2150 Centre Ave, Building C
> > Fort Collins, CO 80526-8116
>
>
> --
> "Hey you! Would you help me to carry the stone?"
> Pink Floyd
>
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> user-unsubscribe@...
> For additional commands, e-mail:
> user-help@...
>
>
>


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



Re: ModelDriven CRUD validation failure still causes JPA update

by newton.dave :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

--- Jon_French@... wrote:
> That's an interesting idea Dave. I'm using the
> paramsPrepareStack. It'll take some investigation to

> see if that would fix the issue.

Shouldn't take much; it might be as simple as adding a
validate/defaultWorkFlow up towards the top (although
I might try just adding new ones rather than removing
the existing ones).

> Fort Collins Science Center
> US Geological Survey
> 2150 Centre Ave, Building C

I used to live up Poudre River Canyon (Poudre Park); I
sure miss the climbing out there :(

d.


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


Re: ModelDriven CRUD validation failure still causes JPA update

by Musachy Barroso :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I only use the beanutils.copy solution on a few places, on the other
cases I always load the object model on the prepare() method if the
"id" field is set (editing), and then call update or create on the
entity manager in my action method. I never got that problem so I'm
wondering what is different. (I use JPA on top of hibernate)

musachy

On 10/1/07, Dave Newton <newton.dave@...> wrote:

> --- Jon_French@... wrote:
> > That's an interesting idea Dave. I'm using the
> > paramsPrepareStack. It'll take some investigation to
>
> > see if that would fix the issue.
>
> Shouldn't take much; it might be as simple as adding a
> validate/defaultWorkFlow up towards the top (although
> I might try just adding new ones rather than removing
> the existing ones).
>
> > Fort Collins Science Center
> > US Geological Survey
> > 2150 Centre Ave, Building C
>
> I used to live up Poudre River Canyon (Poudre Park); I
> sure miss the climbing out there :(
>
> d.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@...
> For additional commands, e-mail: user-help@...
>
>


--
"Hey you! Would you help me to carry the stone?" Pink Floyd

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


wierd problem

by henry5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


I am having a wierd problem where my action calls are not forwarding to the correct page.  Actually it works one time, but when I try to run the same action again (this time on the same page) it attempts to go to a different action (i.e. INPUT). Here is how I have it set up:
 
struts.xml:
<action name="ItemList" class="action.ItemAction" method="list">
<result name="success">/jsp/items.jsp</result>
</action>
<action name="Item" class="action.ItemAction" method="view">
<result name="input">/jsp/itemEdit.jsp</result>
<result name="success">/jsp/itemView.jsp</result>
</action>
 
items.jsp:
 
<s:form method="post" theme="simple" name="theForm">
<s:iterator value="resultList" status="status">
 
<a onClick=retrieveURL('<%= request.getContextPath() %>/Item!view.action?someBean.itemId=<s:property value="item_id"/>', main_span, null, "POST");>
<s:property value="item_details"/><br>
</a>
</s:iterator>
</s:form>
 
 
itemView.jsp:
<script language='javascript'>
function sort() {
  ... sorting code ...
  retrieveURL("<%= request.getContextPath() %>/Item!view.action", main_target, "POST");
}
function retrieveURL(url, target, postMethod) {
  try {
    var params = Form.serialize($('theForm'));
  } catch (e) {
    var params = "";
  }
  new Ajax.Request(url,
  {
    method:postMethod,
    parameters:params,
    postBody:params,
    onSuccess: function(transport) {
      var response = transport.responseText;
      target.innerHTML = response;
    },
    onLoading: function() {
      showLoader(true);
    },

  });
}
</script>
<s:form method="post" theme="simple" name="theForm">
<s:hidden name="someBean.itemId" value="%{someBean.itemId}" />
<s:hidden name="someBean.sortColumn" value="%{someBean.sortColumn}" />
<s:hidden name="someBean.sortDirection" value="%{someBean.sortDirection}" />
<s:property value="%{someBean.itemId}"/><br>
<s:property value="%{someBean.itemName}"/><br>
<s:property value="%{someBean.itemDescription}"/><br>
<hr>
<input type='button' onClick='sort("actual");' value='Sort'/>
<s:iterator value="AssociatedItemList" status="status">
<s:property value="dateRange"/> - <s:property value="actual"/> - <s:property value="target"/><br>
</s:iterator>
 
So on 'items.jsp' I click on a line item and it successfully forwards to the 'success' page. However, on the details screen when click on 'sort', it calls the same action but does not return to the same page.
 
_________________________________________________________________
Connect to the next generation of MSN Messenger 
http://imagine-msn.com/messenger/launch80/default.aspx?locale=en-us&source=wlmailtagline

Re: ModelDriven CRUD validation failure still causes JPA update

by jon_french :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks Piero:

* don't flush the session and destroy the object

This is exactly what I want to do. However, the Hibernate session
automatically flushes when the EntityManager is closed during the
OpenEntityManagerInView filter exit filter operation. I definitely need
the filter in place to lazy load collections in my view pages.

* instantiate a temporary bean used for the form only:
BeanUtils.copyProperties(tempBean, yourBean);

Yes, but I'm reluctant to use this work-around although I admit that it
would probably work. IMO, the beauty of the ModelDriven pattern is that
you don't have to do this copy but can rather cut out the old Struts 1
ActionForm "middle-man" bean and do direct updates to your business
objects. Everything works great except this one problem with a validation
failure inappropriately triggering a database update.

* manage your entities with JPA's EntityManager, merge them manually.

I do. In fact, if the validation does NOT fail, my ModelDriven action
calls a JPA backed DAO that does update the entity with a
EntityManager.merge(...) call. In the case of failed validation, this DAO
is never invoked, but rather Hibernate automatically "merges" the invalid
Entity changes to the database when the Hibernate Session is closed.

Thanks for you help,

Jon French
Programmer
ASRC Management Services
ECOS Development Team
jon_french@...
970-226-9290

Fort Collins Science Center
US Geological Survey
2150 Centre Ave, Building C
Fort Collins, CO 80526-8116



Piero Sartini <lists@...>
10/01/2007 03:38 PM
Please respond to
"Struts Users Mailing List" <user@...>


To
"Struts Users Mailing List" <user@...>
cc

Subject
Re: ModelDriven CRUD validation failure still causes JPA update






> Since my Model is acquired from the Hibernate session (during the
> "prepare()" Action method), it is a transactional persistent instance.
> Note this from the hibernate documentation:
>
> "Transactional persistent instances (ie. objects loaded, saved, created
or
> queried by the Session) may be manipulated by the application and any
> changes to persistent state will be persisted when the Session  is
flushed
> (discussed later in this chapter). There is no need to call a particular
> method (like update(), which has a different purpose) to make your
> modifications persistent."

I do not know hibernate but you could try the following solutions:

* don't flush the session and destroy the object

* instantiate a temporary bean used for the form only:
BeanUtils.copyProperties(tempBean, yourBean);

* manage your entities with JPA's EntityManager, merge them manually.

Piero

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



Re: ModelDriven CRUD validation failure still causes JPA update

by jon_french :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Yes, musachy. I do something very similar. Here is my prepare() method:

    public void prepare() throws Exception {
        if( this.id==0 ) {
            this.entity = new Entity();
        } else {
            this.entity = this.entityDao.findById(this.id);
        }
    }

Here is the (obvious) getModel():

    public Object getModel() {
        return this.entity;
    }

Here is the update, "execute()" method that is called upon form
submission:

    @Validations(
           requiredStrings =
                    {@RequiredStringValidator(fieldName = "model.name",
message = "You must enter a title.")}
    )
    public String update() {
        if( this.id==0 ) {
            this.entityDao.create(this.entity);
        } else {
            this.entity.update(this.entity);
        }
        return SUCCESS;
    }

So when a user enters a blank "model.name" field, this.entity.name
property gets set to null, the update() method never is called, but
Hibernate tries to write the update to the database when
OpenEntityManagerInViewFilter closes the EntityManager:

Here is the relevant code from OpenEntityManagerInViewFilter

      protected void doFilterInternal(
                        HttpServletRequest request, HttpServletResponse
response, FilterChain filterChain)
                        throws ServletException, IOException {

              ...

                try {
                        filterChain.doFilter(request, response);
                }

                finally {
                        if (!participate) {
                                EntityManagerHolder emHolder =
(EntityManagerHolder)
 TransactionSynchronizationManager.unbindResource(emf);
                                emHolder.getEntityManager().close();
                        }
                }
        }

Jon French
Programmer
ASRC Management Services
ECOS Development Team
jon_french@...
970-226-9290

Fort Collins Science Center
US Geological Survey
2150 Centre Ave, Building C
Fort Collins, CO 80526-8116



"Musachy Barroso" <musachy@...>
10/01/2007 05:10 PM
Please respond to
"Struts Users Mailing List" <user@...>


To
"Struts Users Mailing List" <user@...>
cc

Subject
Re: ModelDriven CRUD validation failure still causes JPA update






I only use the beanutils.copy solution on a few places, on the other
cases I always load the object model on the prepare() method if the
"id" field is set (editing), and then call update or create on the
entity manager in my action method. I never got that problem so I'm
wondering what is different. (I use JPA on top of hibernate)

musachy

On 10/1/07, Dave Newton <newton.dave@...> wrote:

> --- Jon_French@... wrote:
> > That's an interesting idea Dave. I'm using the
> > paramsPrepareStack. It'll take some investigation to
>
> > see if that would fix the issue.
>
> Shouldn't take much; it might be as simple as adding a
> validate/defaultWorkFlow up towards the top (although
> I might try just adding new ones rather than removing
> the existing ones).
>
> > Fort Collins Science Center
> > US Geological Survey
> > 2150 Centre Ave, Building C
>
> I used to live up Poudre River Canyon (Poudre Park); I
> sure miss the climbing out there :(
>
> d.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@...
> For additional commands, e-mail: user-help@...
>
>


--
"Hey you! Would you help me to carry the stone?" Pink Floyd

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



Re: ModelDriven CRUD validation failure still causes JPA update

by Musachy Barroso :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Only difference is that I don't use model driven. Is there any setting
to tell hibernate to flush the session when closed or it does that by
default?

musachy

On 10/1/07, Jon_French@... <Jon_French@...> wrote:

> Yes, musachy. I do something very similar. Here is my prepare() method:
>
>     public void prepare() throws Exception {
>         if( this.id==0 ) {
>             this.entity = new Entity();
>         } else {
>             this.entity = this.entityDao.findById(this.id);
>         }
>     }
>
> Here is the (obvious) getModel():
>
>     public Object getModel() {
>         return this.entity;
>     }
>
> Here is the update, "execute()" method that is called upon form
> submission:
>
>     @Validations(
>            requiredStrings =
>                     {@RequiredStringValidator(fieldName = "model.name",
> message = "You must enter a title.")}
>     )
>     public String update() {
>         if( this.id==0 ) {
>             this.entityDao.create(this.entity);
>         } else {
>             this.entity.update(this.entity);
>         }
>         return SUCCESS;
>     }
>
> So when a user enters a blank "model.name" field, this.entity.name
> property gets set to null, the update() method never is called, but
> Hibernate tries to write the update to the database when
> OpenEntityManagerInViewFilter closes the EntityManager:
>
> Here is the relevant code from OpenEntityManagerInViewFilter
>
>       protected void doFilterInternal(
>                         HttpServletRequest request, HttpServletResponse
> response, FilterChain filterChain)
>                         throws ServletException, IOException {
>
>               ...
>
>                 try {
>                         filterChain.doFilter(request, response);
>                 }
>
>                 finally {
>                         if (!participate) {
>                                 EntityManagerHolder emHolder =
> (EntityManagerHolder)
>  TransactionSynchronizationManager.unbindResource(emf);
>                                 emHolder.getEntityManager().close();
>                         }
>                 }
>         }
>
> Jon French
> Programmer
> ASRC Management Services
> ECOS Development Team
> jon_french@...
> 970-226-9290
>
> Fort Collins Science Center
> US Geological Survey
> 2150 Centre Ave, Building C
> Fort Collins, CO 80526-8116
>
>
>
> "Musa