|
|
||
Brian Leonard's BlogNovember 2006 ArchivesUsing Composite Keys with JPAPosted by bleonard on November 29, 2006 at 09:14 AM | Permalink | Comments (14)The Java Persistence API is beautiful. I create a class, annotate it with @Entity and @Id and I'm on my way. That is, however, assuming the Id is of a simple type (I'm a firm believer that primary keys should be of a simple type, unrelated to the data they index.). However, if you find yourself in a situation where you must use a composite key (hopefully this is only because you're connecting to some existing data set), JPA will still work for you, although it gets a bit more complex. First of all, you can declare a composite primary key using one of two annotations: @IdClass or @EmbeddedId. In this blog, I'm going to focus on the latter. Using @EmbeddedIdA composite primary key must be represented by a primary key class. And when using the @EmbeddedId approach, it must be annotated as @Embeddable. @Embeddable public class ContactPK implements Serializable { @Column(name = "FIRSTNAME", nullable = false) private String firstname; @Column(name = "LASTNAME", nullable = false) private String lastname; ... The primary key in the entity class is annoated with @EmbeddedId annotation: @Entity public class Contact implements Serializable { /** * EmbeddedId primary key field */ @EmbeddedId protected ContactPK contactPK; ... However, it doesn't stop there. The entity class must also implement the hashcode() and equals() methods. As I mentioned in my introduction, hopefully you're only using composite keys if you already have existing tables defined that way. In the spirit of that assumption, we're going to start with a table and just use NetBeans to create our entity class. Setting Things UpTo continue we need NetBeans 5.5 and the Java EE Application Server 9.0. If you need both, just download and install the bundle. Creating the TableCompositeCustomer.sql will create a table with a composite primary key populated with some sample data.
Generating Entities With Composite KeysThe setup was the hard part. To create the entity class:
And you're done. The wizard created the primary key class, CompositeCustomerPK as well as the entity CompositeCustomer. Testing the EntityHere we'll delete our table and let JPA re-create it for us.
A Conversation with SeamPosted by bleonard on November 15, 2006 at 10:51 AM | Permalink | Comments (1)Another one of Seam's compelling features is its ability to manage "conversations". A Seam conversation provides fine grain control over the traditional HTTP session scope so that "concurrent conversations" can occur in HTTP session, which usually occur when users open another browser window or tab. The problem is best described with an example. I've added a confirmation page to the registration application used in my previous blog entries. So consider this application use case: I launch the application and enter my registration information:
And the Registration page takes me to a confirmation page:
However, rather than completing the transaction (or conversation), I open a new browser tab and decide to register my brother as well:
And click Register to load his confirmation page:
But before completing this 2nd transaction (conversation), I return back to the registration tab with my registration details and now decide to complete the original transaction (conversation):
Hmmm, not was I was expecting (actually, it was - but you know what I mean). Now for anyone who's done web application development, managing the HTTP Session for these scenarios can be a nightmare. Let's see how easily the Seam framework addresses this all too common problem. If you'd like to produce and fix this problem yourself, continue with the following Steps. If you just want to see the solution, jump to Step 3: Solving the Conversation Problem. Step 1: Getting the Goods
Step 2: Setting Up Your EnvironmentIn this first step, we'll set up the environment to run our Registration application. Add the JBoss Server to NetBeans
The Registration Project that we'll open in the next step references a class library called JBossSeam, which we'll now create using the 1.0.1 GA of Seam:
These libraries are now available for use by any project. Open the Registration ProjectThe Registration application is a NetBeans Enterprise Application Project, which is actually comprised of 3 projects: Registration, Registration-EJBModule and Registration-WebModule. Registraiton-EJBModule and Registration-WebModule are Java EE Modules of the Registration project. Registration-EJBModule generates the EJB jar file. Registration-WebModule generates the war file and Registration generates the ear file which contains the jar and war.
Step 3: Solving the Conversation ProblemThe Registration application in the example is already using Seam, it's just not using the conversation feature. In this step, we'll make a few simple changes to our application which will solve the conversation problem outlined in the introduction.
| ||
|
|