Collections are not persisted, because Spring-Webapp set's Hibernate-Flush-Mode to Manual
My Spring-3.0.5/Hibernate-3.3.0 webapp does not store collections. When creating a new persitent object with an asscociated collection, the object is persited, but the collection not. In a test-case all works well, so it's not a faulty Mapping-Annotation.
When I'm running the test-case and the webapp with log-level TRACE, the test-case yields something like (grep for "[fF]lush":
AbstractFlushingEventListener - flushing session
AbstractFlushingEventListener - processing flush-time cascades
AbstractFlushingEventListener - dirty checking collections
AbstractFlushingEventListener - Flushing entities and processing referenced collections
AbstractFlushingEventListener - Processing unreferenced collections
AbstractFlushingEventListener - Scheduling collection removes/(re)creates/updates
AbstractFlushingEventListener - Flushed: 0 insertions, 0 updates, 0 deletions to 4 objects
AbstractFlushingEventListener - Flushed: 2 (re)creations, 0 updates, 0 removals to 开发者_JAVA技巧3 collections
but the webapp only says:
org.hibernate.impl.SessionImpl - setting flush mode to: MANUAL
When I grab vor "SessionImpl" I get
DEBUG: org.hibernate.impl.SessionImpl - opened session at timestamp: 13071848978
DEBUG: org.springframework.orm.hibernate3.HibernateTransactionManager - Opened new Session [org.hibernate.impl.SessionImpl@acaf083] for Hibernate transaction
DEBUG: org.springframework.orm.hibernate3.HibernateTransactionManager - Preparing JDBC Connection of Hibernate Session [org.hibernate.impl.SessionImpl@acaf083]
TRACE: org.hibernate.impl.SessionImpl - setting cache mode to: GET
TRACE: org.hibernate.impl.SessionImpl - setting cache mode to: NORMAL
TRACE: org.hibernate.impl.SessionImpl - setting cache mode to: GET
TRACE: org.hibernate.impl.SessionImpl - setting cache mode to: NORMAL
TRACE: org.hibernate.impl.SessionImpl - after transaction completion
TRACE: org.hibernate.impl.SessionImpl - closing session
for the test-case and
DEBUG: org.hibernate.impl.SessionImpl - opened session at timestamp: 13071842364
TRACE: org.hibernate.impl.SessionImpl - setting flush mode to: MANUAL
TRACE: org.hibernate.impl.SessionImpl - closing session
for the webapp.
I have no clue, why the webapp always disables automatic flushing!
Here is my Webapp-Configuration (shortend for clearity):
<tx:annotation-driven proxy-target-class="true"/>
<context:spring-configured/>
<context:component-scan base-package="de.halbekunst.fotos" />
<mvc:annotation-driven />
and my resources-definitions:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="jdbcUrl">
<value><![CDATA[${jdbc.url}]]></value>
</property>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="minPoolSize" value="3"/>
<property name="maxPoolSize" value="30"/>
<property name="acquireIncrement" value="3"/>
<property name="maxIdleTime" value="25200"/>
<property name="maxIdleTimeExcessConnections" value="14400"/>
<property name="idleConnectionTestPeriod" value="7200"/>
<property name="maxStatements" value="50"/>
<property name="preferredTestQuery" value="SELECT 1;"/>
</bean>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="de.halbekunst.fotos"/>
<property name="hibernateProperties">
<value>
hibernate.dialect=${hibernate.dialect}
hibernate.query.substitutions=true 'Y', false 'N'
hibernate.cache.use_second_level_cache=false
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
</value>
<!-- Turn batching off for better error messages under PostgreSQL -->
<!-- hibernate.jdbc.batch_size=0 -->
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
After one week of debugging (I striped down my original project until I was able to identify the source of the described error) I now know what went wrong:
The exposed method of the faulty controller was not public and because of that, the annotation had no effect.
For anyone, who has similar problems: here is the sriped down version of my project, that demonstrates the error:
https://github.com/kai-moritz/experiment
You need Maven! To run the example, just execute the two commands
mvn war:inpace
mvn jetty:run
精彩评论