|
|
||
Sahoo's BlogMarch 2006 ArchivesUsing Java Persistence API in application client in Java EE platformPosted by ss141213 on March 21, 2006 at 01:03 AM | Permalink | Comments (7)In Java EE 5 platform Java Persistence API can be used in three types of containers, viz: ejb, web and application client container. In my earlier blogs, I had talked about using this API in web applications and in EJB applications. This time we will talk about using Java Persistence API in application clients. We also discuss about two different way of packaging the application. You can download the complete sample, unzip and run 'ant deploy' to see the sample in action. The steps are very simple as discussed below: Step #1: Write entity bean UserCredential.java Step #2: Define a persistence unit in persistence.xml <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"> Points to note about this persistence.xml are: 1) One persistence.xml can be used to define multiple PUs, but in this case we have defined only one PU by name pu1. 2) We need not specify any elements/attributes other than transaction-type, as the default values are just fine. By default the entity manager's transaction type is JTA. Since Application Client Container is not required to support JTA, we can not use JTA entity managers in this example. So we have set it to RESOURCE_LOCAL here. 3) There is no need to enumerate all the entity bean class names inside because we are defining only one PU in this persistence.xml and we will be packaging the entity classes along with this persistence.xml in a jar file, so container can discover all the entity beans. Step #3: Write Client.java 1) It declares dependency on the PersistenceUnit using @PersistenceUnit as shown below: @PersistenceUnit private static EntityManagerFactory emf; Also note that, the field ucm is static. More discussion on this further below. Secondly, there is only one PU visible to this application client, hence there is no need to specify the unitName attribute in @PersistenceUnit. 2) It must be a public class because it will be used by ACC which does not belong to the package of this class. 3) We also need to write a manifest.mf file which must contain the name of the class containing the main(). 4) The rest of the client class is simple. It creates an EntityManager using the injected EntityManagerFactory. It closes it at the end of the application. Since we can't use JTA, the client uses EntityTransaction API to manage transactions. 5) There is no need to write application-client.xml because it is optional. Java EE 5 compatible platform can discover appclient.jar's module type because it contains META-INF/MANIFEST.MF with a Main-Class attribute. Step #4: Build and package the application using build.xml jpa_acc_option1.ear Since the entities.jar is placed in the EAR lib directory, it is automatically added to CLASSPATH while running the appclient. So, no need to use Class-Path manifest entry. Option #2: package the entity beans and the persistence.xml in the appclient.jar along with appclient main class. No need to make an ear file in this case, as we can just deploy the appclient.jar. The appclient.jar contains following entries: appclient.jar: To demonstrate this, there are two build targets, viz: build-app1 and build-app2. The third build target internally call these two targets. Same goes for deploy and verify targets. Both of the packaging alternatives discussed here are completely portable. It is upto you to decide which of the options you want to use. Step #5: Run the application Use of Java2DB Why injected field or method must be static in application client? Software Required Hope you found this example useful. More examples of glassfish persistence | ||
|
|