- EJB 3 Developer Guide
- Michael Sikora
- 419字
- 2021-07-02 11:34:55
Packaging and Deploying Entities
As we have introduced a number of new source code artifacts that were not present in the previous chapter on session beans, the packaging process will be a little different. First we look at the program directory structure for our sample application.
The Program Directory Structure
Below is the program directory structure for the BankClient
application which invokes the BankServiceBean
in order to add the Customer
entity to the database.

There are a couple of differences in the directory structure from the one described in the previous chapter. We have added an entity
subdirectory; this contains the entity source code, Customer.java
. We have also added a config
subdirectory containing the manifest.mf
and persistence.xml
files.
Building the Application
Because we are persisting entities from an EJB container we need to place the persistence.xml
file in the META-INF
directory within the EJB module, BankService.jar
. The package-ejb
target in the Ant build file does this:
<target name="package-ejb" depends="compile"> <jar jarfile="${build.dir}/BankService.jar"> <fileset dir="${build.dir}"> <include name="ejb30/session/**" /> <include name="ejb30/entity/**" /> </fileset> <metainf dir="${config.dir}"> <include name="persistence.xml" /> </metainf> </jar> </target>
We can use the jar -tvf
command to examine the contents of the JAR file:
C:\EJB3Chapter03\glassfish\lab1\build>jar -tvf BankService.jar
...META-INF/
...META-INF/MANIFEST.MF
...ejb30/
...ejb30/entity/
...ejb30/session/
...ejb30/entity/Customer.class
...ejb30/session/BankService.class
...ejb30/session/BankServiceBean.class
...META-INF/persistence.xml
This is only one of a number of ways to package the persistence unit. Entities can be persisted not only from an EJB container but also from a web container or from a Java SE application running outside the container. We shall see an example of Java SE persistence in Chapter 6. Consequently the persistence unit (entity classes together with the persistence.xml
file) can be placed in WEB-INF/classes
directory of a WAR file or in a Java SE application client jar file.
Alternatively we can package a persistence unit into a separate jar file. This jar file can then be added to the WEB-INF/lib
directory of a WAR file, the root of an EAR file or in the library directory of an EAR file. Adding a persistence unit to a Java EE module limits its scope to that module. If we place the persistence unit in an EAR it is visible to all modules within the application.
In our example we want the database tables to be created at deploy time, and dropped when we undeploy an application from the container. We modify the deploy
target in our Ant build file, adding a createtables=true
clause:
<target name="deploy">
<exec executable="${glassfish.home}/bin/asadmin"
failonerror="true"
vmlauncher="false">
<arg line="deploy --user admin --passwordfile
adminpassword --createtables=true
${build.dir}/BankService.ear"/>
</exec>
</target>
Similarly we add a droptables=true
clause for the undeploy
target.
<target name="undeploy">
<exec executable="${glassfish.home}/bin/asadmin"
failonerror="true"
vmlauncher="false">
<arg line="undeploy --user admin --passwordfile
adminpassword --droptables=true BankService"/>
</exec>
</target>
Note that before we can deploy BankService
within GlassFish, we need to start up the embedded Derby database. We do this from the command-line with the asadmin
utility:
C:\> asadmin start-database
- Flash CS6標(biāo)準(zhǔn)教程(全視頻微課版)
- Photoshop CS6從入門到精通
- ChronoForms 3.1 for Joomla! site Cookbook
- After Effects CC 2019 影視后期特效合成案例教程
- 我為PS狂 Photoshop照片處理一分鐘秘笈
- 新編AutoCAD 2016從入門到精通
- Web 2.0 Solutions with Oracle WebCenter 11g
- Android User Interface Development: Beginner's Guide
- Apache Maven 3 Cookbook
- Photoshop 2024從入門到精通
- Indesign平面排版技術(shù)應(yīng)用
- 新印象:CINEMA 4D電商設(shè)計基礎(chǔ)與實戰(zhàn)(全視頻微課版)
- NetSuite OneWorld Implementation 2011 R2
- 中文版Flash CS6動畫制作(慕課版)
- Microsoft Silverlight 4 and SharePoint 2010 Integration