File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Reading All the records from a table + Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Reading All the records from a table + Hibernate" Watch "Reading All the records from a table + Hibernate" New topic
Author

Reading All the records from a table + Hibernate

Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 757
Hi i wanted to read all the records from a table and store it in a List.

I wrote the bellow code for the same



where as i was expecting only


Why rest of the select queries are getting fired. ???


Jigar Naik


Francois Nadeau
Ranch Hand

Joined: Apr 09, 2009
Posts: 46
Not sure, but why are you using a transaction if all you are doing is querying the database?
Also, you probably want to release your Session after using it (unless you are only using one I guess).




[url]http://FrancoisNadeau.com[/url]
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 757
Hi Francois

i tried without transaction but i got bellow stack trace...

Francois Nadeau
Ranch Hand

Joined: Apr 09, 2009
Posts: 46
Hold on a minute, I think I miss read your log.

Does your Customer Object have a relationship with licensemanager.module?

If so what it is, and how did you define it?


Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 757
Yes there is a relationship between Customer and Module

module.customer_id is a foreign key holding reference to customer.id

Customer POJO



Module POJO

Francois Nadeau
Ranch Hand

Joined: Apr 09, 2009
Posts: 46
Ok, now I understand.

The extra calls are loading the Module information when you are getting the customer info.

You have two options if that is not what you want:

1. Load the Modules lazily

This will delay loading the Module info until you need it. This may increases performance if you don't always need the info.

2. Use a Join


Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 757
Hi Francois ,

Thans a lot.. it worked...

FetchType.LAZY i got...

but i am not able to understand



What it does ?
Francois Nadeau
Ranch Hand

Joined: Apr 09, 2009
Posts: 46
Hi Jigar,

Sorry, about that. The JoinColumn will perform an SQL join with the Module table when you load the Customer data. So that will allow Hibernate to load the data for both of the classes with a single select query.

Be careful with the Lazy load though. You will get an LazyInitializationException if you need the information inside the Module, but no longer are within the scope of the session that you are using.

For example:



Should not work.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading All the records from a table + Hibernate