gpt4 book ai didi

JPA将默认的刷新模式从AUTO更改为COMMIT

转载 作者:行者123 更新时间:2023-12-03 19:38:10 24 4
gpt4 key购买 nike

我尝试了几种将FlushMode更改为完整应用程序的方法。
这是对的还是还有另一种方法呢?

我不想务实地做到这一点。

这是我发现的属性(property),但它不起作用。

<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>

最佳答案

试试这个

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

在独立程序上进行测试,我可以看到基础Hibernate Session/EntityManager的值从 AUTO更改为 COMMIT
这是我的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

关于JPA将默认的刷新模式从AUTO更改为COMMIT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24778966/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com