aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate:What 'criteria.add(Restrictions' should be added to remove getting the list of'empl_notes' Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate:What Watch "Hibernate:What New topic
Author

Hibernate:What 'criteria.add(Restrictions' should be added to remove getting the list of'empl_notes'

Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
Hi all,

I'm playing with Hibernate and found it to be very easy (and potentially will save me a lot of SQLs)

Say I have the followings columns:

1. department
2. Employee
3. Employee_notes

Employee has 1 department_ID and 0..MANY notes.
When I get a list of departments


I get all information related to the employees (email...) AND THEIR NOTES.

Question: while this is excellent and save me a lot of work writing SQL statements, it's 'expensive'. What 'criteria.add(Restrictions' should I add in order to avoid getting the list of all notes.

Thank you!
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

So, in the mappings, the one to many or many to many relationship will be configured, by default, to be lazy. In fact, the associated objects will not actually be returned.

If you do make a call to the getter to get the associated many objects, Hibernate will then go and grab the data, but it won't if the data is not requested.

Close the Hibernate Session and try to access the many objects. You should get a Hibernate LazyInitializationException, as the collection bag has not yet been set.

That should be how it works. Let me know if you're seeing otherwise.

Regards,

-Cameron McKenzie
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755

When I do this:


it doesn't return all logs...GREAT!

But what if I wish to keep the option in other events/instances? can I do something with the criteria.add(Restrictions ???


BTW, I ordered your book - it had excellent reviews on amazon!
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

>>But what if I wish to keep the option in other events/instances?

So, sometimes you want it lazy loaded, other times you don't? What you do is make up your stinkin' mind!

The opposite of Lazy is Eagar, so you can set it to eagar loading, but that will do it all the time. So, with a single mapping, you need to do it one way or the other.

I usually go through a service layer, and then a DAO. If I need a collection eagarly loaded, in the service layer, I call the getter for the collection after I have accessed the central object through the DAO. So, a given service will load eagarly, and another service will load in a lazy manner.

I think that's what you're getting at.

Thanks for picking up a copy of my book on Hibernate. It's always humbling when someone puts a bit of faith in me and picks up one of my titles.

-Cameron McKenzie
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate:What 'criteria.add(Restrictions' should be added to remove getting the list of'empl_notes'