gpt4 book ai didi

java - Maven 无法定位持久单元

转载 作者:行者123 更新时间:2023-12-02 04:06:24 30 4
gpt4 key购买 nike

嗨,我正在使用 mavenjpa 构建一个 java 应用程序。我有一个数据库项目,它是我的主应用程序的依赖项。数据库项目在开发模式下功能齐全,但是当我生成 jar 时,我收到错误无法找到持久性单元。我将 persistence.xml 文件放在 META-INF 文件夹中,该文件夹与我的 jar 文件位于同一文件夹中。我有以下 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="user-db">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.main.application.User</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.username" value="user" />
<property name="hibernate.connection.password" value="pwd" />
<property name="hibernate.connection.url" value="jdbc:mysql://127.0.0.1/users_db" />
</properties>
</persistence-unit>
</persistence>

堆栈跟踪:

> Exception in thread "main" java.lang.reflect.InvocationTargetException
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
> at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> at java.lang.reflect.Method.invoke(Method.java:497)
> at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
> Caused by: javax.persistence.PersistenceException: Unable to locate
> persistence units
> at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:84)
> at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:71)
> at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:52)
> at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
> at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
> at com.user.Dao.<init>(Dao.java:22)
> at com.user.Dao.get(Dao.java:26)
> at com.user.UserHandler.<init>(UserHandler.java:61)
> at com.user.Main.main(Main.java:10)
> ... 5 more Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range:
> -1
> at java.lang.String.substring(String.java:1967)
> at org.hibernate.boot.archive.internal.ArchiveHelper.getJarURLFromURLEntry(ArchiveHelper.java:45)
> at org.hibernate.jpa.boot.internal.PersistenceXmlParser.parsePersistenceXml(PersistenceXmlParser.java:105)
> at org.hibernate.jpa.boot.internal.PersistenceXmlParser.doResolve(PersistenceXmlParser.java:84)
> at org.hibernate.jpa.boot.internal.PersistenceXmlParser.locatePersistenceUnits(PersistenceXmlParser.java:66)
> at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:80)
> ... 13 more

最佳答案

我发现我导出项目的方式错误。我看到this线程,这是那里建议的,现在一切似乎都有效。

关于java - Maven 无法定位持久单元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34240126/

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