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.
- Create a new project in NetBeans 6.1 (Ctrl+Shift+N)
- Choose "Java" --> "Java Desktop Application"
- Name the application, and select "Basic Application"
- 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)
- Change the class names if necessary, and click on "Create Persistence Unit..."
- The defaults here are fine - btw, the latest versions of TopLink and Hibernate both fully support JPA. I'm using TopLink for this project.
- 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)
- Drag an "Entity Manager", "Query", and "Query Result" instance onto the design area, and rename them if you choose.
- 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).
- 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
would actually be
SELECT *
FROM WeeklyDetail
INNER JOIN...
"wd" is just like a SQL alias (i.e. SELECT wd.* FROM WeeklyDetail wd), but it is required in JPQL.
SELECT wd
FROM WeeklyDetail wd
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. - Make sure in "other properties" below the query field, that your Entity Manager is selected as the "entityManager".
- Select your "Query Results" instance (I've called mine "weeklyDetailsQueryResult"), and view it's properties.
- Select your query that you just worked with, and check "modifiableWrapper" and "observable".
- 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. - 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).
- Right click on the table, and select "Bind" --> "elements".
- For the binding source, choose the query result you just created, and choose the columns you would like displayed in the table.
- Now when you run the project, the table should populate with the data from your database.
NOTE: screen captures showing each step coming soon
3 comments:
Very Good Article. I was wondering what that JPA persistance Palette was all about. Thanks.
Hello,
I really need the second part, and i don't find it on your website...
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.
Post a Comment