Java Hibernate OpenSession in View Avoid fetch on Controller

177 Views Asked by At

we have a APP in Hibernate lately we start using Open Session in View in our DAO we fetch the data we really need.. we dont close the session but later in our controller in any operation on the Entity Hibernate is fetching the Data from the DB i know this behavior is the main reason to use open session but i dont need the fetch is some cases.. i was wondering if i can tell hibernate not fetch the data in some cases....

student.getSchool().getTeachers()

in this case i have load all the data i need from this 3 entitys but hibernate starts to load the school and the teachers again..

thanks a lot

1

There are 1 best solutions below

0
On

Three main options:

  1. If you use EH-Cache, you won't have to wait while Hibernate queries the database for Student & School again.
  2. Or you could keep the form & Hibernate Session in the HTTP Session, which also achieves caching.
  3. For AJAX or similar requests fetching just the Teachers, you could change the Criteria to Projection or use a Hibernate Query, to "project" or retrieve just the target entity.. at the database level, doing a joined or sub-expression query. Student would be loaded but only as a proxy, in this case.