Hibernate の Session.flush() メソッドがメモリ内の現在のデータ状態をデータベースに書き込むことを理解しています。私はそれを使用し、それはうまく動作します。しかし、私の質問は次のとおりです: Session.flush() の使用は必須ですか?
Session.flush() を削除すると、データベースに何も挿入/更新されません。また、ログ ファイルにエラーはありません。
アプリケーションで Spring + Hibernate を使用しています。次のように web.xml で定義した OpenSessionInViewFilter も使用しています。
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
flushMode を COMMIT として使用しようとしましたが、それでも役に立ちませんでした。
私のSpring applicationContext.xmlには、データソース用の次の行があります:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="${jdbc.jndiName}" />
<property name="resourceRef" value="true" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>...</value>
</list>
</property>
</bean>
最後に、コード スニペットは次のようになります。
public String saveRegistration (final Registration registration) {
getHibernateTemplate().saveOrUpdate(registration);
getSession().flush(); // If I remove this, no records would be inserted/updated!!
String id = registration.getId();
return id;
}
ご覧のとおり、Spring と Hibernate で非常に基本的な構成を使用しています。
なぜ getSession.flush() を呼び出す必要があるのか を理解するのを手伝ってもらえますか? データベースにレコードが保存/更新されないのはなぜですか?