title: JPA/Hibernate - Embedding an Attribute
body: <p>I am having a trouble mapping an embedded attribute of a class.  I have created some classes that are similar to what I am trying to do to illustrate.  Basically, I have an @Embeddable class hierarchy that uses Inheritance.  The top level class "Part Number" has only one attribute, and the extending classes add no attributes to the "Part Number" class, they only add some validation/logic.</p>
  
  <p>Here is what I mean:</p>
  
  <p><strong>PART</strong></p>
  
  <pre><code>@Entity
  @Table(name="PART")
  public class Part {
      private Integer id;
      private String name;
      private PartNumber partNumber;
  
      @Id
      @GeneratedValue(strategy=GenerationType.SEQUENCE)
      public Integer getId() {
          return id;
      }
      public void setId(Integer id) {
          this.id = id;
      }
  
      @Column(name="PART_NAME")
      public String getName() {
          return name;
      }
      public void setName(String name) {
          this.name = name;
      }
  
      @Embedded
      public PartNumber getPartNumber() {
          return partNumber;
      }
      public void setPartNumber(PartNumber partNumber) {
          this.partNumber = partNumber;
      }
  
  }
  </code></pre>
  
  <p><strong>PARTNUMBER</strong></p>
  
  <pre><code>@Embeddable
  public abstract class PartNumber {
  
      protected String partNumber;
      private String generalPartNumber;
      private String specificPartNumber;
  
      private PartNumber() {
  
      }
  
      public PartNumber(String partNumber) {
          this.partNumber = partNumber;
  
      }
  
      @Column(name = "PART_NUMBER")
      public String getPartNumber() {
          return partNumber;
      }
  
      public void setPartNumber(String partNumber) {
          this.partNumber = partNumber;
      }
  
      /**
       * @param partNumber
       * @return
       */
      public boolean validate(String partNumber) {
          // do some validation
          return true;
      }
  
      /**
       * Returns the first half of the Part Number
       * 
       * @return generalPartNumber
       */
      @Transient
      public String getGeneralPartNumber() {
          return generalPartNumber;
  
      }
  
      /**
       * Returns the last half of the Part Number 
       * which is specific to each Car Brand
       * 
       * @return specificPartNumber
       */
      @Transient
      public String getSpecificPartNumber() {
          return specificPartNumber;
  
      }
  
  }
  </code></pre>
  
  <p><strong>FORD PARTNUMBER</strong></p>
  
  <pre><code>public class FordPartNumber extends PartNumber {
  
      /**
       * Ford Part Number is formatted as 1234-#1234
       * 
       * @param partNumber
       */
      public FordPartNumber(String partNumber) {
          super(partNumber);
          validate(partNumber);
      }
  
      /*
       * (non-Javadoc)
       * 
       * @see com.test.PartNumber#validate(java.lang.String)
       */
      @Override
      public boolean validate(String partNumber) {
          // do some validation
          return true;
      }
  
      /*
       * (non-Javadoc)
       * 
       * @see com.test.PartNumber#getGeneralPartNumber()
       */
      @Override
      public String getGeneralPartNumber() {
          return partNumber;
  
      }
  
      /*
       * (non-Javadoc)
       * 
       * @see com.test.PartNumber#getSpecificPartNumber()
       */
      @Override
      public String getSpecificPartNumber() {
          return partNumber;
  
      }
  
  }
  </code></pre>
  
  <p><strong>CHEVY PARTNUMBER</strong></p>
  
  <pre><code>public class ChevyPartNumber extends PartNumber {
  
      /**
       * Chevy Part Number is formatted as 1234-$1234
       * 
       * @param partNumber
       */
      public ChevyPartNumber(String partNumber) {
          super(partNumber);
          validate(partNumber);
      }
  
      /*
       * (non-Javadoc)
       * 
       * @see com.test.PartNumber#validate(java.lang.String)
       */
      @Override
      public boolean validate(String partNumber) {
          // do some validation
          return true;
      }
  
      /*
       * (non-Javadoc)
       * 
       * @see com.test.PartNumber#getGeneralPartNumber()
       */
      @Override
      public String getGeneralPartNumber() {
          return partNumber;
  
      }
  
      /*
       * (non-Javadoc)
       * 
       * @see com.test.PartNumber#getSpecificPartNumber()
       */
      @Override
      public String getSpecificPartNumber() {
          return partNumber;
  
      }
  }
  </code></pre>
  
  <p>Of course this does not work, because Hibernate ignores the Inheritance Hierarchy and doesn't like the fact that PartNumber is abstract.  <strong>Is there some way to do this using JPA or Hibernate Annotations?</strong>  I have tried using the @Inheritance JPA annotation.</p>
  
  <p>I am not able to refactor the "PartNumber" part of the hierarchy because the original Developer wants to be able to extend PartNumber with N many XXXXPartNumber classes.</p>
  
  <p><strong>Does anyone know if anything like this will be a part of the JPA 2.0 or a new version of Hibernate?</strong></p>