users@glassfish.java.net

Re: One to many, qualified by type?

From: <glassfish_at_javadesktop.org>
Date: Mon, 20 Aug 2007 10:45:09 PDT

<p>Continuing this highly popular thread, I'm beginning to realize with some uneasiness that in fact the multiple mappings approach may in fact be the recommended one. See, for example, <a href="http://forums.java.net/jive/thread.jspa?messageID=230520">this thread</a> wherein it is suggested to "overload" a column name. The gist is to mark one "half" of your composite <tt>@Embeddable</tt> primary key class as read-only, and then "overload" the same column in the entity class for which your <tt>@Embeddable</tt> serves as its <tt>@EmbeddedId</tt> with a fully mutable entity relationship (i.e. <tt>@ManyToOne</tt>).</p>

<p>I guess I still distrust this approach, which would lead me to do the following:
<ul>
<li>In my <tt>PersonNameEntityPK</tt> class, mark his <tt>personID</tt> property as read-only (<tt>insertable="false", updatable="false"</tt>). Nevertheless, provide a setter for it, because after all this is part of the primary key. It's just that it must be set/modified <i>manually</i>. Also ensure that this field's column name is something that we'll "overload" later, like <tt>PersonID</tt>.</li>
<li>In my <tt>PersonNameEntity</tt> class, ensure that in fact there <i>is</i> an entity relationship (<tt>@ManyToOne</tt>) between it and the <tt>PersonEntity</tt> class. Make sure that this relationship is persisted to the same column name as my read-only composite primary key (<tt>PersonID</tt>). Make sure that whenever this new <tt>PersonEntity</tt> is set here that I <i>manually extract</i> his <tt>long</tt> primary key and call the callable-only-by-my-class-internals <tt>setPersonID</tt> method on my <tt>PersonNameEntityPK</tt> class so that these properties stay in sync.</li>
</ul></p>

<p>Whether I distrust it or not, I can't find any other way to do it unless we bring an extra (unnecessary) join table into the picture.</p>

<p>It would seem to be the case that this is how to stay specification compliant while using part of a composite primary key as a foreign key back to an associated parent record. So pay attention, because when you're putting simple parent-specific maps into the database (like addresses or names or other things qualified by a user-supplied string), this is how to do it.</p>

<p>Best,<br/>
Laird</p>
[Message sent by forum member 'ljnelson' (ljnelson)]

http://forums.java.net/jive/thread.jspa?messageID=231661