Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>>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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic