Storing an entity between requests

View: New views
9 Messages — Rating Filter:   Alert me  

Storing an entity between requests

by Stephen Nelson-7 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi

I've been building my app using Stripes and enjoying the simplicity  
immensely. I'm now working on some general functionality where an  
entity is loaded from the database and then displayed to the user;  
they make changes and then save it back. I have two event methods on  
my action bean at the moment - view and update.

Now what is happening is that the entity loaded when the view method  
is called isn't present when the update method is called. I've  
confirmed this by logging the persisted ID value within the update  
method and this is null.

Now, I don't want to reload it again when updating - in case its state  
has changed in the meantime and would be overwritten. Is there a built-
in way to do this that I'm missing? With other frameworks I've put  
things in the session directly but that seems a dirty thing to do when  
using Stripes!

I have the Prag Prog book on order if it's covered in there but would  
appreciate a pointer to keep me going until Amazon delivers!

Cheers,

--
Stephen Nelson




------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Stripes-users mailing list
Stripes-users@...
https://lists.sourceforge.net/lists/listinfo/stripes-users

Re: Storing an entity between requests

by Grzegorz Krugły :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I use method such like

    @Before(stages = LifecycleStage.BindingAndValidation)
    public void beforeBindingAndValidation() {
            node =
ejb.getNode(Long.parseLong(context.getRequest().getParameter("node.id")));
    }

to refetch any entities before Stripes does any binding. The method
comes from http://www.stripesframework.org/display/stripes/Best+Practices

HTH
Grzegorz



Stephen Nelson pisze:

> Hi
>
> I've been building my app using Stripes and enjoying the simplicity  
> immensely. I'm now working on some general functionality where an  
> entity is loaded from the database and then displayed to the user;  
> they make changes and then save it back. I have two event methods on  
> my action bean at the moment - view and update.
>
> Now what is happening is that the entity loaded when the view method  
> is called isn't present when the update method is called. I've  
> confirmed this by logging the persisted ID value within the update  
> method and this is null.
>
> Now, I don't want to reload it again when updating - in case its state  
> has changed in the meantime and would be overwritten. Is there a built-
> in way to do this that I'm missing? With other frameworks I've put  
> things in the session directly but that seems a dirty thing to do when  
> using Stripes!
>
> I have the Prag Prog book on order if it's covered in there but would  
> appreciate a pointer to keep me going until Amazon delivers!
>
> Cheers,
>
> --
> Stephen Nelson
>
>
>
>
> ------------------------------------------------------------------------------
> Come build with us! The BlackBerry(R) Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart your
> developing skills, take BlackBerry mobile applications to market and stay
> ahead of the curve. Join us from November 9 - 12, 2009. Register now!
> http://p.sf.net/sfu/devconference
> _______________________________________________
> Stripes-users mailing list
> Stripes-users@...
> https://lists.sourceforge.net/lists/listinfo/stripes-users
>
>  


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Stripes-users mailing list
Stripes-users@...
https://lists.sourceforge.net/lists/listinfo/stripes-users

Re: Storing an entity between requests

by Ben Gunter-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

So you're creating a new entity and persisting it then redirecting to a view and the entity is not there? If that's the case, then you're probably forgetting to commit the transaction after you save the entity.

-Ben

On Tue, Oct 27, 2009 at 10:53 AM, Stephen Nelson <stephen@...> wrote:
Hi

I've been building my app using Stripes and enjoying the simplicity
immensely. I'm now working on some general functionality where an
entity is loaded from the database and then displayed to the user;
they make changes and then save it back. I have two event methods on
my action bean at the moment - view and update.

Now what is happening is that the entity loaded when the view method
is called isn't present when the update method is called. I've
confirmed this by logging the persisted ID value within the update
method and this is null.

Now, I don't want to reload it again when updating - in case its state
has changed in the meantime and would be overwritten. Is there a built-
in way to do this that I'm missing? With other frameworks I've put
things in the session directly but that seems a dirty thing to do when
using Stripes!

I have the Prag Prog book on order if it's covered in there but would
appreciate a pointer to keep me going until Amazon delivers!

Cheers,

--
Stephen Nelson

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Stripes-users mailing list
Stripes-users@...
https://lists.sourceforge.net/lists/listinfo/stripes-users

Re: Storing an entity between requests

by Aaron Porter-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Stephen,
I always reload before the update but it sounds like you're trying to
avoid that. You could try @Session from the StripesStuff project.
@Session persists to/from the session without any work on your part.
You'll still have to re-attach the entity to the EntityManager before
updates will be persisted.

Aaron

Stephen Nelson wrote:

> Hi
>
> I've been building my app using Stripes and enjoying the simplicity  
> immensely. I'm now working on some general functionality where an  
> entity is loaded from the database and then displayed to the user;  
> they make changes and then save it back. I have two event methods on  
> my action bean at the moment - view and update.
>
> Now what is happening is that the entity loaded when the view method  
> is called isn't present when the update method is called. I've  
> confirmed this by logging the persisted ID value within the update  
> method and this is null.
>
> Now, I don't want to reload it again when updating - in case its state  
> has changed in the meantime and would be overwritten. Is there a built-
> in way to do this that I'm missing? With other frameworks I've put  
> things in the session directly but that seems a dirty thing to do when  
> using Stripes!
>
> I have the Prag Prog book on order if it's covered in there but would  
> appreciate a pointer to keep me going until Amazon delivers!
>
> Cheers,
>
> --
> Stephen Nelson
>
>
>
>
> ------------------------------------------------------------------------------
> Come build with us! The BlackBerry(R) Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart your
> developing skills, take BlackBerry mobile applications to market and stay
> ahead of the curve. Join us from November 9 - 12, 2009. Register now!
> http://p.sf.net/sfu/devconference
> _______________________________________________
> Stripes-users mailing list
> Stripes-users@...
> https://lists.sourceforge.net/lists/listinfo/stripes-users
>
>  


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Stripes-users mailing list
Stripes-users@...
https://lists.sourceforge.net/lists/listinfo/stripes-users

Re: Storing an entity between requests

by Stephen Nelson-7 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Aaron, Ben and Grzegorz

Thanks for the replies. I think a code snippet may be helpful as I'm  
probably not explaining what I'm trying to do very well...

@DefaultHandler
     @DontValidate
     public Resolution view() {
         ServiceProviderContext sp = getServiceProviderContext();
         serviceProvider = getService().getServiceProvider
(sp.getServiceProviderId());
         return new ForwardResolution("/WEB-INF/jsp/serviceProvider/
editAccount.jsp");
     }

     public Resolution update() {
         ServiceProviderContext sp = getServiceProviderContext();
         telephoneNumber.setTelephoneType
(TelephoneNumber.TelephoneType.LANDLINE);
         serviceProvider.addTelephoneNumber(telephoneNumber);
         ServiceProviderValidationResult result = getService
().updateServiceProvider(serviceProvider);

         if (result.isValidationSuccessful()) {
             return new RedirectResolution("/service-providers/" +  
getServiceProviderDisplayName());
         } else {
             return getContext().getSourcePageResolution();
         }
     }

I've taken out some error handling code to cut down the line count.  
What I'm trying to do is load the entity in the view method, allow the  
user to modify certain fields in editAccount.jsp and then update these  
changed fields along with the original unchanged data in the update  
method.

What is happening at the moment, I think, is that no state is  
persisted across the two requests - that's fine and I don't expect it  
to. I wonder if there is an built-in way to handle this transient  
state though? I don't want to re-load the entity before the update  
method as there is a small possibility that the entity may have  
already been updated during the users think time and this would result  
in that update being lost.

Many thanks

--
Stephen

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Stripes-users mailing list
Stripes-users@...
https://lists.sourceforge.net/lists/listinfo/stripes-users

Re: Storing an entity between requests

by Aaron Porter-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Stephen,
It sounds like you can accomplish what you're trying to do by marking
the ServiceProvider declaration in your ActionBean with @Session from
the StripesStuff project. That will save sessionProvider in the
HttpSession at the end of each request and restore it at the beginning
of subsequent requests.

You can get the StripesStuff jar at
http://sourceforge.net/projects/stripes-stuff/files/

Aaron

Stephen Nelson wrote:

> Hi Aaron, Ben and Grzegorz
>
> Thanks for the replies. I think a code snippet may be helpful as I'm  
> probably not explaining what I'm trying to do very well...
>
> @DefaultHandler
>      @DontValidate
>      public Resolution view() {
>          ServiceProviderContext sp = getServiceProviderContext();
>          serviceProvider = getService().getServiceProvider
> (sp.getServiceProviderId());
>          return new ForwardResolution("/WEB-INF/jsp/serviceProvider/
> editAccount.jsp");
>      }
>
>      public Resolution update() {
>          ServiceProviderContext sp = getServiceProviderContext();
>          telephoneNumber.setTelephoneType
> (TelephoneNumber.TelephoneType.LANDLINE);
>          serviceProvider.addTelephoneNumber(telephoneNumber);
>          ServiceProviderValidationResult result = getService
> ().updateServiceProvider(serviceProvider);
>
>          if (result.isValidationSuccessful()) {
>              return new RedirectResolution("/service-providers/" +  
> getServiceProviderDisplayName());
>          } else {
>              return getContext().getSourcePageResolution();
>          }
>      }
>
> I've taken out some error handling code to cut down the line count.  
> What I'm trying to do is load the entity in the view method, allow the  
> user to modify certain fields in editAccount.jsp and then update these  
> changed fields along with the original unchanged data in the update  
> method.
>
> What is happening at the moment, I think, is that no state is  
> persisted across the two requests - that's fine and I don't expect it  
> to. I wonder if there is an built-in way to handle this transient  
> state though? I don't want to re-load the entity before the update  
> method as there is a small possibility that the entity may have  
> already been updated during the users think time and this would result  
> in that update being lost.
>
> Many thanks
>
> --
> Stephen
>
> ------------------------------------------------------------------------------
> Come build with us! The BlackBerry(R) Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart your
> developing skills, take BlackBerry mobile applications to market and stay
> ahead of the curve. Join us from November 9 - 12, 2009. Register now!
> http://p.sf.net/sfu/devconference
> _______________________________________________
> Stripes-users mailing list
> Stripes-users@...
> https://lists.sourceforge.net/lists/listinfo/stripes-users
>
>  


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Stripes-users mailing list
Stripes-users@...
https://lists.sourceforge.net/lists/listinfo/stripes-users

Re: Storing an entity between requests

by Stephen Nelson-7 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Oct 28, 2009 at 1:30 PM, Aaron Porter <aaron@...> wrote:
Stephen,
It sounds like you can accomplish what you're trying to do by marking
the ServiceProvider declaration in your ActionBean with @Session from
the StripesStuff project. That will save sessionProvider in the
HttpSession at the end of each request and restore it at the beginning
of subsequent requests.

You can get the StripesStuff jar at
http://sourceforge.net/projects/stripes-stuff/files/

Aaron


Hi Aaron

Thanks for the reply. I'll take a look at the Stripes stuff shortly but your description sounds like the functionality I need. Can I also evict it from session when I've completed the update? That way I won't be carrying additional state in the session when it's not required.

Many thanks

--
Stephen

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Stripes-users mailing list
Stripes-users@...
https://lists.sourceforge.net/lists/listinfo/stripes-users

Re: Storing an entity between requests

by Aaron Porter-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>From a quick glance at the source you just have to set your entity to null when you're done with it and it will be removed from the session.

Aaron

Stephen Nelson wrote:
On Wed, Oct 28, 2009 at 1:30 PM, Aaron Porter <aaron@...> wrote:
Stephen,
It sounds like you can accomplish what you're trying to do by marking
the ServiceProvider declaration in your ActionBean with @Session from
the StripesStuff project. That will save sessionProvider in the
HttpSession at the end of each request and restore it at the beginning
of subsequent requests.

You can get the StripesStuff jar at
http://sourceforge.net/projects/stripes-stuff/files/

Aaron


Hi Aaron

Thanks for the reply. I'll take a look at the Stripes stuff shortly but your description sounds like the functionality I need. Can I also evict it from session when I've completed the update? That way I won't be carrying additional state in the session when it's not required.

Many thanks

--
Stephen

------------------------------------------------------------------------------ Come build with us! The BlackBerry(R) Developer Conference in SF, CA is the only developer event you need to attend this year. Jumpstart your developing skills, take BlackBerry mobile applications to market and stay ahead of the curve. Join us from November 9 - 12, 2009. Register now! http://p.sf.net/sfu/devconference

_______________________________________________ Stripes-users mailing list Stripes-users@... https://lists.sourceforge.net/lists/listinfo/stripes-users


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Stripes-users mailing list
Stripes-users@...
https://lists.sourceforge.net/lists/listinfo/stripes-users

Re: Storing an entity between requests

by Poitras Christian :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

 
You can remove the object from session either by setting it to null or removing it from session by it's key.
 

Remove objects from session

Just calling session.removeAttribute(key) will not work in all cases. SessionStoreInterceptor saves fields in session after event completes and only if no validation error occurs. To remove object from session manually, the best solutions are:

  1. No validation error: set field to null in the event (if it's not a primitive).
  2. Validation errors: use session.removeAttribute(key).
Christian

 

From: Stephen Nelson [mailto:stephen@...]
Sent: Wednesday, October 28, 2009 12:46 PM
To: Stripes Users List
Subject: Re: [Stripes-users] Storing an entity between requests

On Wed, Oct 28, 2009 at 1:30 PM, Aaron Porter <aaron@...> wrote:
Stephen,
It sounds like you can accomplish what you're trying to do by marking
the ServiceProvider declaration in your ActionBean with @Session from
the StripesStuff project. That will save sessionProvider in the
HttpSession at the end of each request and restore it at the beginning
of subsequent requests.

You can get the StripesStuff jar at
http://sourceforge.net/projects/stripes-stuff/files/

Aaron


Hi Aaron

Thanks for the reply. I'll take a look at the Stripes stuff shortly but your description sounds like the functionality I need. Can I also evict it from session when I've completed the update? That way I won't be carrying additional state in the session when it's not required.

Many thanks

--
Stephen

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Stripes-users mailing list
Stripes-users@...
https://lists.sourceforge.net/lists/listinfo/stripes-users