gpt4 book ai didi

java - 如何在可执行 jar 文件中使用 JPA?

转载 作者:行者123 更新时间:2023-12-01 04:21:40 25 4
gpt4 key购买 nike

我有这样的结构:

  • xptoapp
    • xptoapp-main
    • xptoapp-core
    • xptoapp-domain

包“xptoapp-main”有一个像这样的pom.xml:

<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org /xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org /2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.xpto</groupId>
<artifactId>xptoapp</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>xptoapp-main</artifactId>
<name>xptoapp-main</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.xpto</groupId>
<artifactId>xptoapp-domain</artifactId>
<version>${version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.xpto</groupId>
<artifactId>xptoapp-core</artifactId>
<version>${version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>


在项目“xptoapp-domain”内,我有一个 META-INF/persistence.xml。我应该将 persistence.xml 放入项目“xptoapp-app”(文件夹 META-INF )中吗?

当我尝试执行“java -jar xpto-app.jar”时,出现异常:

Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: javax.persistence.PersistenceException: Unable to configure EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:385)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at com.xpto.domain.persistence.PersistenceManager.<init>(PersistenceManager.java:14)
at com.xpto.app.App.main(App.java:30)
... 5 more
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(Unknown Source)
at org.hibernate.ejb.packaging.JarVisitorFactory.getJarURLFromURLEntry(JarVisitorFactory.java:57)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:351)
... 10 more



在我的情况下如何使用 JPA?为什么我会收到此异常?

持久性.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="xptoapp" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.xpto.domain.entity.Account</class>
<class>com.xpto.domain.entity.Transfer</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:xptoapp" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="false" />
</properties>
</persistence-unit>
</persistence>

最佳答案

已修复。问题出在 eclipse 导出我的 jar 的那一刻。它将文件夹 META-INF 放入名为“resources”的文件夹中。

谢谢大家。

关于java - 如何在可执行 jar 文件中使用 JPA?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18835643/

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