spring 如何在我的配置文件中将刷新模式设置为“COMMIT”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13259677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I set flush mode to "COMMIT" in my configuration files?
提问by Dave
I'm using Spring 3.1.1.RELEASE, Hibernate 4.1.0.Final, and JPA 2.0. Is there a way I can configure Spring transactions to commit after the transactions are executed without Java code? In other words, I would like to set flush mode to commit in either the application context file, hibernate configuration file, or persistence.xml file. My Spring transaction service class looks like
我使用的是 Spring 3.1.1.RELEASE、Hibernate 4.1.0.Final 和 JPA 2.0。在没有 Java 代码的情况下执行事务后,有没有办法配置 Spring 事务以提交?换句话说,我想将刷新模式设置为在应用程序上下文文件、休眠配置文件或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);
}
...
and my application context is set up like so …
我的应用程序上下文是这样设置的……
<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 />
My persistence.xml file is
我的 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>
and my hibernate config file is
我的休眠配置文件是
<?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>
采纳答案by blob
回答by bvulaj
Try the following in your web.xml
在您的 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>
参考。
回答by Steve Ebersole
As another option, you can configure the Hibernate EntityManager directly to use a particular flush mode by default using the org.hibernate.flushModeconfiguration setting.
作为另一种选择,您可以使用org.hibernate.flushMode配置设置直接将 Hibernate EntityManager 配置为默认使用特定的刷新模式。
回答by Jimmy Johnson
I am not sure if this type of setting is available in spring. (I haven't seen one) But, as an alternative hibernate provides generic CRUD methods that you can use for all your classes if you pass them in as generics. Just put the call to the flush method in the Update/Create methods and use these exclusively to create/update all your classes.
我不确定这种类型的设置在春季是否可用。(我没有见过)但是,作为替代,hibernate 提供了泛型 CRUD 方法,如果您将它们作为泛型传入,您可以将它们用于所有类。只需在 Update/Create 方法中调用flush 方法,并专门使用这些方法来创建/更新您的所有类。
Here is an example:
下面是一个例子:
http://www.ibm.com/developerworks/java/library/j-genericdao/index.html
http://www.ibm.com/developerworks/java/library/j-genericdao/index.html

