作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Spring 3.1.1.RELEASE、Hibernate 4.1.0.Final 和 JPA 2.0。有没有一种方法可以配置 Spring 事务在事务执行后提交,而无需 Java 代码?换句话说,我想将刷新模式设置为在应用程序上下文文件、hibernate 配置文件或 persistence.xml 文件中提交。我的 Spring 事务服务类看起来像
@Transactional(rollbackFor = Exception.class)
@Service
public class ContractServiceImpl implements ContractService
{
@Autowired
private ContractDAO m_contractDao;
public void addContract(Contract contract)
{
m_contractDao.create(contract);
}
...
我的应用程序上下文设置如下......
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:mem:myproject" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="persistenceXmlLocation" value="classpath*:META-INF/test-persistence.xml"/>
<property name="persistenceUnitName" value="testingDatabase"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="sharedEntityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven />
我的 persistence.xml 文件是
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="testingDatabase" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.ejb.cfgfile" value="/hsql_hibernate.cfg.xml" />
<property name="org.hibernate.FlushMode" value="commit" />
</properties>
</persistence-unit>
</persistence>
我的 hibernate 配置文件是
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.pool_size">1</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<mapping class="org.mainco.subco.sbadmin.domain.Product" />
<mapping class="org.mainco.subco.sbadmin.domain.Contract" />
<mapping class="org.mainco.subco.organization.domain.Country" />
<mapping class="org.mainco.subco.organization.domain.State" />
<mapping class="org.mainco.subco.organization.domain.Address" />
<mapping class="org.mainco.subco.organization.domain.OrganizationType" />
<mapping class="org.mainco.subco.organization.domain.Organization" />
</session-factory>
</hibernate-configuration>
最佳答案
在 web.xml 中尝试以下操作
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>flushMode</param-name>
<param-value>COMMIT</param-value>
</init-param>
</filter>
关于spring - 如何在配置文件中将刷新模式设置为 "COMMIT"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13259677/
我是一名优秀的程序员,十分优秀!