Developing Spring Web Flow 2 Application part 5 – Persistence

As part of “Developing Spring Web Flow 2 Application” series, this post shows the following features:

  • Persistence




(from: Spring Web Flow Reference Guide)
Web Flow offers “flow managed persistence” where a flow can create, commit, and close a object persistence context for you.




Spring Web Flow provides a flow scoped PersistenceContext on flow startup and commits changes at the end of the flow. To use PersistenceContext, there are 3 steps involved.




1. Mark the flow with a persistence-context element

<persistence-context />



2. Configure a FlowExecutionListener and register it with flow executor.

If using Hibernate, register the HibernateFlowExecutionListener. If using JPA, register a jpaFlowExecutionListener.

<webflow:flow-executor id="flowExecutor"> <webflow:flow-execution-listeners> <webflow:listener ref="jpaFlowExecutionListener" /> </webflow:flow-execution-listeners> </webflow:flow-executor> <bean id="jpaFlowExecutionListener" class="org.springframework.webflow.persistence.JpaFlowExecutionListener" > <constructor-arg ref="entityManagerFactory" /> <constructor-arg ref="transactionManager" /> </bean>



3. To commit the change, add a commit attribute at the end state.

<end-state id="shop" commit="true" />



Then, you can use PersistenceContext in the flow to do things such as persisting an object.

<action-state id="persist"> <evaluate expression="persistenceContext.persist(order.address)" /> ... </action-state>



Advertisements
Developing Spring Web Flow 2 Application part 5 – Persistence

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s