2014-07-16 28 views
7

我試過幾種方法將FlushMode更改爲完整的應用程序。 這是對的還是有另一種方法來做到這一點?JPA將默認Flushmode從AUTO更改爲COMMIT

我不想這樣做的語用。

這是我發現的財產,但它不工作。

<persistence 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" 
version="2.0"> 
    <persistence-unit name="myPU"> 
     <properties> 
      ... 
      <property name="hibernate.connection.autocommit" value="false"/> 
      <!-- Also tried this: --> 

      <property name="org.hibernate.FlushMode" value="commit"/> 
      ... 
     </properties> 
    </persistence-unit> 
</persistence> 

更新:

我已經在通過zxcf鏈接描述創建的類,但我無法弄清楚如何在我的persistence.xml中添加此結構。

<property name="jpaDialect"> 
    <bean class="test.jpa.vendor.HibernateJpaDialect"> 
     <property name="flushMode" value="MANUAL"/> 
    </bean> 
</property> 

回答

6

試試這個

<property name="org.hibernate.flushMode" value="COMMIT"/> 

測試這對一個獨立的程序,我可以看到一個Hibernate Session/EntityManager的底層的更改值從AUTOCOMMIT

這裏是我的persistence.xml

<?xml version="1.0" encoding="UTF-8"?> 
<persistence 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" 
    version="2.0"> 
    <persistence-unit name="JPATest" transaction-type="RESOURCE_LOCAL"> 
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 

    <class>com.test.TestEntity</class> 
    <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode> 
    <properties> 
     <property name="hibernate.hbm2ddl.auto" value="update"/> 
     <property name="hibernate.show_sql" value="true"/> 
     <property name="hibernate.format_sql" value="true"/> 
     <property name="hibernate.use_sql_comments" value="true"/> 
     <property name="hibernate.cache.use_query_cache" value="true"/> 
     <property name="hibernate.archive.autodetection" value="class, hbm"/>  
     <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/> 
     <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> 
     <property name="hibernate.connection.username" value="xxx"/> 
     <property name="hibernate.connection.password" value="xxx"/> 
     <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test?createDatabaseIfNotExist=true"/>  
     <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"/> 
     <property name="hibernate.cache.provider_configuration" value="classpath:ehcache.xml"></property> 
     <property name="hibernate.use.second.level.cache" value="true"/> 
     <property name="hibernate.cache.region_prefix" value="neutrino.jpa.cache"/> 
     <property name="hibernate.cache.use_query_cache" value="false"/> 
     <property name="hibernate.generate_statistics" value="true"/> 
     <property name="hibernate.jdbc.batch_size" value="10"/> 
     <property name="hibernate.order_updates" value="true"/> 
     <property name="hibernate.order_inserts" value="true"/> 
    <property name="org.hibernate.flushMode" value="COMMIT"/>  
    </persistence-unit> 
</persistence> 

這裏是如何我測試了

EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPATest"); 
EntityManager em = emf.createEntityManager(); 
Session session = em.unwrap(Session.class); 
System.out.println("Underlying Hibernate session flushmode #######   "+session.getFlushMode()); 
System.out.println("EntityManager flushmode    #######   "+em.getFlushMode()); 

這給了我

Underlying Hibernate session flushmode #######   COMMIT 
EntityManager flushmode    #######   COMMIT 

如果我省略了財產presistence.xml,我得到這個

Underlying Hibernate session flushmode #######   AUTO 
EntityManager flushmode    #######   AUTO 
+0

這只是資本化發行與價值? – wrschneider

+0

看起來就是這樣!請在你的最後測試,不要忘記把這個問題標記爲回答:-) – Shailendra

+0

@Sheilendra這不行。 –

相關問題