The problem is not really related to Hibernate, but there is a workaround that the Hibernate developers may choose to implement.
When using a package annotation in hibernate you end up specifying the package in the persistence.xml file with a Hibernate-specific extension:
<class>path.to.my.package</class>
Note that this references a package and not an annotated Class. This entry will cause Hibernate to eventually call Package.getPackage("path.to.my.package"). The JavaDoc states that the return value can be null "..if no package information is available from the archive or codebase."
Apparently, the return value can also be null simply because the ClassLoaders don't feel like looking to hard for the information. The author of this article (
http://www.javaworld.com/javaworld/jw-0 ... -jpvs.html) refers to this behavior as an optimization. I think this is more likely a JDK bug, but I can't find any open bugs for it.
Here is what I think may be a workaround for the problem. Since the EntityManager is looking for a specific class in this package, a call to:
Code:
Class.forName("path.to.my.package.package-info")
causes the Package.getPackage(name) call to succeed.
Hibernate version: Hibernate EntityManager 3.1beta4
Using as a stand-alone container
Mapping documents:Using annotations
Full stack trace of any exception that occurs:java.lang.IllegalArgumentException: class or package not found
at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:149)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:73)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:37)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:27)
at TestConnection.main(TestConnection.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)
Caused by: java.lang.IllegalArgumentException: class or package not found
at org.hibernate.ejb.Ejb3Configuration.addNamedAnnotatedClasses(Ejb3Configuration.java:558)
at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:387)
at org.hibernate.ejb.Ejb3Configuration.createFactory(Ejb3Configuration.java:96)
at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:142)
... 9 more
Caused by: java.lang.ClassNotFoundException: us.oh.state.dot.hibernate
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:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:164)
at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:108)
at org.hibernate.ejb.Ejb3Configuration.addNamedAnnotatedClasses(Ejb3Configuration.java:552)
... 12 more
Process finished with exit code 1
[/code]