Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading All the records from a table + Hibernate

 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. ???
 
Francois Nadeau
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).



 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Francois

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

 
Francois Nadeau
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic