如何解决当 autoCommit 为 false 时,EclipseLink v2.7.9 不会在 Postgres v13.3 中生成表
EclipseLink - v2.7.9
Postgres - v13.3
Postgres JDBC Client - v42.2.22
HikariCP - v4.0.3
JDK - v11.0.11
OS - macOS
我已将 autoCommit 设置为 false 并使用了 EclipseLink 属性 <property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
,但这些表并未在 Postgres 中生成,而这些表是在 MySQL 中使用相同设置创建的。
这仅在我使用数据源(例如 HikariDataSource)或 org.postgresql.ds.PGSimpleDataSource
的自定义扩展并将 autoCommit 设为 false 时发生。
扩展 PGSimpleDataSource 以将 autoCommit 设置为 false。
public class MyPGSimpleDataSource extends PGSimpleDataSource {
@Override
public Connection getConnection() throws SQLException {
Connection connection = super.getConnection();
connection.setAutoCommit(false);
return connection;
}
}
但是当我将 autoCommit 设置为 true 时,表格在 Postgres 中成功生成。
但是,EclipseLink 在 MySQL v8.0.25 中生成表,无论 autoCommit 是 true 还是 false。
我还尝试使用 Hibernate v5.5.2 复制该问题,发现即使 autoCommit 为 false 也会生成表。
我正在持久性属性映射中传递数据源,并且在 EclipseLink 属性下方添加了急切地创建 EntityManagerFactory 的属性。
<property name="eclipselink.deploy-on-startup" value="true"/>
用于测试此用例的简单数据源
PGSimpleDataSource ds = new MyPGSimpleDataSource();
ds.setServerNames(null);
ds.setPortNumbers(null);
ds.setUser("postgres");
ds.setPassword("******);
ds.setDatabaseName("JPA");
Map<String,Object> properties = new HashMap<>();
properties.put("javax.persistence.nonJtaDataSource",ds);
EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("AdeptJ_PU_Postgres",properties);
用于 EclipseLink 的persistence.xml
<persistence-unit name="AdeptJ_PU_Postgres" transaction-type="RESOURCE_LOCAL">
<!-- JPA Entities -->
<class>com.adeptj.modules.data.jpa.entity.User</class>
<class>com.adeptj.modules.data.jpa.entity.Address</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<shared-cache-mode>NONE</shared-cache-mode>
<properties>
<property name="eclipselink.deploy-on-startup" value="true"/>
<property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
<property name="eclipselink.ddl-generation.output-mode" value="database"/>
<property name="eclipselink.logging.file" value="jpa.log"/>
<property name="eclipselink.logging.level" value="FINEST"/>
<property name="eclipselink.exception-handler" value="com.adeptj.modules.data.jpa.JpaExceptionHandler"/>
</properties>
</persistence-unit>
使用 Hibernate 进行测试的最小persistence.xml
<persistence-unit name="AdeptJ_PU_Postgres" transaction-type="RESOURCE_LOCAL">
<!-- JPA Entities -->
<class>com.adeptj.modules.data.jpa.entity.User</class>
<class>com.adeptj.modules.data.jpa.entity.Address</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<shared-cache-mode>NONE</shared-cache-mode>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
如果需要更多信息,请告诉我。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。