-->
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.  [ 13 posts ] 
Author Message
 Post subject: Please help with these tables relationship
PostPosted: Thu Aug 26, 2004 7:58 am 
Beginner
Beginner

Joined: Wed Aug 25, 2004 10:27 pm
Posts: 21
Location: Indonesia
Hibernate version: 2.1
Name and version of the database you are using: SQL Server 2000


Hi all,

I have the following two tables (from Northwind example) :

Products
======
ProductID (PRIMARY KEY)
ProductName
CategoryID

Categories
=======
CategoryID (PRIMARY KEY)
CategoryName

I want to connect Products.CategoryID to Categories.CategoryID so that when I retrieve the Products record I also can get the category name too.

Is it possible to define the relationship only in the mappings, not by HQL ?

I try to use one-to-one relationship but there is no way to specify that I want to connect from Products.CategoryID not from Products.ProductID.

Any help would be greatly appreciated.


Setya


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 26, 2004 8:10 am 
Regular
Regular

Joined: Thu Aug 05, 2004 2:27 am
Posts: 54
Location: South Africa
from product
Code:

<!-- bi-directional many-to-one association to Category -->
    <many-to-one
        name="category"
        class="package.blablablabla.Category"
    >
        <column name="CategoryId_fk"  not-null="true"/>
    </many-to-one>


and from category
Code:
<!-- bi-directional one-to-many association to Product -->
    <set
        name="products"
        lazy="true"
        inverse="true"
    >
        <key>
            <column name="CategoryId_fk" />
        </key>
        <one-to-many
            class="package.blablablabla.Product"
        />
    </set>



now when you select Product you can call product.getCategory(), and work from there for it's name, or from Category to get all products in that category.
just note the mapping for category says lazy="true", check hibernate docs for details of effects of lazy


Top
 Profile  
 
 Post subject: Please help with these tables relationship
PostPosted: Thu Aug 26, 2004 10:31 pm 
Beginner
Beginner

Joined: Wed Aug 25, 2004 10:27 pm
Posts: 21
Location: Indonesia
tq wrote:
from product
Code:

<!-- bi-directional many-to-one association to Category -->
    <many-to-one
        name="category"
        class="package.blablablabla.Category"
    >
        <column name="CategoryId_fk"  not-null="true"/>
    </many-to-one>


and from category
Code:
<!-- bi-directional one-to-many association to Product -->
    <set
        name="products"
        lazy="true"
        inverse="true"
    >
        <key>
            <column name="CategoryId_fk" />
        </key>
        <one-to-many
            class="package.blablablabla.Product"
        />
    </set>




Thanks for your response,

From your explanation I have to add one more column in Categories table to hold the ProductID.
This seems to be Parent-Child relationship, in fact they're not since Categories table is merely a master table that holds the categories information and it does not care which products it refers to.

Is this the only way to go ?


Regards,



Setya


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 27, 2004 12:30 am 
Beginner
Beginner

Joined: Mon Dec 15, 2003 5:25 am
Posts: 48
Location: Delhi, India
No.

CategoryID is already part of Products table.

_________________
Vinod K. Singh


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 27, 2004 2:15 am 
Regular
Regular

Joined: Thu Aug 05, 2004 2:27 am
Posts: 54
Location: South Africa
vinodsingh wrote:
No.

CategoryID is already part of Products table.

100%

Setya, that mapping is based on your schema, my apologies for not using exact column names like CATEGORYID instead of CategoryId_fk.

you can copy that mapping into your hmb.xml and only add the properties I didn't list, such as product name, oh and correct the variable and column names. I only wanted to show you how the relationship is mapped as your topic suggests.


Top
 Profile  
 
 Post subject: Re: Please help with these tables relationship
PostPosted: Fri Aug 27, 2004 4:18 am 
Beginner
Beginner

Joined: Wed Aug 25, 2004 10:27 pm
Posts: 21
Location: Indonesia
tq wrote:
and from category
Code:
<!-- bi-directional one-to-many association to Product -->
    <set
        name="products"
        lazy="true"
        inverse="true"
    >
        <key>
            <column name="CategoryId_fk" />
        </key>
        <one-to-many
            class="package.blablablabla.Product"
        />
    </set>




Thanks again for your response,

Sorry for not being clear, the above mapping suggests that I should create one more pair of getter/setter methods that holds products collection (name="products") in the Category persistent class.

This is actually what I want to avoid since I want the relationship to go to one direction only (Products -> Categories).


Regards,


Setya


Top
 Profile  
 
 Post subject: Re: Please help with these tables relationship
PostPosted: Fri Aug 27, 2004 4:36 am 
Regular
Regular

Joined: Thu Aug 05, 2004 2:27 am
Posts: 54
Location: South Africa
Setya wrote:
This is actually what I want to avoid since I want the relationship to go to one direction only (Products -> Categories).

ah I see, then don't use it, it's not required to be bi-directional.
you must however have the mapping in product.


Top
 Profile  
 
 Post subject: Re: Please help with these tables relationship
PostPosted: Fri Aug 27, 2004 6:10 am 
Beginner
Beginner

Joined: Wed Aug 25, 2004 10:27 pm
Posts: 21
Location: Indonesia
tq wrote:
ah I see, then don't use it, it's not required to be bi-directional.
you must however have the mapping in product.


tq,

Yes, I've tried to remove the mapping in the Categories side and it works.

On more thing, when I load the Product class is it possible to explicitly tell Hibernate through program not to load the corresponding Category class, since I do not always need the data from Category table. The intention is to boost performance because Hibernate does not have to perform the unneccessary join from Products -> Categories.

Regards,


Setya


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 27, 2004 7:16 am 
Regular
Regular

Joined: Thu Aug 05, 2004 2:27 am
Posts: 54
Location: South Africa
sorry Setya I have no idea, short of using hql or sql to only select what you want.
You can optimise it by using outer-join but I don't know how to exclude it.
How will you update the product without a category though?
or do you plan to use an addition property in your mapping that holds the categoryId?
oh nevermind, maybe you just want to select, still you'd have to hold that categoryId somewhere.

Anyway, I'm pretty new to hibernate myself, and haven't explored this, maybe post a seperate topic asking specifically about that.


Top
 Profile  
 
 Post subject: Please help about these tables relationship
PostPosted: Fri Aug 27, 2004 7:55 am 
Beginner
Beginner

Joined: Wed Aug 25, 2004 10:27 pm
Posts: 21
Location: Indonesia
tq wrote:
oh nevermind, maybe you just want to select, still you'd have to hold that categoryId somewhere.


Yes, that's what I intend to do, just plain selecting column in Products only.

Quote:
Anyway, I'm pretty new to hibernate myself,


Are u kidding ?, u sound like an expert to me.

Quote:
and haven't explored this, maybe post a seperate topic asking specifically about that.


I will.

Thank you very much for your help so far.


Regards,

Setya


Top
 Profile  
 
 Post subject: Re: Please help about these tables relationship
PostPosted: Fri Aug 27, 2004 8:25 am 
Regular
Regular

Joined: Thu Aug 05, 2004 2:27 am
Posts: 54
Location: South Africa
Setya wrote:
Are u kidding ?, u sound like an expert to me.

gee thanks, but after months of hibernate and I'm still checking the docs daily

Quote:
I will.

Thank you very much for your help so far.


you're welcome Setya[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 27, 2004 10:52 am 
Beginner
Beginner

Joined: Mon Aug 16, 2004 6:15 am
Posts: 24
Setya,
In your categories mapping file, tell it to use a proxy class. What hibernate will do here is make many-to-one relationships lazy. You won't actually retrieve the related category until you have a line of code that accesses a property of category other than the id.

However, this does have the downside of making the relationship always lazy by default, but this can be overriden by using HQL and specifying a fetch clause, so with some forethought you actually get the best of both worlds :)


Top
 Profile  
 
 Post subject: Please help about these tables relationship
PostPosted: Sun Aug 29, 2004 10:32 pm 
Beginner
Beginner

Joined: Wed Aug 25, 2004 10:27 pm
Posts: 21
Location: Indonesia
CSwinney wrote:
Setya,
In your categories mapping file, tell it to use a proxy class. What hibernate will do here is make many-to-one relationships lazy. You won't actually retrieve the related category until you have a line of code that accesses a property of category other than the id.

However, this does have the downside of making the relationship always lazy by default, but this can be overriden by using HQL and specifying a fetch clause, so with some forethought you actually get the best of both worlds :)


CSwinney,

Thank you for your suggestion.

Actually I plan to use Hibernate in EJB environment in which the List returned by Hibernate will be accessed by client applications. In this case the property of Category will be accessed when the list is already passed to the client. Is is possible at all to apply lazy relationship in this kind of environment ?

Regards,


Setya


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