-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: ID with no generator OR foriegn generator w/o backref
PostPosted: Fri Apr 02, 2004 3:55 pm 
Newbie

Joined: Wed Mar 24, 2004 7:40 pm
Posts: 18
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?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 05, 2004 7:28 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You can use an assigned generator and manage it manually of via an interceptor
Check http://www.hibernate.org/162.html on one-to-one lazy capability

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.