Skip to main content

Using the Java Persistence API on the desktop

Posted by joconner on April 25, 2007 at 9:11 PM PDT

emcreation_sm.gifI'm always excited when I can get in on the fun those EE guys seem to have all the time. My latest exploration into the EE spec has helped me find another little treasure that I can bring back to the desktop: the Java Persistence API (JPA).

I've been tinkering, tossing in a JPA annotation here and there, discovering how to use the JPA in my desktop applications. I'm convinced that it's not so hard.

So, why would you want to use the JPA in your desktop apps? For one thing, the API is pretty simple. At the most basic level you have persist, find, merge, and remove methods. Second, you can branch out to full Java Persistence query language, a relative of Enterprise JavaBeans query language, when you want the full power of selecting, updating, and deleting entities using expressive queries that work with plain old Java objects (POJOs) in your applications. And don't forget that when you use the Java Persistence query language, your queries will work across multiple databases. You don't have to write vendor-specific SQL for each different db that you support. Write in this query language once, and your queries work everywhere. Hey, haven't I heard something like that before?

OK, I reread that last paragraph. There's nothing specific there about why this is good for desktops. Maybe that's another good point. You can use the same persistence framework for your desktop apps that you use for the web-based ones. That's worth something.

What do you need to get started with the JPA? You need a reference implementation for starters. You can get that from the GlassFish project. No need to download the whole GlassFish server. You just need the persistence API. You can download the Java Persistence implementation separately. You might also want the spec itself. You can get that from the EJB spec. It's a smaller subsection that you can retrieve as a PDF file. It's part of the JSR 220 JSR.

Got the spec? Got the implementation? Oops, one last thing. You have to have a database. I'm using Java DB myself. That's Sun's branded version of the Apache Derby project. Sun branded? Yes, if you get the Sun version, you have options for support. The db is free of course, but you can purchase support plans too...or not. It's entirely up to you.

Related Topics >>