-->
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.  [ 6 posts ] 
Author Message
 Post subject: Performance questions - Efficient way to compare a set
PostPosted: Fri Oct 15, 2004 4:08 am 
Beginner
Beginner

Joined: Fri Oct 01, 2004 7:13 am
Posts: 20
Hibernate version:2.1.3

I have a set of contacts ids for a user which I need to compare with those contacts belonging to a user.

Currently I am obtaining the Contact object based on the id, and then

User.addContact( Contact )

Is there an easy was to just add the id, and not have the search for the Contact object first?

Effectively I only need to add those in the passed set of id's that do not exist in the db?

Sure someone has come across this issue in Hibernate before.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 15, 2004 11:55 am 
Regular
Regular

Joined: Tue Sep 28, 2004 6:34 pm
Posts: 50
Did you try to use "lazy" on Contact class?
In one method/session obtain the Contact object and add to user.

(this is just idea - I know nothing about your model)

Lukasz


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 18, 2004 4:28 am 
Beginner
Beginner

Joined: Fri Oct 01, 2004 7:13 am
Posts: 20
Thanks Lukasz,

We are using lazy on Contacts for users, but I am not quite sure what you mean.

To explain a little more.

For example we may be given 5 contact ids ( 123, 134, 145, 156, 167) that belong to a group.

We have in the db the Contacts that are a member of the group, but here we are effectively syncronising the sets. So lets say that Contact 145 does not exist in the group in the db.

We need an efficient way of syncing the group members in the db, and those passed.

Currently the most efficient way I can see is to loop through the contactids passed, then check if they exist as members in the db. Then here comes the bit that I would like to improve on..

I then have to search for the Contact using the id to obtain the Contact object then

group.addContact( contact );

Is there a way of achieving this in a more efficient way?

Is there a way to avoid seaching and instantiating the Contact as all I want to do it create the relationship to the group, and I know all the Contacts are already in the db, the only bit I do not know at this point is whether they are a group member or not?

Hope that is clearer - anyone have some experience of similar situations or some advice?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 18, 2004 1:30 pm 
Regular
Regular

Joined: Tue Sep 28, 2004 6:34 pm
Posts: 50
A few things:

cam292 wrote:
For example we may be given 5 contact ids ( 123, 134, 145, 156, 167) that belong to a group.
...
Currently the most efficient way I can see is to loop through the contactids passed, then check if they exist as members in the db.

one improvement here - you can use sql "in" clause like:
Query q = session.createQuery(....
" WHERE contact_whatever.id in (:id_list)"
q.setParameterList("id_list", ids);


Quote:
I then have to search for the Contact using the id to obtain the Contact object then

group.addContact( contact );

Is there a way of achieving this in a more efficient way?

Is there a way to avoid seaching and instantiating the Contact as all I want to do it create the relationship to the group, and I know all the Contacts are already in the db, the only bit I do not know at this point is whether they are a group member or not?



Quote from http://www.hibernate.org/118.html#A19:
How can I create an association to an entity without fetching that entity from the database (if I know the identifier)?
If the entity is proxyable (lazy="true"), simply use load(). The following code does not result in any SELECT statement:

Code:
Item itemProxy = (Item) session.load(Item.class, itemId);
Bid bid = new Bid(user, amount, itemProxy);
session.save(bid);


One thing that you have to remember (and for sure will not like) is that:
hibernate always initializes a collection when you want to add or remove an element:
http://www.hibernate.org/117.html#A9


And following this idea if this will be a huge performance issue you can refractor the code and handle relations in persistence layer by creating the 'relation object'. I know some people will not like it


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 18, 2004 8:27 pm 
Regular
Regular

Joined: Tue Sep 28, 2004 6:34 pm
Posts: 50
BTW. Check also one of the recent topics about meny-to-meny:
http://forum.hibernate.org/viewtopic.ph ... 61#2219161

and link from above topic (this has example of association object 'UtilisateurRole'):
http://forum.hibernate.org/viewtopic.php?t=933543

Lukasz


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 25, 2004 6:38 am 
Beginner
Beginner

Joined: Fri Oct 01, 2004 7:13 am
Posts: 20
Thanks for your help.

I'll run some tests and see which way is best for me.

Thanks


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