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
|