| Author |
EJB can't find Optional Package and thus Persistence Unit in it
|
Amir Keibi
Greenhorn
Joined: Apr 23, 2012
Posts: 25
|
|
Environment
WebLogic 11g (consequently JEE 1.6 and EJB 3.0)An EJB contained JAR that also holds all persistence Entities. It worked fine (have a web app that uses the EJB and it works as expected). I decided to separate Entities to another layer and to do this I created another module and deploy that as an Optional Package. So now I have two JAR files which, by the way, I deploy separately and not as part of one application; persistence JAR and EJB JAR.
Problem
During the deployment of EJB I get the following error (which tells me that the Optional Package wasn't found):
Unable to deploy EJB: StocksBean from BLayer-1.0.0-SNAPSHOT.jar: No persistence unit named 'internalAppsPU' is available in scope BLayer-1.0.0-SNAPSHOT.jar. Available persistence units: []
More Info
Manifest of persistence module's JAR
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: bm03043
Build-Jdk: 1.6.0_18
Extension-Name: PersistenceLayer
Implementation-Version: 1.0.0-SNAPSHOT
Specification-Version: 1.6
Persistence.XML in META-INF folder of persistence module's JAR
EJB's code
Manifest of EJB's JAR
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: bm03043
Build-Jdk: 1.6.0_18
Extension-List: PLayer
PLayer-Extension-Name: PersistenceLayer
PLayer-Implementation-Version: 1.0.0-SNAPSHOT
PLayer-Specification-Version: 1.6
Please take note the following:
The InternalAppsDS data source is configured previously in WebLogic.At some point I was under the impression that the names in Extension-List have to match the name of the JAR file deployed as Optional Package. But as I read more I realized that's not correct.
|
 |
James Boswell
Ranch Hand
Joined: Nov 09, 2011
Posts: 657
|
|
Persistence.XML in META-INF folder
The filename should be persistence.xml, not Persistence.XML.
|
 |
Amir Keibi
Greenhorn
Joined: Apr 23, 2012
Posts: 25
|
|
|
Thanks James. But that's not it. That was just my mistake typing it Persistence.xml.
|
 |
James Boswell
Ranch Hand
Joined: Nov 09, 2011
Posts: 657
|
|
Amir
Is the persistence layer jar contained within the lib folder of your EJB jar?
|
 |
Amir Keibi
Greenhorn
Joined: Apr 23, 2012
Posts: 25
|
|
|
No. As explained, it's deployed separately as an Optional Package. The reason is that I would be using my Entities in Web Layer as well (not in the capacity of Persistence but as POJOs).
|
 |
James Boswell
Ranch Hand
Joined: Nov 09, 2011
Posts: 657
|
|
Amir
I know you are using version 1.0 but I've been reading JPA 2.0 spec and I'm not sure if what you looking to achieve can be done. I have copied in the relevant sections below:
8.2 Persistence Unit Packaging
...
A persistence unit is defined by a persistence.xml file. The jar file or directory whoseMETA-INF directory contains thepersistence.xml file is termed the root of the persistence unit. In Java EE environments, the root of a persistence unit must be one of the following:
an EJB-JAR file the WEB-INF/classes directory of a WAR file[80] a jar file in the WEB-INF/lib directory of a WAR file a jar file in the EAR library directory an application client jar file
It is not required that an EJB-JAR or WAR file containing a persistence unit be packaged in an EAR unless the persistence unit contains persistence classes in addition to those contained within the EJB-JAR or WAR. See Section 8.2.1.6.
NOTE: Java Persistence 1.0 supported use of a jar file in the root of the EAR as the root of a persistence unit. This use is no longer supported. Portable applications should use the EAR library directory for this case instead. See [9].
A persistence unit must have a name. Only one persistence unit of any given name must be defined within a single EJB-JAR file, within a single WAR file, within a single application client jar, or within an EAR. See Section 8.2.2, “Persistence Unit Scope”.
Thepersistence.xml file may be used to designate more than one persistence unit within the same scope.
All persistence classes defined at the level of the Java EE EAR must be accessible to all other Java EE components in the application - i.e. loaded by the application classloader -such that if the same entity class is referenced by two different Java EE components (which may be using different persistence units), the referenced class is the same identical class.
8.2.2 Persistence Unit Scope
An EJB-JAR, WAR, application client jar, or EAR can define a persistence unit.
When referencing a persistence unit using theunitName annotation element orpersistence-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#myPe rsistenceUnit refers to a persistence unit whose name, as specified in the name element of thepersistence.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.
|
 |
Amir Keibi
Greenhorn
Joined: Apr 23, 2012
Posts: 25
|
|
Thanks James. If I understand correctly, the suggestion is to bundle the persistence layer's JAR with EJB's JAR in one application's EAR file and deploy together while making sure persistence.xml is at the EAR level. If so, then I'm gonna try the following and see how it goes:
- Keep my Entities in the Optional Package and deploy as I do now. Cause those Entities travel throughout layers of the application and are shared between EJBs and Web layer.
- Deploy the persistence.xml with EJB's jar cause that's the only place that uses it.
|
 |
Amir Keibi
Greenhorn
Joined: Apr 23, 2012
Posts: 25
|
|
And we have a liftoff! Thanks James. The document snippet was most useful.
Little problem though. The web layer works because the persistence layer's JAR file is included in the WAR file's lib folder. If I take it out, the web app won't be deployed. It'll be looking for the com.xx.persistence.Stock entity which means it didn't find the Optional Package. Even though I have the following in Manifest of the WAR file:
Manifest-Version: 1.0
Built-By: bm03043
Build-Jdk: 1.6.0_18
Created-By: Apache Maven 3.0.4
Archiver-Version: Plexus Archiver
Extension-List: persistence
persistence-Extension-Name: PersistenceLayer
persistence-Specification-Version: 1.6
persistence-Implementation-Version: 1.0.0-SNAPSHOT
|
 |
James Boswell
Ranch Hand
Joined: Nov 09, 2011
Posts: 657
|
|
Amir
Glad to hear the specification listing was useful.
I'm intrigued to know why your web layer has a dependency on a JPA entity class?
|
 |
Amir Keibi
Greenhorn
Joined: Apr 23, 2012
Posts: 25
|
|
Well, my Entities travel throughout layers of the application. Of course in web layer they are only a bunch of POJOs that hold the data that is coming or going to DB.
Anyway, managed to solve the issue altogether.
For the sake of others who might come across this, I ended up with the following layers:
Persistence Layer in which I have all Entities.
Business Logic Interface Layer which contains interfaces (of my Enterprise Components aka EJBs).
Business Logic Layer which holds the implementation of my Enterprise Components and persistence.xml and has dependency references to Persistence Layer and Business Logic Interface Layer.
Web Layer that has dependency references to Persistence Layer and Business Logic Interface Layer.
|
 |
James Boswell
Ranch Hand
Joined: Nov 09, 2011
Posts: 657
|
|
Amir
Glad you managed to sort out your issue.
I would argue that a web layer should never have any dependency on a persistence layer. It may depend on the business model but not the underlying data model (i.e. your JPA entities).
|
 |
Amir Keibi
Greenhorn
Joined: Apr 23, 2012
Posts: 25
|
|
|
I don't treat Entities as dummy objects that only hold data. They have logic in them which is required everywhere they go. This is a natural outcome of our Domain-Driven design.
|
 |
James Boswell
Ranch Hand
Joined: Nov 09, 2011
Posts: 657
|
|
|
The JPA entities should be for exactly that - holding data which is passed to and from the persistence tier. Any logic should be in a separate business model which sits between the web and persistence tiers. Just giving my opinion.
|
 |
 |
|
|
subject: EJB can't find Optional Package and thus Persistence Unit in it
|
|
|