我正在Wildfly 15.0.1上运行Java EE应用程序。我试图使用JPA而不在Wildfly上创建数据源,因为我希望所有依赖项都打包在应用程序源代码中。我正在使用Maven构建WAR。
我正在使用PostgreSQL,并且它已在http://localhost:5432上启动并运行,我为此项目创建了一个名为test的数据库。
因此,我将此依赖项添加到了pom.xml:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.5</version>
</dependency>
我的persistence.xml位于src/main/resources/META-INF/persistence.xml
中:
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.2"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
<persistence-unit name="pu1">
<!-- classes -->
<class>myapp.model.Address</class>
<class>myapp.model.Transaction</class>
<properties>
<!-- database connection -->
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/testing" />
<property name="javax.persistence.jdbc.user" value="postgres" />
<property name="javax.persistence.jdbc.password" value="postgres" />
<!-- hibernate -->
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
我的Address和Transaction类上都有 @Entity 批注。但是,什么都没有发生。这是日志片段:
22:58:05,759 INFO [org.hibernate.jpa.internal.util.LogHelper] (ServerService Thread Pool -- 103) HHH000204: Processing PersistenceUnitInfo [
name: pu1
...]
22:58:05,760 INFO [org.jboss.weld.deployer] (MSC service thread 1-5) WFLYWELD0003: Processing weld deployment myapp.backend-1.0.war
22:58:05,791 WARN [org.jboss.as.jaxrs] (MSC service thread 1-3) WFLYRS0018: Explicit usage of Jackson annotation in a JAX-RS deployment; the system will disable JSON-B processing for the current deployment. Consider setting the 'resteasy.preferJacksonOverJsonB' property to 'false' to restore JSON-B.
22:58:05,796 INFO [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-3) WFLYJCA0005: Deploying non-JDBC-compliant driver class org.postgresql.Driver (version 42.2)
22:58:05,810 INFO [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-8) WFLYJCA0018: Started Driver service with driver-name = myapp.backend-1.0.war_org.postgresql.Driver_42_2
22:58:05,816 INFO [org.jboss.as.jpa] (ServerService Thread Pool -- 103) WFLYJPA0010: Starting Persistence Unit (phase 2 of 2) Service 'myapp.backend-1.0.war#pu1'
22:58:05,817 INFO [org.hibernate.dialect.Dialect] (ServerService Thread Pool -- 103) HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL94Dialect
22:58:05,820 INFO [org.hibernate.type.BasicTypeRegistry] (ServerService Thread Pool -- 103) HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType@6543510a
22:58:05,822 INFO [org.hibernate.envers.boot.internal.EnversServiceImpl] (ServerService Thread Pool -- 103) Envers integration enabled? : true
22:58:05,858 INFO [org.hibernate.tool.schema.internal.SchemaCreatorImpl] (ServerService Thread Pool -- 103) HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@4cc01a34'
22:58:05,858 INFO [org.hibernate.hql.internal.QueryTranslatorFactoryInitiator] (ServerService Thread Pool -- 103) HHH000397: Using ASTQueryTranslatorFactory
答案 0 :(得分:1)
JPA规范:
8.2.1.2交易类型
transaction-type属性用于指定实体是否 实体经理工厂为持久性提供的经理 单位必须是JTA实体管理器或资源本地实体管理器。 该元素的值为JTA或RESOURCE_LOCAL。交易类型 JTA假设将提供JTA数据源- 由jta-data-source元素指定或由容器提供。 通常,在Java EE环境中,事务类型为 RESOURCE_LOCAL假定将提供非JTA数据源。在 Java EE环境,如果未指定此元素,则默认 是JTA。在Java SE环境中,如果未指定此元素, 默认值为RESOURCE_LOCAL。
JTA是EE应用程序服务器中的默认事务类型。要使用它,您需要在WildFly中配置的数据源。然后将其添加到persistence.xml:
<persistence-unit name="pu1" transaction-type="JTA">
<!-- classes -->
<class>myapp.model.Address</class>
<class>myapp.model.Transaction</class>
<properties>
<jta-data-source>jdbc/jndi_name_of_datasource</jta-data-source>
<!-- hibernate -->
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>