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.  [ 3 posts ] 
Author Message
 Post subject: How to configure many-to-many w/ data on relationship
PostPosted: Mon Nov 20, 2006 8:46 am 
Newbie

Joined: Fri Nov 17, 2006 9:40 am
Posts: 11
Location: Switzerland
Hi all

We need to configure a few many-to-many relationships in Hibernate. I believe I get the concept of a "simple" many-to-many relationship. However, I haven't found any examples on how to configure such a relationships if the many-to-many map itself contains data about the mapping. Say I have:

Table INVOICE (ID, INVOICENUMBER, AMOUNT)
Table PAYMENT (ID, PAYMENTDATE, AMOUNT)

A many-to-many relation between the two such that:

One PAYMENT can be distributed across multiple INVOICEs, one INVOICE can be paid by multiple PAYMENTs. AND - here's the thing - if a PAYMENT is distributed across multiple INVOICEs, the INVOICE-PAYMENT-MAP contains the amount assigned from a payment to an invoice. The business logic will acertain the the sum of all those amounts equals the total payment amount.

PAYMENT: 500.-
INVOICE A: 200.-, INVOICE-PAYMENT-MAP.AMOUNT = 200
INVOICE B: 300.-, INVOICE-PAYMENT-MAP.AMOUNT = 300

How do I configure this in Hibernate? (Please supply chapter or links with RTFM-comments)

Thanks
Simon


Top
 Profile  
 
 Post subject: Re: How to configure many-to-many w/ data on relationship
PostPosted: Mon Nov 20, 2006 10:08 am 
Newbie

Joined: Wed Jan 04, 2006 5:17 am
Posts: 4
You would need to create a third object InvoicePayment that would hold the additional information, in this case the amount of the payment allocated to the specific invoice. The relationships would also change: you would have a one-to-many relationship between Invoice and InvoicePayment and a many-to-one relationship between InvoicePayment and Payment.

Hope that helps!

Mike.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 20, 2006 11:50 am 
Newbie

Joined: Fri Nov 17, 2006 9:40 am
Posts: 11
Location: Switzerland
Hi Mike

Thnkas fou your input. Yes, we've been playing around with configurations such as

Code:
<hibernate-mapping>
    <class name="net.umbrella.data.model.PaymentInvoiceMapModel" table="PAYMENT_INVOICE_MAP">
        <composite-id name="id" class="net.umbrella.data.model.PaymentInvoiceMapIdModel">
            <key-property name="paymentid" type="int">
                <column name="PAYMENTID" />
            </key-property>
            <key-property name="invoiceid" type="int">
                <column name="INVOICEID" />
            </key-property>
        </composite-id>
        <many-to-one name="bookings" class="net.umbrella.data.model.BookingModel" update="false" insert="false" fetch="select">
            <column name="PAYMENTID" not-null="true" />
        </many-to-one>
        <many-to-one name="invoices" class="net.umbrella.data.model.InvoiceModel" update="false" insert="false" fetch="select">
            <column name="INVOICEID" not-null="true" />
        </many-to-one>
        <property name="amount" type="java.lang.Double">
            <column name="AMOUNT" scale="4" not-null="true" />
        </property>
    </class>
</hibernate-mapping>


The problem we most often encountered was: deleting an invoice deletes the map entry from the invoice side, but not from the payment side. Result: Hibernate exception along the line of "deleted object would be re-saved"

Cheers
Simon


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