[jboss-user] [EJB3] - EJB3 inheritance and composite key

Bobby Lawrence do-not-reply at jboss.com
Fri Feb 17 17:32:33 EST 2012

Bobby Lawrence [https://community.jboss.org/people/blawrence] created the discussion

"EJB3 inheritance and composite key"

To view the discussion, visit: https://community.jboss.org/message/717603#717603

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?

Reply to this message by going to Community

Start a new discussion in EJB3 at Community

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20120217/17ad9477/attachment-0001.html 

More information about the jboss-user mailing list