-->
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.  [ 4 posts ] 
Author Message
 Post subject: Newbie - Many-to-One help needed.
PostPosted: Fri Sep 05, 2003 3:49 pm 
Newbie

Joined: Fri Sep 05, 2003 3:24 pm
Posts: 2
Hi there,

I'm a newbie to hibernate. I've a BookstoreItem objetc which is an Item associated with a Bookstore. The BookstoreItem has a bookstoreId & an itemId as the composite keys. Hibernate mandates that a seperate id for a BookstoreItem. How do I model this as a many-to-one with Bookstore and Item objects.

Bookstore (id, name, ...)
Item (id, title, author, language, ...)
BookstoreItem (bookstoreId, ItemId, PLU, ....)

What would the config be for BookstoreItem?

Thanks,
Ven


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 05, 2003 4:07 pm 
Beginner
Beginner

Joined: Thu Sep 04, 2003 2:50 pm
Posts: 45
Location: US: New York NY
here is one way

Code:
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class
        name="BookstoreItem"
        table="bookstore_item"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="bookstoreId"
            column="bookstore_id"
            type="long"
        >
            <generator class="sequence">
                <param name="sequence">bookstore_id_seq</param>
            </generator>
        </id>

       
       <property
            name="PLU"
            type="java.lang.String"
            update="true"
            insert="true"
            column="PLU"
            length="300"
        />
        <one-to-one
            name="bookstore"
            class="Bookstore"
            cascade="none"
            outer-join="auto"
            constrained="false"
        />

        <one-to-one
            name="item"
            class="Item"
            cascade="none"
            outer-join="auto"
            constrained="false"
        />

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-ListAd.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject: Newbie - Many-to-One help needed.
PostPosted: Sat Sep 06, 2003 2:09 pm 
Newbie

Joined: Fri Sep 05, 2003 3:24 pm
Posts: 2
Thanks Russel for your precious input.
The problem I'm facing is that the primary keys in BookstoreItem (BookstoreId, ItemId) are inturn foreign keys to Bookstore and Item entitites respectively.

Hence the generator for bookstoreItemId doent hold good in this scenario.

Any thoughts?

Thanks,
Ven


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 06, 2003 8:08 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Use a <composite-id> mapping with <key-many-to-one> subelements then. But carefully read the Parent / Child Relationship section of the documentation, since the default unsaved-value will not save new instances of the child class..


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