0 Replies Latest reply on Feb 17, 2012 5:32 PM by Bobby Lawrence

    EJB3 inheritance and composite key

    Bobby Lawrence Newbie

      Hoping someone can help.

      I have an app that I'm trying to use EJB3 entities for.

      One entity represents a Person:


      @Table (name = "USERS")
      @Inheritance (strategy = InheritanceType.JOINED)
      public class Person {
          @Column (name = "PERSON_ID")
          @SequenceGenerator (name = "personIdGenerator", allocationSize = 1, sequenceName = "PERSON_SEQ")
          @GeneratedValue (generator = "personIdGenerator", strategy = GenerationType.SEQUENCE)
          private long id;
         @Column (name = "EMAIL")
         private String email;
          @Column (name = "FIRSTNAME")
          private String firstName;
         @Column (name = "LASTNAME")
         private String lastName;


      Now I have another entity called PrimaryInvestigator that extends Person so I can use the email, lastname, firstname attributes.  However the db table for this entity has a composite key representing the person's id and the project the person is associated with: projectId.  So this entity requires a composite key.  :


      @IdClass (PrimaryInvestigatorPK.class)
      @Table (name = "PROJECT_PI")
      public class PrimaryInvestigator extends Person {
          @Column (name = "PROJECT_ID", insertable=false, updatable=false)
          private long projectId;
          @Column (name = "BIOSKETCH_FILE_ID") 
          private long biosketchFileId;


      My IdClass for the PrimaryInvestigator entity looks like this:


      public class PrimaryInvestigatorPK implements Serializable {
        public Long userId;
        public Long projectId;


      Since part of the composite key is in the Person superclass, I figured I could just add another @Id attribute representing the other part of the composite key and Hibernate would simply use the combination of the two in my composite key.


      However, every time I try to deploy the EJBs, I get the following exception:


       org.hibernate.AnnotationException: Unable to define/override @Id(s) on a subclass: org.jlab.mis.apps.mics.valueobjects.PrimaryInvestigator


      Is what I'm trying to do possible?  If so how? 

      I can't use a @MappedSuperclass because I need to be able to persist Person objects by themselves...

      Any ideas?