この戦略は Spring Web Flow (SWF)を利用します。 FMP を使う場合は、 OSIV の設定は不要です。 設定方法は、 SWF の Listener を設定している xml ファイルに、以下を記述すれば良いです。 JPA の場合は、以下のように記述を追加します。
JPA を使わず Hibernate のみの場合は、以下のように記述を追加します。
- <webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry">
- <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>
あとは、各々のスコープを利用するフローの xml に、
- <webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry">
- <webflow:flow-execution-listeners>
- <webflow:listener ref="hibernateFlowExecutionListener" />
- </webflow:flow-execution-listeners>
- </webflow:flow-executor>
- <bean id="hibernateFlowExecutionListener"
- class="org.springframework.webflow.persistence.HibernateFlowExecutionListener">
- <constructor-arg ref="sessionFactory" />
- <constructor-arg ref="transactionManager" />
- </bean>
のように、そのフローのスコープ内で Hibernate の Session が生きていることを設定します。 そして、スコープの終わりには、
- <?xml version="1.0" encoding="UTF-8"?>
- <flow xmlns="http://www.springframework.org/schema/webflow"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/webflow
- http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
- <persistence-context />
- ....
- </flow>
のように記述することで、トランザクションのコミットと同時に Session を閉じるようにします。
- <end-state id="bookingConfirmed" commit="true" />
以上で、 FMP の設定は終わりです。
まとめ
SWF の FMP を使うことで、手動で Hibernate と同期すること、つまり分離(detach)と付加(attach)を操作することなく、そのフロースコープを定義することで自動で同期してくれます。 References
Open Session in View by Hibernate Community
https://community.jboss.org/wiki/OpenSessionInView
Spring Web Flow 2 でのフロー・マネージド・パーシスタンス by IBM
OSIV by Spring Forum
OSIVF vs OSIVI
Session Strategy by Hibernate Reference