-->
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: Recursion and Composite Keys
PostPosted: Mon Oct 20, 2003 12:23 am 
Newbie

Joined: Sun Oct 19, 2003 8:53 pm
Posts: 1
Hi,

I'm currently working on a project that involves reverse-engineering Hibernate mappings and model objects from an existing (and ugly) database schema. Up until now we've managed to work with the numerous composite keys but have now hit a hurdle.

The basic concept is like the Employee-Manager recursive relationship you see in Database 101 tutorials.

We have a table:

EMPLOYEE {
database_id
employee_id
employee_name
....
manager_id
}

where the primary key is database_id and employee_id combined. Each employee can have a manager (zero or one) who is also an employee and each manager can have many employees (though we're not concerned with this side of the relationship at the moment).

<hibernate-mapping>

<class name="Employee" table="EMPLOYEE">
<composite-id name="id" class="EmployeePrimaryKey">
<key-property name="databaseId" column="database_id"/>
<key-property name="id" column="employee_id"/>
</composite-id>

<property name="employeeName" column="employee_name"/>
... other properties

<many-to-one name="manager" class="Employee" insert="false" update="false">
<column name="database_id"/>
<column name="manager_id"/>
</many-to-one>
</class>

</hibernate-mapping>

This appears to work fine until an employee with no manager is encountered (i.e. manager_id == null). I think the problem occurs because although manager_id is null, database_id always has a value and hence the composite EmployeePrimaryKey is never null, so Hibernate tries to load the manager even though half the Key is null. Looking at the Hibernate src (Component.nullSafeGet()) it seems as though Hibernate only terminates loading if ALL the properties of the primary key are null. The behaviour we are after would be if any of the primary keys are null (this makes sense for a composite key)

Is there any workaround for this without having to modify Hibernate's code?

TIA


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 20, 2003 7:07 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Hmmm I admit that this does kinda make sense.

A workaround for now would be to use a UserType.


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.