-->
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.  [ 10 posts ] 
Author Message
 Post subject: newbie mapping question
PostPosted: Thu Nov 17, 2005 1:39 pm 
Newbie

Joined: Thu Nov 17, 2005 1:29 pm
Posts: 12
Hibernate version:
3.1
Mapping documents:

I have a mapping question, which is probably answered somewhere but I'm not familiar enough with your jargon to correctly search for it.

I have 3 tables involved.
Table A has an ID
Table B has two IDs one unique across the table and one Company ID.
Table X has A_ID, B_ID, B_COID. (such that one record in A can map to n records in B's but only once per company)


How do I define a mapping from A -> B and/or B -> A whereby they are mapped through table X. which has two columns on the B side?

Thanks,
Zorkerman


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 1:51 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 6:24 pm
Posts: 45
You may be able to use a one-to-many or many-to-many map within the A's configuration, see:
http://www.hibernate.org/hib_docs/v3/re ... ns-mapping
http://www.hibernate.org/hib_docs/v3/re ... -onetomany
http://www.hibernate.org/hib_docs/v3/re ... s-ofvalues


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 2:13 pm 
Newbie

Joined: Thu Nov 17, 2005 1:29 pm
Posts: 12
tysonnorris wrote:


The trouble with those is they presume only one ID on table B. Which I have set up and works, except it violates the unique constraint that only 1 A can point to one B, per company.

Thanks though,
Zorkerman


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 2:32 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 6:24 pm
Posts: 45
Ok, how about a composite-id:
http://www.hibernate.org/hib_docs/v3/re ... nents.html
or a"Collections of dependent objects" config (same page)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 3:07 pm 
Newbie

Joined: Thu Nov 17, 2005 1:29 pm
Posts: 12
tysonnorris wrote:
Ok, how about a composite-id:
http://www.hibernate.org/hib_docs/v3/re ... nents.html
or a"Collections of dependent objects" config (same page)


yes I looked at that too, and it is not a component, it is stored in a wholely separate table.

I'm currently trying to get an idBad with a many-to-many relationship working, but I'm getting some problems with the different numbers of keys defined, since the B table has one ID, and the company ID is not defined as a key.

Thanks for the help I really appreciate it,
Zorkerman


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 3:12 pm 
Newbie

Joined: Thu Nov 17, 2005 1:29 pm
Posts: 12
tysonnorris wrote:
Ok, how about a composite-id:
http://www.hibernate.org/hib_docs/v3/re ... nents.html
or a"Collections of dependent objects" config (same page)


I just don't think that a composite ID is the right thing to do. There is nothing wrong with loading a B without the company ID. Which I do often. If I set the company ID as an ID, I'll always need it to load these guys.

Zorkerman


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 3:18 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 6:24 pm
Posts: 45
X being a separate table does NOT imply that X is not a component of A, given that X's id is based on A's ID.

For example, I think your A object could be mapped similar to this example for an object eg.Order from the docs:
(think A=eg.Order, X=eg.OrderLine)

<class name="eg.Order" .... >
....
<set name="purchasedItems" table="purchase_items" lazy="true">
<key column="order_id">
<composite-element class="eg.OrderLine">
<many-to-one name="purchaseDetails class="eg.Purchase"/>
<many-to-one name="item" class="eg.Item"/>
</composite-element>
</set>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 4:37 pm 
Newbie

Joined: Thu Nov 17, 2005 1:29 pm
Posts: 12
tysonnorris wrote:
X being a separate table does NOT imply that X is not a component of A, given that X's id is based on A's ID.

For example, I think your A object could be mapped similar to this example for an object eg.Order from the docs:
(think A=eg.Order, X=eg.OrderLine)

<class name="eg.Order" .... >
....
<set name="purchasedItems" table="purchase_items" lazy="true">
<key column="order_id">
<composite-element class="eg.OrderLine">
<many-to-one name="purchaseDetails class="eg.Purchase"/>
<many-to-one name="item" class="eg.Item"/>
</composite-element>
</set>
</class>



maybe instead of saying a and b I could use a metaphor.

So here goes: I have users that can be a part of multiple groups. However, since there are multiple companies it is important that one user can only be mapped to a group in his same company. So the mapping table has a user ID, a group ID and a company ID. Then has a unique constraint on group and company.

The only failing with this metaphor is that users in my [metaphorical] world can be mapped to a group in any company.


I'm trying to set up the mappings both from the user to the group (eventually) and now the group back to the user. So I can say group.add(userx.userid) or a manager's mapUserToGroup(userID[], groupID, company)

Anyway, I think we're getting close. In fact what you've said already may have worked for someone who understands hibernate a little better.

Thanks again,
Zorkerman


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 4:39 am 
Regular
Regular

Joined: Sat Nov 05, 2005 5:33 am
Posts: 70
Location: Linz, Austria
What about showing us some DDL and perhaps the mappings you have so far.
Then it would be a lot easier to talk about the structure of the data.


Top
 Profile  
 
 Post subject: the code
PostPosted: Fri Nov 18, 2005 2:25 pm 
Newbie

Joined: Thu Nov 17, 2005 1:29 pm
Posts: 12
hhuber wrote:
What about showing us some DDL and perhaps the mappings you have so far.
Then it would be a lot easier to talk about the structure of the data.


alright lets try:

the card vendor:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
        <class name='works.supplier.db.HCardVendor' table='su_card_merchant_info'>
                <id name='id' column='su_card_merchant_id'>
                        <generator class='identity'/>
                </id>
                <property name='name' column='su_supplier_name'/>
                <property name='city' column='su_city'/>
                <property name='state' column='su_state'/>
                <property name='postalCode' column='su_postal_code'/>
                <property name='mcc' column='su_mcc'/>
                <property name='addr1' column='su_addr1'/>
                <property name='womenOwned' column='su_b_women_owned'/>
                <property name='hash' column='su_hash'/>

            <!--  join table="HCompanySupplier"
                inverse="true"
                optional="true">
                <key column="su_card_merchant_id"/>
                <many-to-one name="su_rollup_xref"
                    column="personId"
                    not-null="true"/>
            </join-->
        </class>
</hibernate-mapping>



the company supplier:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
        PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping>
        <class name='works.supplier.db.HCompanySupplier'
                table='su_supplier_rollup'>
                <id name='id' column='sr_rollup_id'>
                        <generator class='identity' />
                </id>
                <property name='name' column='sr_name' />
                <property name='companyId' column='sr_company_id' />
                <property name='active' column='sr_active' />

                <set name="cardVendors" table='su_rollup_xref' lazy='true'>
                        <key>
                                <column name="su_card_merchant_id" />
                        </key>

                        <many-to-many unique='true'
                                class="works.supplier.db.HCardVendor">
                                <column name='sr_rollup_id'></column>
                                <formula>'sr_company_id = su_supplier_rollup.sr_company_id'</formula>
                        </many-to-many>
                </set>
                <!--set name="cardVendors" table="su_xref_id" lazy="true">
                        <key column="sr_rollup_id">
                        <composite-element class="eg.works.supplier.db.CardVendor">
                        <many-to-one name="purchaseDetails' class="eg.Purchase"/>
                        <many-to-one name="item" class="eg.Item"/>
                        </composite-element>
                        </set-->
        </class>
</hibernate-mapping>



the datalayer is
Code:
su_card_merchant_info(su_card_merchant_id [long, unique])
su_rollup_xref (su_xref_id,
su_card_merchant_id, [fkey to su_card_merchant_info]
sr_rollup_id, [fkey to su_supplier_rollup]
sr_company_id [denormalized to improve performance from su_supplier_rollup]

su_supplier_rollup (sr_rollup_id [long unique],
sr_company_id [int, really just a piece of data]
)


also one card vendor can be mapped to n su_supplier_rollups, an su_supplier_rollup is considered unique if it's name and company_id are a unique combination.

I can get the mapping to almost work, but cannot get the hibernate datalayer to add the company ID to the su_rollup_xref table.

Thanks,
Nathan
)[/code][/quote]


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