出于某种原因,除非我明确地刷新(),否则我无法通过休眠来保存我的对象。
我正在使用Spring MVC
执行保存的部分DAO
public final T saveOrUpdate(final T instance) {
context.currentSession().saveOrUpdate(instance);
context.currentSession().flush(); //TODO should not have to do this
return instance;
}
web.xml文件的一部分,允许通过AJAX查看视图
<filter>
<filter-name>Open Session In View Filter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Open Session In View Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
用于交易管理的弹簧配置的一部分
<context:property-placeholder location="classpath:environment.properties" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${hibernate.connection.driver_class}" />
<property name="jdbcUrl" value="${hibernate.connection.url}" />
<property name="user" value="${hibernate.connection.username}" />
<property name="password" value="${hibernate.connection.password}" />
<property name="initialPoolSize" value="5" />
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="25" />
<property name="acquireIncrement" value="5" />
<property name="maxIdleTime" value="1800" />
<property name="numHelperThreads" value="5" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="entityInterceptor">
<bean class="org.mycompany.persistence.AuditTrailInterceptor"/>
</property>
<property name="hibernateProperties">
<props>
<!-- Hibernate Tweak to enhance performance -->
<prop key="hibernate.order_inserts">true</prop>
<!-- Hibernate Tweak to enhance performance -->
<prop key="hibernate.order_updates">true</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<!-- Enable mapping of annotated hibernate classes -->
<property name="packagesToScan" value="org.mycompany" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
示例服务方法
@Service
@Transactional
class MyServiceImpl implements MyService {
...
@Override
public final void save(final MyObject obj) {
myObjectDao.save(obj);
}
答案 0 :(得分:0)
结果是我的root-context.xml和servlet-context.xml之间的配置问题,我必须执行以下操作:
我必须将以下内容放在root-context.xml中:
<!-- Load everything except @Controllers -->
<context:component-scan base-package="my.package">
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
并在servlet-context.xml中:
<!-- Search this package for annotated Spring Beans -->
<!-- Load @Controllers only -->
<context:component-scan base-package="my.package" use-default-filters="false">
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
请注意,use-default-filters="false"
很重要,而且我最初遇到了很多麻烦,似乎servlet正在从root-context.xml覆盖bean