<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">

<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>

                                <td>

                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="https://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>

                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px;  -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
    EJB3 inheritance and composite key
</h3>
<span style="margin-bottom: 10px;">
    created by <a href="https://community.jboss.org/people/blawrence">Bobby Lawrence</a> in <i>EJB3</i> - <a href="https://community.jboss.org/message/717603#717603">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>Hoping someone can help.</p><p>I have an app that I'm trying to use EJB3 entities for.</p><p>One entity represents a Person:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java">@Entity
@Table (name = <font color="red">"USERS"</font>)
@Inheritance (strategy = InheritanceType.JOINED)
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> Person <font color="navy">{</font>
&#160;
&#160;&#160;&#160; @Column (name = <font color="red">"PERSON_ID"</font>)
&#160;&#160;&#160; @Id
&#160;&#160;&#160; @SequenceGenerator (name = <font color="red">"personIdGenerator"</font>, allocationSize = 1, sequenceName = <font color="red">"PERSON_SEQ"</font>)
&#160;&#160;&#160; @GeneratedValue (generator = <font color="red">"personIdGenerator"</font>, strategy = GenerationType.SEQUENCE)
&#160;&#160;&#160; <font color="navy"><b>private</b></font> <font color="navy"><b>long</b></font> id;
&#160;
&#160;&#160; @Column (name = <font color="red">"EMAIL"</font>)
&#160;&#160; <font color="navy"><b>private</b></font> String email;
&#160;&#160;&#160; 
&#160;&#160;&#160; @Column (name = <font color="red">"FIRSTNAME"</font>)
&#160;&#160;&#160; <font color="navy"><b>private</b></font> String firstName;
&#160;&#160;&#160; 
&#160;&#160; @Column (name = <font color="red">"LASTNAME"</font>)
&#160;&#160; <font color="navy"><b>private</b></font> String lastName;
&#160;
&#160;&#160; ...
<font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Now I have another entity called PrimaryInvestigator that extends Person so I can use the email, lastname, firstname attributes.&#160; 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.&#160; So this entity requires a composite key.&#160; :</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java">@Entity
@IdClass (PrimaryInvestigatorPK.class)
@Table (name = <font color="red">"PROJECT_PI"</font>)
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> PrimaryInvestigator <font color="navy"><b>extends</b></font> Person <font color="navy">{</font>
&#160;
&#160;&#160;&#160; @Column (name = <font color="red">"PROJECT_ID"</font>, insertable=false, updatable=<font color="navy"><b>false</b></font>)
&#160;&#160;&#160; @Id
&#160;&#160;&#160; <font color="navy"><b>private</b></font> <font color="navy"><b>long</b></font> projectId;
&#160;
&#160;&#160;&#160; @Column (name = <font color="red">"BIOSKETCH_FILE_ID"</font>) 
&#160;&#160;&#160; <font color="navy"><b>private</b></font> <font color="navy"><b>long</b></font> biosketchFileId;
&#160;&#160;&#160; ...
<font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>My IdClass for the PrimaryInvestigator entity looks like this:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java"><font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> PrimaryInvestigatorPK <font color="navy"><b>implements</b></font> Serializable <font color="navy">{</font>
&#160;
&#160; <font color="navy"><b>public</b></font> Long userId;
&#160; <font color="navy"><b>public</b></font> Long projectId;
&#160;
&#160; ...
<font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>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.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>However, every time I try to deploy the EJBs, I get the following exception:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java"> org.hibernate.AnnotationException: Unable to define/override @Id(s) on a subclass: org.jlab.mis.apps.mics.valueobjects.PrimaryInvestigator
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Is what I'm trying to do possible?&#160; If so how?&#160; </p><p>I can't use a @MappedSuperclass because I need to be able to persist Person objects by themselves...</p><p>Any ideas?</p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Reply to this message by <a href="https://community.jboss.org/message/717603#717603">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in EJB3 at <a href="https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2029">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


                </td>
            </tr>
        </tbody>
    </table>

</div>

</body>
</html>