JPA 2.0、hibernate 3.5、jars 和persistence.xml 位置
我正在使用 hibernate 3.5 & 构建桌面应用程序JPA 2.0。
我有 2 个 jar,
lib,它定义了每个实体和 DAO,包如下所示:
org.my.package.models
org.my.package.models.dao
org.my.package.models.utils
在 org.my.package.utils 中,我定义了用于获取 EM 和 DAO 的 hibernate 实用程序类。 EMF 实例,这意味着库绑定到持久性单元名称,但这现在不是问题(无论如何你可以推荐我更好的方法来管理它)
第二个 jar 构建如下:
org.my.package.app
META -INF 是在项目的根目录上定义的,这意味着在我的 jar 中,我可以直接在根目录中找到此目录:
META-INF/
META-INF/persistence.xml
org/
org/my/
...
org/my/package/app/Main.class
当我运行应用程序时,hibernate 无法找到 persistence.xml 它会抛出一个异常,例如“package or未找到 PersistenceUnitName 的类”。
SLF4J: The requested version 1.5.11 by your slf4j binding is not compatible with [1.5.5, 1.5.6, 1.5.7, 1.5.8]
SLF4J: See http://www.slf4j.org/codes.html#version_mismatch for further details.
3 [main] INFO org.hibernate.cfg.annotations.Version - Hibernate Annotations 3.5.0-Final
25 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.5.0-Final
28 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
33 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
41 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
153 [main] INFO org.hibernate.annotations.common.Version - Hibernate Commons Annotations 3.2.0.Final
160 [main] INFO org.hibernate.ejb.Version - Hibernate EntityManager 3.5.0-Final
Exception in thread "main" java.lang.ExceptionInInitializerError
at Main.main(Main.java:171)
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: PMMPU] class or package not found
at org.hibernate.ejb.Ejb3Configuration.addNamedAnnotatedClasses(Ejb3Configuration.java:1316)
at org.hibernate.ejb.Ejb3Configuration.addClassesToSessionFactory(Ejb3Configuration.java:1094)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:981)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:275)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:359)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
at dil.tal.polymarmots.utils.HibernateUtil.getEmf(HibernateUtil.java:45)
at dil.tal.polymarmots.utils.HibernateUtil.getEm(HibernateUtil.java:54)
at dil.tal.polymarmots.utils.HibernateUtil.getMotDAOImpl(HibernateUtil.java:115)
at dil.tal.polymarmots.models.Mot.<clinit>(Mot.java:30)
... 1 more
Caused by: java.lang.ClassNotFoundException: model.Extrait
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:170)
at org.hibernate.ejb.Ejb3Configuration.classForName(Ejb3Configuration.java:1232)
at org.hibernate.ejb.Ejb3Configuration.addNamedAnnotatedClasses(Ejb3Configuration.java:1304)
... 11 more
我用谷歌搜索了一下问题 但我无法正确组织源代码。
有什么帮助吗?
I'm building a desktop application using hibernate 3.5 & JPA 2.0.
I have 2 jars,
the lib, which defines every entity and DAO, packages looks like this :
org.my.package.models
org.my.package.models.dao
org.my.package.models.utils
In org.my.package.utils I defined my hibernate utility class for getting EM & EMF instances, which means the lib is bound to a Persistence Unit name but that's not a problem for now (anyway you can recommend me a better way to manage that)
the second jar is built as follow:
org.my.package.app
META-INF is defined on the root of the project which means in my jar I can find this directories directly in the root:
META-INF/
META-INF/persistence.xml
org/
org/my/
...
org/my/package/app/Main.class
When I run the app, hibernate doesn't managed to find persistence.xml it throws an exception something like "package or class for PersistenceUnitName not found".
SLF4J: The requested version 1.5.11 by your slf4j binding is not compatible with [1.5.5, 1.5.6, 1.5.7, 1.5.8]
SLF4J: See http://www.slf4j.org/codes.html#version_mismatch for further details.
3 [main] INFO org.hibernate.cfg.annotations.Version - Hibernate Annotations 3.5.0-Final
25 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.5.0-Final
28 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
33 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
41 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
153 [main] INFO org.hibernate.annotations.common.Version - Hibernate Commons Annotations 3.2.0.Final
160 [main] INFO org.hibernate.ejb.Version - Hibernate EntityManager 3.5.0-Final
Exception in thread "main" java.lang.ExceptionInInitializerError
at Main.main(Main.java:171)
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: PMMPU] class or package not found
at org.hibernate.ejb.Ejb3Configuration.addNamedAnnotatedClasses(Ejb3Configuration.java:1316)
at org.hibernate.ejb.Ejb3Configuration.addClassesToSessionFactory(Ejb3Configuration.java:1094)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:981)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:275)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:359)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
at dil.tal.polymarmots.utils.HibernateUtil.getEmf(HibernateUtil.java:45)
at dil.tal.polymarmots.utils.HibernateUtil.getEm(HibernateUtil.java:54)
at dil.tal.polymarmots.utils.HibernateUtil.getMotDAOImpl(HibernateUtil.java:115)
at dil.tal.polymarmots.models.Mot.<clinit>(Mot.java:30)
... 1 more
Caused by: java.lang.ClassNotFoundException: model.Extrait
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:170)
at org.hibernate.ejb.Ejb3Configuration.classForName(Ejb3Configuration.java:1232)
at org.hibernate.ejb.Ejb3Configuration.addNamedAnnotatedClasses(Ejb3Configuration.java:1304)
... 11 more
I googled a bit about the problem but I can't get the source code organisation right.
Any help ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
未找到类或包消息是不言自明的:未找到类或包 - 不是
persistence.xml
- 正如异常原因所建议的:model.Extrait
实体并不反映您所显示的包装,但它很可能在您的persistence.xml
中声明(您没有显示),但没有出现在类路径上。The class or package not found message is self-explaining: a class or a package is not found - not the
persistence.xml
- as suggested by the cause of the exception:The
model.Extrait
entity doesn't reflect the packaging you're showing but it is very likely declared in yourpersistence.xml
(that you're not showing) but not present on the class path.