<!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;">
Inverse OneToOne and JoinTable problem
</h3>
<span style="margin-bottom: 10px;">
created by <a href="http://community.jboss.org/people/chrisxl">Chris Ball</a> in <i>EJB3</i> - <a href="http://community.jboss.org/message/582412#582412">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">
<div class="jive-rendered-content"><p>I have come across a problem in JBoss 4.2.2 (hibernate 3.2.4) where I cannot insert into or select from an entity (CompanyCar) that is the inverse-side of a @OneToOne join to a second entity (Employee), when the second entity has a @ManyToOne join that uses a @JoinTable.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>This is also a problem in JBoss 5.1, but it appears to have been “fixed” in JBoss 6.0 (hibernate 3.6). Unfortunately we are not yet in a position to consider upgrading the version of JBoss we use.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>I am looking for a workaround that we can use for JBoss 4.2.2. Also I’d like to know if this looks like a genuine bug, or whether I have got something wrong with my entity model that for some reason the hibernate spec has been changed to allow in the later release.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-weight: bold;">My entity model:</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </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> CompanyCar <font color="navy"><b>implements</b></font> Serializable <font color="navy">{</font>
 
 
      <font color="navy"><b>private</b></font> Long id;
      <font color="navy"><b>private</b></font> Employee employee;
 
 
      @Id
      @Column(name = <font color="red">"id"</font>, nullable = <font color="navy"><b>false</b></font>)
      <font color="navy"><b>public</b></font> Long getID() <font color="navy">{</font>
            <font color="navy"><b>return</b></font> id;
      <font color="navy">}</font>
 
 
      <font color="navy"><b>public</b></font> <font color="navy"><b>void</b></font> setID(<font color="navy"><b>final</b></font> Long id) <font color="navy">{</font>
            this.id = id;
      <font color="navy">}</font>
 
 
      @OneToOne(mappedBy=<font color="red">"car"</font>,optional = true, fetch = FetchType.LAZY)
      <font color="navy"><b>public</b></font> Employee getEmployee() <font color="navy">{</font>
            <font color="navy"><b>return</b></font> employee;
      <font color="navy">}</font>
 
      <font color="navy"><b>public</b></font> <font color="navy"><b>void</b></font> setEmployee(<font color="navy"><b>final</b></font> Employee employee) <font color="navy">{</font>
            this.employee = employee;
      <font color="navy">}</font>
<font color="navy">}</font>
 
 
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> EmployeeTeam <font color="navy"><b>implements</b></font> Serializable <font color="navy">{</font>
 
 
      <font color="navy"><b>private</b></font> String teamName;
      <font color="navy"><b>private</b></font> Collection<Employee> members;
 
 
      @Id
      @Column(name = <font color="red">"teamname"</font>, length = 30, nullable = <font color="navy"><b>false</b></font>)
      <font color="navy"><b>public</b></font> String getTeamName() <font color="navy">{</font>
            <font color="navy"><b>return</b></font> teamName;
      <font color="navy">}</font>
 
 
      <font color="navy"><b>public</b></font> <font color="navy"><b>void</b></font> setTeamName(<font color="navy"><b>final</b></font> String teamName) <font color="navy">{</font>
            this.teamName = teamName;
      <font color="navy">}</font>
 
 
      @OneToMany(mappedBy = <font color="red">"team"</font>)
      <font color="navy"><b>public</b></font> Collection<Employee> getMembers() <font color="navy">{</font>
            <font color="navy"><b>return</b></font> members;
      <font color="navy">}</font>
 
 
      <font color="navy"><b>public</b></font> <font color="navy"><b>void</b></font> setMembers(<font color="navy"><b>final</b></font> Collection<Employee> members) <font color="navy">{</font>
            this.members = members;
      <font color="navy">}</font>
<font color="navy">}</font>
 
 
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> Employee <font color="navy"><b>implements</b></font> Serializable <font color="navy">{</font>
 
 
      <font color="navy"><b>private</b></font> String employeeID;
      <font color="navy"><b>private</b></font> CompanyCar car;
      <font color="navy"><b>private</b></font> EmployeeTeam team;
 
      @Id
      @Column(name = <font color="red">"employeeid"</font>, length = 36, nullable = <font color="navy"><b>false</b></font>)
      <font color="navy"><b>public</b></font> String getEmployeeID() <font color="navy">{</font>
            <font color="navy"><b>return</b></font> employeeID;
      <font color="navy">}</font>
 
 
      <font color="navy"><b>public</b></font> <font color="navy"><b>void</b></font> setEmployeeID(<font color="navy"><b>final</b></font> String employeeID) <font color="navy">{</font>
            this.employeeID = employeeID;
      <font color="navy">}</font>
 
 
      @OneToOne(optional = true, fetch = FetchType.LAZY)
      @JoinColumn(name = <font color="red">"companycarid"</font>, referencedColumnName = <font color="red">"id"</font>)
      <font color="navy"><b>public</b></font> CompanyCar getCar() <font color="navy">{</font>
            <font color="navy"><b>return</b></font> car;
      <font color="navy">}</font>
 
 
      <font color="navy"><b>public</b></font> <font color="navy"><b>void</b></font> setCar(<font color="navy"><b>final</b></font> CompanyCar car) <font color="navy">{</font>
            this.car = car;
      <font color="navy">}</font>
 
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinTable(
                  name = <font color="red">"employeeteammembers"</font>,
                  joinColumns = <font color="navy">{</font> @JoinColumn(name = <font color="red">"employee_fk"</font>, referencedColumnName =<font color="red">"employeeid"</font>) <font color="navy">}</font>,
                  inverseJoinColumns = @JoinColumn(name = <font color="red">"team_fk"</font>, referencedColumnName = <font color="red">"teamname"</font>)
            )
      <font color="navy"><b>public</b></font> EmployeeTeam getTeam() <font color="navy">{</font>
            <font color="navy"><b>return</b></font> team;
      <font color="navy">}</font>
 
 
      <font color="navy"><b>public</b></font> <font color="navy"><b>void</b></font> setTeam(<font color="navy"><b>final</b></font> EmployeeTeam team) <font color="navy">{</font>
            this.team = team;
      <font color="navy">}</font>
<font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-weight: bold;">On inserting a CompanyCar:</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java">            <font color="navy"><b>final</b></font> CompanyCar car = <font color="navy"><b>new</b></font> CompanyCar();
            car.setID(1L);
            entityManager.persist(car);
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>not-null property references a null or transient value: uk.co.exel.test.CompanyCar.employee</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-weight: bold;">On loading a CompanyCar:</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Invalid column name 'companycarid'</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>The following SQL is generated by hibernate. It is attempting to join the CompanyCar directly to EmployeeTeam?</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-sql"><span class="jive-sql-keyword">select</span> companycar0_.id <span class="jive-sql-keyword">as</span> id0_, companycar0_1_.employee_fk <span class="jive-sql-keyword">as</span> employee0_3_
<span class="jive-sql-keyword">from</span> companycars companycar0_
<span class="jive-sql-keyword">left</span> <span class="jive-sql-keyword">outer</span> <span class="jive-sql-keyword">join</span> employeeteams companycar0_1_ <span class="jive-sql-keyword">on</span> companycar0_.id=companycar0_1_.companycarid
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p class="postbody">Many thanks for any comments / suggestions.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Chris.</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/582412#582412">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>