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