-->
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: Pattern/Best Practice
PostPosted: Wed Jul 12, 2006 3:49 pm 
Newbie

Joined: Thu Jan 12, 2006 6:02 pm
Posts: 9
Hibernate version:

3.1.1

Mapping documents:

Annotations

Code between sessionFactory.openSession() and session.close():

N/A

Full stack trace of any exception that occurs:

N/A

Name and version of the database you are using:

MySQL 5.0.18

The generated SQL (show_sql=true):

N/A

Debug level Hibernate log excerpt:

N/A


I am looking for a pattern/best practice for my application, which I'm sure is fairly common.

I have a number of entities (Person, Location, Customer) that have a set of Contacts. I currently have a one-to-many relationship (Cascade.ALL) from the parent to the Contact with a join table (lazy load). While I perhaps shouldn't know/care about the application, the application deals with the parent entities on different screens than the contact. Thus, when loading the parent object, we don't really need to load all of the contacts (thus, lazy load).

However, when I add a new contact, I need to load the parent from the database (parent's can be huge) and then add the new contact to the list. This is fine. However, when I am just updating the parent entity, I need to also force a load of the child contacts before calling update or all of my contacts "disappear" (I assume because of the Cascade.ALL).

Is this the correct way to model this and correct way to perform the child Contact object updates and inserts?

TIA,

Jim


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 11:36 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If you do your update of the parent in the same session as it was loaded, so you still have a valid, proxied set of contacts, then you shouldn't have to force a load of the contacts. Hibernate should know that the set isn't dirty, and thus it shouldn't try to update it.

I don't think that this works for bags (which have to be fully updated every time, due to hibernate not being able to figure out if an in-memory entity corresponds to a specific row in the DB).

Also note that upgrading your hibernate may help in this area, as there is a new lazy value "extra" that can apply to collections. Not entirely sure what it does (I'm using 3.1.1 too), but I believe that it might be appropriate for entities with multiple large or very large collections.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 13, 2006 12:40 pm 
Newbie

Joined: Thu Jan 12, 2006 6:02 pm
Posts: 9
Unfortunately, the parent entities are currently detached, so I need to force the load of the contacts.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 13, 2006 7:07 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Then I think that you're doing it right already. You might be a little better off if you don't reattach the existing detached object, and instead find the same object in your current session, using the detached object's ID. However, that's only useful if the object has few properties, otherwise copying the modified properties between objects becomes a pain.

_________________
Code tags are your friend. Know them and use them.


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.