-->
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.  [ 1 post ] 
Author Message
 Post subject: Map Relationship Attributes to multiple properties
PostPosted: Mon Apr 07, 2008 11:04 am 
Newbie

Joined: Thu Mar 06, 2008 5:27 am
Posts: 5
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


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

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.