gpt4 book ai didi

java - Payara Micro忽略persistence.xml中的连接url

转载 作者:行者123 更新时间:2023-12-04 08:00:08 26 4
gpt4 key购买 nike

我正在编写一个 Payara Micro Web 应用程序,它应该连接到 persistence.xml 中定义的数据库.它应该连接的数据库是我本地机器上的一个文件,在项目启动时,它不存在。据我了解,H2 应该在第一次连接时为数据库创建一个文件。persistence.xml好像:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="com.danjbower_h2test_war_0.0.1PU"
transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

<class>com.danjbower.h2test.Test</class>

<properties>
<property name="hibernate.connection.driver_class" value="org.h2.Driver" />
<property name="hibernate.connection.url" value="jdbc:h2:file:C:/databases/testdb" />
<property name="hibernate.connection.username" value="sa" />
<property name="hibernate.connection.password" value="" />

<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false" />
<property name="hibernate.transaction.jta.platform" value="SunOne" />
</properties>
</persistence-unit>
</persistence>
当项目运行时,它会连接到数据库并运行 sql 命令来创建表并添加我想在启动 bean 中定义的特定实体。但是,当我检查我的系统时,它没有创建 C:\databases\testdb.mv.db文件正如我所料。出了什么问题,我怎样才能使它实际生成数据库文件而不是看似忽略配置?

MCVE 的附加文件
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.danjbower</groupId>
<artifactId>H2Test</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>

<name>H2Test</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
<version.payara>5.201</version.payara>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>fish.payara.api</groupId>
<artifactId>payara-bom</artifactId>
<version>${version.payara}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-web-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.enterprise.concurrent</groupId>
<artifactId>jakarta.enterprise.concurrent-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.resource</groupId>
<artifactId>jakarta.resource-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.batch</groupId>
<artifactId>jakarta.batch-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.microprofile</groupId>
<artifactId>microprofile</artifactId>
<scope>provided</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.25.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.4.25.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.2.Final</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>fish.payara.maven.plugins</groupId>
<artifactId>payara-micro-maven-plugin</artifactId>
<version>1.0.6</version>
<configuration>
<payaraVersion>${version.payara}</payaraVersion>
<deployWar>false</deployWar>
<commandLineOptions>
<option>
<key>--autoBindHttp</key>
</option>
<option>
<key>--deploy</key>
<value>${project.build.directory}/${project.build.finalName}</value>
</option>
</commandLineOptions>
<contextRoot>/H2Test</contextRoot>
</configuration>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>payara-patched-externals</id>
<name>Payara Patched Externals</name>
<url>https://raw.github.com/payara/Payara_PatchedProjects/master</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

</project>
测试.java
package com.danjbower.h2test;

import java.util.Objects;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@Table(name = "Tests")
@XmlRootElement
public class Test
{
@Id
@Column(name = "Id", unique = true)
@Basic(optional = false)
@NotNull
private Integer id = 0;

public Integer getId()
{
return id;
}

public void setId(Integer id)
{
this.id = id;
}

@Override
public int hashCode()
{
return id;
}

@Override
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}

if (obj == null)
{
return false;
}

if (getClass() != obj.getClass())
{
return false;
}

final Test other = (Test) obj;

return Objects.equals(id, other.id);
}

@Override
public String toString()
{
return "Test (Id: " + id + ")";
}
}
启动程序
package com.danjbower.h2test;

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Startup
@Singleton
public class StartUp
{
@PersistenceContext(unitName = "com.danjbower_h2test_war_0.0.1PU")
private EntityManager entityManager;

@PostConstruct
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void init()
{
Test test = new Test();
test.setId(4);
entityManager.persist(test);
}
}
显示它运行命令的日志
Hibernate: 

create table Tests (
Id integer not null,
primary key (Id)
)

Hibernate:
insert
into
Tests
(Id)
values
(?)


[2021-03-07T03:37:03.511+0000] [] [INFO] [] [PayaraMicro] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1615088223511] [levelValue: 800]
{
"Instance Configuration": {
"Host": "192.168.1.12",
"Http Port(s)": "8080",
"Https Port(s)": "",
"Instance Name": "Hilarious-Boxfish",
"Instance Group": "MicroShoal",
"Hazelcast Member UUID": "ae8dbc3a-b361-4dd6-bbba-762e620f96a8",
"Deployed": [
{
"Name": "H2Test-0.0.1",
"Type": "war",
"Context Root": "/H2Test"
}
]
}
}

[2021-03-07T03:37:03.513+0000] [] [INFO] [] [PayaraMicro] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1615088223513] [levelValue: 800]
Payara Micro URLs:
http://192.168.1.12:8080/H2Test

最佳答案

问题是您正在使用 transaction-type="JTA" ,这意味着数据源是从 Payara Micro 检索的,而不是由 Hibernate 根据您指定的属性创建的。
你应该换 transaction-type="JTA"transaction-type="RESOURCE_LOCAL" .那应该可以修复它并按您的预期运行。
现在发生的事情是 Hibernate 从 Payara Micro 请求数据源并忽略 hibernate.connection特性。您没有指定任何数据源 JNDI 名称(例如使用 hibernate.connection.datasource ),因此它将检索默认数据源。 Payara Micro 中的默认数据源用于存储在临时目录中的 H2 数据库。

关于java - Payara Micro忽略persistence.xml中的连接url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66513001/

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