组装 jar 后 - 没有名为 EntityManager 的持久性提供程序
我正在开发一个独立的应用程序,从我的 ide(intellij idea) 启动它时它工作正常,但是在创建 uberjar 并从中启动应用程序后,抛出 javax.persistence.spi.PersistenceProvider 并提示“没有名为 testPU 的 EntityManager 的持久性提供程序”
这是我的 persistence.xml,它位于 meta-inf 目录下:
org.hibernate.ejb.HibernatePersistence test.model.Configuration <属性> <属性名称=“hibernate.connection.username”值=“root”/> <属性名称=“hibernate.connection.driver_class”值=“com.mysql.jdbc.Driver”/> <属性名称=“hibernate.connection.password”值=“root”/> <属性名称=“hibernate.connection.url”值=“jdbc:mysql://localhost:3306/test”/> <属性名称=“hibernate.show_sql”值=“true”/> <属性名称=“hibernate.dialect”值=“org.hibernate.dialect.MySQLInnoDBDialect”/> <属性名称=“hibernate.c3p0.timeout”值=“300”/> <属性名称=“hibernate.hbm2ddl.auto”值=“更新”/>
这是我创建实体管理器工厂的方法:
emf = Persistence.createEntityManagerFactory("testPU");
我使用maven并尝试了默认配置的组装插件,我没有太多组装jar的经验,我不知道我是否遗漏了一些东西,所以如果你有任何想法,我很高兴听到他们
im developing a standalone application and it works fine when starting it from my ide(intellij idea), but after creating an uberjar and start the application from it javax.persistence.spi.PersistenceProvider is thrown saying "No Persistence provider for EntityManager named testPU"
here is my persistence.xml which is placed under meta-inf directory:
<persistence-unit name="testPU" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <class>test.model.Configuration</class> <properties> <property name="hibernate.connection.username" value="root"/> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> <property name="hibernate.connection.password" value="root"/> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/> <property name="hibernate.c3p0.timeout" value="300"/> <property name="hibernate.hbm2ddl.auto" value="update"/> </properties> </persistence-unit>
and here is how im creating the entity manager factory:
emf = Persistence.createEntityManagerFactory("testPU");
im using maven and tried the assembly plug-in with the default configuration fot it, i dont have much experience with assembling jars and i dont know if im missing something, so if u have any ideas ill be glad to hear them
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的库可能有问题。请尝试执行以下操作。
清单版本:1.0
Ant 版本:Apache Ant 1.7.0
创建者:1.6.0_03-b05(Sun Microsystems Inc.)
主要类别:
类路径:lib/.jar lib/.jar lib/.jar ...
You are probably having problems with your libraries.. Try doing below.
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.0
Created-By: 1.6.0_03-b05 (Sun Microsystems Inc.)
Main-Class:
Class-Path: lib/.jar lib/.jar lib/.jar ...
我记得有过这个问题,我认为问题是你不能引用罐子里的罐子。对于您的外部库,它们需要在您的 jar 内扩展或位于系统 CLASSPATH 的其他位置。既然你没有具体说明你在罐子里放了什么,我就赌这个。
如果使用 -jar 参数运行 jar,则不能使用 -cp 命令行参数。一个或另一个。
I remember having that issue, and I think the problem was that you can't reference a jar in a jar. For your external libraries, they need to be expanded inside your jar or live elsewhere on the system CLASSPATH. So since you don't specify what all you're putting in your jar, I'm betting on this one.
You can NOT use the -cp command line parameter if you run your jar using the -jar parameter. One or the other.
当使用带有预定义
jar-with-dependencies
描述符,您将获得一个 jar 存档,其中包含项目的二进制输出及其解压的依赖项。因此,我能想到的一个可能的问题是带有persistence.xml
的多个 JAR,在这种情况下,我不确定您将在最终的程序集中得到哪一个。由于您收到一条错误消息,抱怨“没有名为 testPU 的 EntityManager 的持久性提供程序”,我将打开 megajar 并:
persistence.xml
是否存在(它应该)testPU
When using the Maven Assembly Plugin with the predefined
jar-with-dependencies
descriptor, you get a jar archive which contains the binary output of your project, along its the unpacked dependencies. So one possible problem I can think of would be multiple JARs withpersistence.xml
, in which case I'm not sure which one you'll get in the final assembly.Since you get an error message complaining about "No Persistence provider for EntityManager named testPU", I would open the megajar and:
persistence.xml
is present (it should)testPU