persistence@glassfish.java.net

Re: using composition with JPA

From: Christopher Delahunt <christopher.delahunt_at_oracle.com>
Date: Tue, 24 Jul 2007 10:54:19 -0400

Sounds like you need 3 classes - 2 entities (Vehicle and Permit) and one embeddable (VehicleDetails) that contains the shared attributes. Vehicle and Permit then would have their own tables each with their own VehicleDetails instance.


Best Regards,
Chris
  ----- Original Message -----
  From: sud
  To: persistence_at_glassfish.dev.java.net
  Sent: Tuesday, July 24, 2007 10:27 AM
  Subject: RE: using composition with JPA


  I'm still unclear about the solutions proposed. Here's a more detailed real-life explanation of what I'm trying to achieve and I'd any appreciate help figuring this out.


  I have a JPA entity class called Vehicle as below

  @Entity
  public class Vehicle {
      @Id
       private int VIN;
      
      private String model;

      private String color;
  .....
  .....
  }

  I have a second JPA entity class called Permit which represents vehicle permits issued by the agency

  @Entity
  public class Permit {
      @Id
      private int number;
   
      private Date issuedDate;

      private Vehicle vehicle;
  ....
  ....
  }

  My business rule is a Vehicle can have many permits issued to it and a Permit can be issued to one and only one Vehicle. You should always be able to retrieve a specific permit and say what was the vehicle it was issued to. So if a permit was issued to a vehicle with color Red, and if the same vehicle has since been updated to color Blue, the permit that was issued should still say vehicle color Red. This is what I meant by the Permit is composed of Vehicle and that the instance variable vehicle in Permit is a snapshot of a vehicle when that permit is created.

  All this leads mean to believe that I need to be able to store the values of the Vehicle object in the Permit table and not create a reference from the Permit table to the Vehicle table. That way if the vehicle was updated, the permit still has the original properties of vehicle it was issued to.

  I hope I explained myself better now. Please let me know how to go about this in JPA.
  -sud

  My issue is with the vehicle object in the Permit class. If



------------------------------------------------------------------------------
  Take the Internet to Go: Yahoo!Go puts the Internet in your pocket: mail, news, photos & more.