Posted by joconner
on May 23, 2007 at 3:28 PM PDT
The Java Persistence API (JPA) is part of the EJB 3.0 spec and simplifies how you save application entities in a database. You can now use POJOs for both application business logic and persistence. Did you know that you can also use this API for desktop applications?
I'm working on a full-length article that describes how to use the JPA on the desktop. The article will be available next week on the Sun Developer Network , but I'll give you a sneak preview now.
So how do you setup the JPA for the desktop? First you need to get the API. You can download the API jar files from the GlassFish project. Yes, you can get a JPA reference implementation separately from the rest of GlassFish, but I don't really want to discourage you from getting the full application server if you want it. If I give you a link to just the persistence implementation , will you promise to also take a look at the rest of the GlassFish project ?
Copy the jar file installer to any project area. Run the installer:
java -jar glassfish-persistence-installer-v2-b46.jar
By the time you get the installer, you may have a newer version...so don't let the specific name above throw you if your copy is slightly different with b47, b48, or something even newer.
This command will create a subdirectory and several files as shown here:
Now that you have the API, you should add this to your project. In NetBeans 5.5, adding an external library to your compiler and runtime classpath is easy. Navigate to your project properties, choose the "Libraries" for your project and add the API jar file. You'll need only the
toplink-essentials.jar file; the other file is for backward compatibility with prior build scripts and versions. NOTE: NetBeans 5.5 already has a copy of the reference implementation, but you can use these instructions to keep your implementation up-to-date. Additionally, you may need to download the implementation if you use a different IDE or environment.
Once you've added this jar file to your projects compile and runtime path (either using the IDE as described or manually), you can use the API in your application. It's easy to setup, and surprisingly easy to use in your desktop application.