-->
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.  [ 5 posts ] 
Author Message
 Post subject: Question on mapping a peculiar ANY
PostPosted: Tue May 17, 2005 6:36 am 
Newbie

Joined: Thu Oct 21, 2004 2:43 pm
Posts: 9
Hi there,

we're kinda puzzled on how to map the following structure with hibernate
Classes-representation: (X and Y are of no further importance)

Interface Resource;Interface Consumer;

Class A extends X implements Resource, Consumer
{
private collection ReservationsForResource;
private collection ReservationsForConsumer;
}

Class B extends Y implements Resource
{
private collection ReservationsForResource;
}

Class Reservation
{
private Resource;
private Consumer;
}

So from the point of view of the reservation it is holding a resource and a consumer, but this Resouce might be an instance of class A or B.

the mapping we use :
----------------------------------------------
On reservation :
/**
* @hibernate.any id-type="long"
* @hibernate.column name = "CONSUMERCLASS"
* @hibernate.column name = "CONSUMERID"
*/
public Consumer getConsumer()
{
return consumer;
}

/**
* @hibernate.any id-type="long"
* @hibernate.column name = "RESOURCECLASS"
* @hibernate.column name = "RESOURCEID"
*/
public Resource getResource()
{
return resource;
}
---------------------------------------------------
On class A :

/**
* @hibernate.set cascade = "all-delete-orphan" inverse="true" lazy="true"
* @hibernate.collection-one-to-many class = "Reservation"
* @hibernate.collection-key column = "CONSUMERID"
*/
protected Set getReservationsForConsumer()
{
return reservationsForConsumer;
}

/**
* @hibernate.set cascade = "all-delete-orphan" inverse="true" lazy="true"
* @hibernate.collection-one-to-many class = "Reservation"
* @hibernate.collection-key column = "RESOURCEID"
*/
protected Set getReservationsOnResource()
{
return reservationsOnResource;
}
--------------------------------------------------------------
On class B :

/**
* @hibernate.set cascade = "all-delete-orphan" inverse="true" lazy="true"
* @hibernate.collection-one-to-many class = "Reservation"
* @hibernate.collection-key column = "RESOURCEID"
*/
protected Set getReservationsOnResource()
{
return reservationsOnResource;
}
-------------------------------------------------------------

This seemed to work just fine for what we needed.
But then we came to the point where something happened we did not expect :
an instance of Class A && Class B in the DB holding the same ID

So, suppose we have these instances in the DB :
class A, id '10';
class B, id '10';
class Reservation, ResourceID '10', ResourceClass 'B', ConsumerID '10', ConsumerClass 'A';

If we ask class A what reservations are on it, hibernate will go and check the reservation table for all rows containing RESOURCEID = 10.
One row will be found, but it will not notice that it is returning a reservation instance with ResouceClass B instead of A.

What we need is a way to make hibernate detect for what implementation of Resource (the Class key) it should be looking for.
We're hoping for a way to indicate to hibernate that it should look for more than the ID column, and that it also should take the CLASS column into account
i think this would solve our problem.

Any ideas on this matter ?
thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 17, 2005 10:21 am 
Newbie

Joined: Thu Oct 21, 2004 2:43 pm
Posts: 9
After reading and thinking a bit more, i seems obvious to me that we can conclude the following :
We need a way for hibernate to accept a composite key for one-to-many mappings.
This would imply a kind of : "one-to-manyAny" mapping
By doing so (using both ID and CLASS keys) hibernate should be fully set to find out what elements to add to the collection and which to omit.

Is there any info on how to do this ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 18, 2005 6:42 am 
Newbie

Joined: Thu Oct 21, 2004 2:43 pm
Posts: 9
anyone ?
We can't possibly be the first that come across this ?
are we ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 18, 2005 7:00 am 
Senior
Senior

Joined: Mon Apr 04, 2005 8:04 am
Posts: 128
Location: Manchester, NH USA
Can you please post your XDoclet-generated Hibernate mappings?

It is possible to use one-to-many with a composite id, so there may instead be a more effective way to organize your persistent object structure (e.g. with subclasses).

I don't see from your example where/how you are mapping the Resource interface to a Hibernate class - or are you?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 18, 2005 9:06 am 
Newbie

Joined: Thu Oct 21, 2004 2:43 pm
Posts: 9
hi there and thx for a reply

we are indeed not mapping the interfaces,
we use the any-mapping to map towards our different implementations of either Resource or Consumer


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.