Persistence Unit Scope

Persistence Unit Scope
An EJB-JAR, WAR, application client jar, or EAR can define a persistence unit.
When referencing a persistence unit using the unitName annotation element or persistence-
unit-name deployment descriptor element, the visibility scope of the persistence unit is
determined by its point of definition. A persistence unit that is defined at the level of an EJB-JAR,WAR,
or application client jar is scoped to that EJB-JAR, WAR, or application jar respectively and is visible to
the components defined in that jar or war. A persistence unit that is defined at the level of the EAR is
generally visible to all components in the application.
However, if a persistence unit of the same name is defined by an EJB-JAR, WAR, or application jar file
within the EAR, the persistence unit of that name defined at EAR level will not be visible to the components
defined by that EJB-JAR, WAR, or application jar file unless the persistence unit reference uses
the persistence unit name # syntax to specify a path name to disambiguate the reference. When the #
syntax is used, the path name is relative to the referencing application component jar file. For example,
the syntax ../lib/persistenceUnitRoot.jar#myPersistenceUnit refers to a persistence
unit whose name, as specified in the name element of the persistence.xml file, is myPersistenceUnit
and for which the relative path name of the root of the persistence unit is
../lib/persistenceUnitRoot.jar. The # syntax may be used with both the unitName
annotation element or persistence-unit-name deployment descriptor element to reference a
persistence unit defined at EAR level.

persistence.xml和类装载

部署时报这个错误:

java.lang.IllegalStateException: PersistenceProvider [oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider@1927691] did not return an EntityManagerFactory for name ‘mypu’

通常这样的问题是由于没有找到META-INF/persistence.xml或者是名字写错了。但是这里的配置并没有错,而且使用本地eclipse环境同样适用oc4j也是可以部署成功的。尝试把persistence.xml放到war把下的meta-inf目录,不成功。(原本这个文件是放到web-inf/lib下的一个jar里)
后来发现将war打包到ear再去发布就成功了。由此发现了oc4j对于war和ear不同的类装载策略。
关于jee的类装载层次可以看这篇文章

-还有一种可能是toplink-essential的版本问题,这个是好使的:2.0-b41-beta2 (03/30/2007)

—-这个问题解决了.原因是使用的应用服务器oc4j在部署是会记住上一次部署时的war文件名(它会根据这个文件名创建一个临时文件夹),而在下次部署时,如果war的文件名变掉,则会还去找原来的那个文件夹!!所以部署前要把oc4j缓存清除并重启!