JPA 2.0,hibernate 3.5,jars& persistence.xml位置

时间:2010-04-18 15:10:05

标签: java hibernate orm jpa-2.0 persistence.xml

我正在使用hibernate 3.5&amp ;;构建桌面应用程序。 JPA 2.0。

我有2个罐子,

定义每个实体和DAO的lib,包如下所示:

org.my.package.models
org.my.package.models.dao
org.my.package.models.utils

在org.my.package.utils中我定义了我的hibernate实用程序类来获取EM& EMF实例,这意味着lib绑定到持久性单元名称,但现在不是问题(无论如何,你可以建议我更好的方法来管理它)

第二个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,它会抛出异常,例如“找不到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

我搜索了一下the problem,但我无法使源代码组织正确。

任何帮助?

1 个答案:

答案 0 :(得分:9)

未找到类或包消息是自我解释的:找不到类或包 - 而不是persistence.xml - 正如异常原因所暗示的那样:

Caused by: java.lang.ClassNotFoundException: model.Extrait

model.Extrait实体未反映您正在展示的包装,但很可能在您的persistence.xml(您未展示)中声明但在课程路径中不存在。< / p>