Hibernate 2.1, Oracle 9i
Summary:
The particular relational semantics for my system mean that what I really want is an object that has no generator for its id -OR- that uses a foreign key for its ID but does not contain a one-to-one reference to the object that contains its key. I read elsewhere in the forums:
deniskrizanovic:
Quote:
I don't want the association bi-directional. How do I make it work unidirectional?
gavin:
Quote:
This is almost always a mistake, especially for the entity which owns the foreign key constraint.
http://forum.hibernate.org/viewtopic.php?t=929169
I do not think it is a mistake here, and it seems based on this case there would be many other similar cases.
Details:
I have 3 objects/tables for a subscription service being built on top of legacy data:
Employee: (legacy: huge object, many relationships all over the place)
Email: (legacy: small object, id is equal to foriegn key Employee_ID, one-to-one relationship with Employee)
File: (new: small object, refrerences Email many-to-many via Employee_ID, but NEVER has to know about the Employee information)
File is mapped using the following:
Code:
<class name="FileInfo" table="FILE_INFO">
<id
name="fileId"
type="java.lang.Long"
column="FILE_ID"
unsaved-value="null"
access="property">
<generator class="sequence">
<param name="sequence">seq_file_id</param>
</generator>
</id>
<property name="description" column="FILE_DESCRIPTION" type="string" not-null="false"/>
<set name="subscribers"
lazy="false"
table="SUBSCRIBERS">
<key column="FILE_ID"/>
<many-to-many class="Email" column="EMPLOYEE_ID"/>
</set>
</class>
I would LIKE to map Email using the the following. Note the lines that are commented out, because I do NOT want to include them (which is the crux of my problem):
Code:
<class name="Email" table="TEMAIL">
<id column="EMPLOYEE_ID"
name="employeeId"
type="java.lang.Long"
unsaved-value="null">
<!-- <generator class="foreign" > -->
<!-- <param name="employeeId">employee</param> -->
</generator>
</id>
<property name="email" type="string" column="EMAIL"/>
<!-- <one-to-one name="employee" class="Employee"/> -->
</class>
</hibernate-mapping>
This obviously does not work, because I am required to define a generator for the ID, and the correct generator (As shown in the comments) is to create a foreign key with a back-reference (one-to-one relationship on EMPLOYEE_ID) between Email and Employee (that is, I am required to include the commented lines).
As I understand it, if I DO define the relationship between Email and Employee, defining that relationship would require File to load the Employee corresponding to all the Email addresses. This would be a complete waste of resources.
Moreover, I am planning to use the Email object in a number of ways that do not involve the Employee object at all.
For example, when the user logs in, they carry around a token with their EMPLOYEE_ID in it. Sometimes I want to get the Email Address without getting the Employee. IF Email contains a one-to-one back-ref to Employee, it seems that it is impossible to get to the Email info without querying the Employee table.
There has to be a better way. Can someone help me find it?