Hi!
I have the following problem where I wanted to map relationship attributes on different properties.
The example is situated in the domain of theatre shows. The theatre has several seats and each seat can be sold for a different show. A reseller can then by a contingent of seats for a certain show and assign a price to every seat. So, a seat can be in multiple contingents, a contingent can have multiple seats and every seat in a contingent has a certain price. I modelled this via three tables:
Code:
Seat table
SEAT_ID | ..... other attributes
Contingent table
CONTINGENT_ID | .... other attributes
Seat2Contingent table
SEAT_ID | CONTINGENT_ID | PRICE
The problem occured with the contingent class. I first started out with a mapping that maps seats onto the contingent class, such that I can call Contingent.getSeats(). This looks as follows.
Code:
<set name="seats" table="Seat2Contingent" cascade="all" inverse="true" fetch="select">
<key column="CONTINGENT_ID"/>
<many-to-many column="SEAT_ID" class="datamodel.Seat" />
</set>
Then, after introducing the price, I wanted to add a hashtable that contains the price for every seat, such that I can call Contingent.getSeatPrice(Seat seat). I specified this in the Contingent mapping as follows:
Code:
<map name="seatPrice" table="Seat2Contingent" cascade="all" fetch="select">
<key column="CONTINGENT_ID"/>
<index-many-to-many column="SEAT_ID" class="datamodel.Seat"/>
<element column="PRICE" type="big_decimal"/>
</map>
From a mapping point of view this is valid and works fine, however, since hibernate does not realize that the set and the map refer to the same table, it will create two entries in the database for every seat:
Code:
SEAT_ID | CONTINGENT_ID | PRICE
----------------------------------
1 | 1 | null
1 | 1 | 20
2 | 1 | null
2 | 1 | 15
...
As soon as I declare the PRICE column to be not-null, Hibernate will fail with an exception - it will try to insert (1,1,null) for the seats mapping, although this column is defined to be not-null and rather the value of the seatPrice map should be taken.
Now my actual question: is there a possibility to create a Hibernate mapping for the mentioned situation? Is it possible to tell Hibernate that two properties/collections refer to the same table and upon saving/updating Hibernate should take both properties/collections to write to the database?
The only other possibility that I can think of when trying to map relationship attributes, is to create a separate class with a composite identifier for the Seat2Contingent table. Are there any other ways how relationship attributes can be mapped?
thanks, best regards
iS