-->
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: Fetching associated object only once.
PostPosted: Thu Apr 17, 2008 10:00 am 
Newbie

Joined: Thu Apr 17, 2008 9:29 am
Posts: 2
I have a scenario, in which I want to load my associated object only once for many other objects. For Example,

I have following kind of code in my hbm file.

<class name="Account" table="Account" lazy="false"
polymorphism="explicit">

<composite-id name="key" class="AccountCompositePKey">
<key-property name="id" column="id"></key-property>
<key-property name="isSavingAccount"></key-property>
</composite-id>
<property name="accountId" />
<property name="accountName" />
.
.
.
<many-to-one name="user"
class="User" lazy="false"
column="createdBy"
not-null="true"
update="false"
insert="false"
unique="true"
outer-join="true"
property-ref="alias" />

</class>


In above example code, when ever Multiple accounts are fetched, then for every account a user object is loaded, even though user is same for all accounts. Meaning, for each account query hit, associated user is fetched.

What I want is to load user only once if he has more than one account in his name. For example, if user 1 has Accounts A, B, C, D in his name - then user 1 details should be loaded only once, even though all 4 accounts details are loaded.

Any one who has faced such issue and has found a solution, please advice me in achieving this.

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 18, 2008 1:19 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
In that case, you should fetch the user first and get the accounts for the particular user. You could use a collection of accounts in the user entity. So everytime you load a single user, you will load all the accounts associated.

But if you say you have to fetch the accounts and then the user, then you cannot do it. There will be multible DB hits for the user.

_________________
Sukirtha


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 18, 2008 5:20 am 
Newbie

Joined: Thu Apr 17, 2008 9:29 am
Posts: 2
I appreciate your response. It was a good food for thought. But, Unfortunately, it does not resolves my problem.

The design is such that, I am searching for all accounts in my application. Now, each account has a "createdBy" field, which is basically references to User name.

When I put no search criteria, then due to many-to-one association in the above code example, each account record fetch will associate a user record with it. I desire do something that fetches user record only once for all the account records having reference to that user. No matter, if any other method of association mapping requires to be used in the hbm file.

This is basically a performance issue.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 18, 2008 9:00 am 
Beginner
Beginner

Joined: Tue Feb 26, 2008 2:04 pm
Posts: 28
Location: UK
rkc wrote:
I appreciate your response. It was a good food for thought. But, Unfortunately, it does not resolves my problem.

The design is such that, I am searching for all accounts in my application. Now, each account has a "createdBy" field, which is basically references to User name.

When I put no search criteria, then due to many-to-one association in the above code example, each account record fetch will associate a user record with it. I desire do something that fetches user record only once for all the account records having reference to that user. No matter, if any other method of association mapping requires to be used in the hbm file.

This is basically a performance issue.


hi,

you could try implementing a second level cache strategy somehow although I'm not really sure how this can be applied to a many-to-one association.


cheers

_________________
savakos


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.