Monday, June 9, 2008

Using the Java Persistence API with Swing in NetBeans 6.1 (part 1)

I have yet to find a good tutorial on how to get started using JPA with the new version of NetBeans that takes advantage of Beans Binding and other helpful new features. NetBeans has a lot built in to help you through it, but configuration is not obvious, and there are a few tricky parts along the way. So hopefully the steps below will at least get you started.

This tutorial will focus on using JPA without EJB (JPA is part of the EJB spec for now, but was built to be used with or without it - future versions will be separate from the EJB spec to better reflect this). I'm also assuming you have a database already set up, and are just looking to connect it to your application.

  1. Create a new project in NetBeans 6.1 (Ctrl+Shift+N)
  2. Choose "Java" --> "Java Desktop Application"
  3. Name the application, and select "Basic Application"
  4. Right click on your project, and select "New" --> "Entity Classes from Database..." and select your database connection (see my previous post about connecting to SQL Server)
  5. Change the class names if necessary, and click on "Create Persistence Unit..."
  6. The defaults here are fine - btw, the latest versions of TopLink and Hibernate both fully support JPA. I'm using TopLink for this project.
  7. Back on the main design screen, locate "Java Persistence" in the "Palette" section (normally on the right hand side, at the bottom of the Palette)
  8. Drag an "Entity Manager", "Query", and "Query Result" instance onto the design area, and rename them if you choose.
  9. Click on the instance of the "Query" in the "Inspector" (bottom left-hand side by default), and view its properties (bottom right-hand side by default).
  10. Click on the elipses "..." next to "query" on the properties pane, and type in a valid JPQL query. JPQL is similar in syntax to SQL, but it deal directly with objects. The JPQL query equivalent of

    SELECT *
    FROM WeeklyDetail
    would actually be

    SELECT wd
    FROM WeeklyDetail wd
    "wd" is just like a SQL alias (i.e. SELECT wd.* FROM WeeklyDetail wd), but it is required in JPQL.

    The nice part about querying objects is that for simple related objects/tables, you don't need to deal with JOINs. The above JPQL query actually returns all related objects (from different underlying SQL tables). To adjust for performance, you can explicitly state how the data is "fetched" - basically "all at one time" or "on demand". However, this is control in the entity classes.

    There's volumes more to JPA - to really get a handle on how to work with it, I suggest reading "Pro EJB 3: Java Persistence API" by Mike Keith and Merrick Schincariol.
  11. Make sure in "other properties" below the query field, that your Entity Manager is selected as the "entityManager".
  12. Select your "Query Results" instance (I've called mine "weeklyDetailsQueryResult"), and view it's properties.
  13. Select your query that you just worked with, and check "modifiableWrapper" and "observable".
  14. Under the "Code" tab (still in the properties), in the "Type Parameters" field, type the name of your package followed by a "." and the entity class name. Technically, the package name should be different casing from your application name (Pascal case for the application, camel case for the package.

    NOTE: If you type the casing incorrectly, it will appear to work until you run the project.
  15. Drag a jTable onto the design area (or jxTable if you're using SwingX (see my previous post) and want to have built-in column sorting).
  16. Right click on the table, and select "Bind" --> "elements".
  17. For the binding source, choose the query result you just created, and choose the columns you would like displayed in the table.
  18. Now when you run the project, the table should populate with the data from your database.
Part 2 of this tutorial will address how to update and delete data from within the table.

NOTE: screen captures showing each step coming soon


Anonymous said...

Very Good Article. I was wondering what that JPA persistance Palette was all about. Thanks.

l said...

I really need the second part, and i don't find it on your website...

Dave Gruska said...

Sorry, it doesn't look like I ever posted it.

Unfortunately, I haven't had to use Java in a long time, and I'm not sure how much of this I remember. If you have a specific question, though, I might be able to help you work through it - feel free to ask in this comment section.