<!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="http://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;">
    @PrimaryKeyJoinColumn - exception
</h3>
<span style="margin-bottom: 10px;">
    created by <a href="http://community.jboss.org/people/lysy78">Hubert Lyczek</a> in <i>EJB3</i> - <a href="http://community.jboss.org/message/581026#581026">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>Hello,</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>I've dicided to try one-to-one relation using @PrimaryKeyJoinColumn. But i cannot force it to work.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Environment:</p><p>- AS: jboss-6.0.0.Final</p><p>- java version "1.6.0_23"</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Problem description:</p><p>I have two entities: Customer, Address. Relation between Customer and address is bidirectional one-to-one. The relation&#160; uses primary keys.</p><p>You can see created entity beans below:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</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">"CUSTOMER"</font>)
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> Customer <font color="navy"><b>implements</b></font> Serializable<font color="navy">{</font>
&#160; 
&#160;&#160;&#160;&#160; @Id
&#160;&#160;&#160;&#160; @Column(name=<font color="red">"CUSTOMER_ID"</font>, nullable=<font color="navy"><b>false</b></font>)
&#160;&#160;&#160;&#160; @GeneratedValue(strategy=GenerationType.AUTO)
&#160;&#160;&#160;&#160; <font color="navy"><b>private</b></font> Integer customerId;
&#160; 
&#160;&#160;&#160;&#160; ...
&#160; 
&#160;&#160;&#160;&#160; <font color="darkgreen">//bidirectional</font>
&#160;&#160;&#160;&#160; @OneToOne(fetch=FetchType.EAGER, cascade=CascadeType.ALL, optional=<font color="navy"><b>true</b></font>)
&#160;&#160;&#160;&#160; @PrimaryKeyJoinColumn
 &#160;&#160;&#160;&#160; <font color="navy"><b>private</b></font> Address address;&#160; 
 
&#160;&#160;&#160;&#160; ...
<font color="navy">}</font>
&#160;
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code">@Entity
@Table(name="ADDRESS")
public class 
Address implements Serializable{ &#160;&#160;&#160;&#160; 
&#160;&#160;&#160;&#160; @Id
&#160;&#160;&#160;&#160; @Column(name="ADDRESS_ID", nullable=false)
&#160;&#160;&#160;&#160; @GeneratedValue(generator="foreignGenerator")&#160;&#160;&#160;&#160; @GenericGenerator(
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; name="foreignGenerator", strategy="foreign", 
&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160; parameters={@Parameter(name="property", value="customer"
&#160;&#160;&#160;&#160; )})&#160;&#160;&#160;&#160; private Integer addressId;
&#160;&#160;&#160;&#160; 
&#160;&#160;&#160;&#160; ...
&#160; 
&#160;&#160;&#160;&#160; 
&#160;&#160;&#160;&#160; @OneToOne(mappedBy="address", fetch=FetchType.LAZY, optional=false) 
 
 &#160;&#160;&#160;&#160; private Customer customer; 
 

&#160;&#160;&#160;&#160; ...
}
 


 
 
&#160; 

</code></pre><p><br/>As you see i'm using @GeneratedValue and @GenericGenerator to be sure that address_id will be the same as customer_id.</p><p>Unfortunately that doesn't work.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>I tried to run code (from remote client):</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java">Customer customer = <font color="navy"><b>new</b></font> Customer();
...&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="darkgreen">//set customer fields</font>
 Address address = <font color="navy"><b>new</b></font> Address();
 ...&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="darkgreen">//set addres fields</font>
customer.setAddress(address);address.setCustomer(customer);

&#160;
&#160;
 
 
databaseManager.createCustomer(customer);&#160;&#160;&#160;&#160; <font color="darkgreen">//persist</font>
</code></pre><p><br/>This is implementation of method createCustomer.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</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>void</b></font> 
&#160;&#160;&#160; &#160;&#160;&#160;&#160; entityManager.persist(customer);
<font color="navy">}</font> 
&#160;
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Unfortunately when i try to persist customer with address i get this exception:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code">Exception in thread "main" javax.ejb.EJBTransactionRolledbackException: 
org.hibernate.id.IdentifierGenerationException: null id generated for:class hl.test.relations.entity.Address
 CMTTxInterceptor.java:148)
&#160;&#160;&#160;&#160; at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.handleInCallerTx(
&#160;&#160;&#160;&#160; ...
 
 
 
 


</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>I thought that i would get the same id for Address as for Customer. But i got exeption and i don't know why.</p><p>Could you guys help me find out where is the reason of the exception and how to fix my code?</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Thank you in advance</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Hubert</p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Reply to this message by <a href="http://community.jboss.org/message/581026#581026">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in EJB3 at <a href="http://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>