This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes (Hibernate) lazy loading even when no association between classes ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "(Hibernate) lazy loading even when no association between classes ?" Watch "(Hibernate) lazy loading even when no association between classes ?" New topic
Author

(Hibernate) lazy loading even when no association between classes ?

Raj Ohadi
Ranch Hand

Joined: Jun 30, 2006
Posts: 316
I got a hibernate error and it shows something related to lazy loading. By simply adding "lazy=false" in th hibernate mapping file the problem is fixed. But I am surprised ---

In my mini example I have only two tables. One of them includes a foreign key refering to the other table. My tables are like

Department(id, chairman_name, address)
Person(name, phone)

where "chairman_name" is FK to "Person" table.

My POJO classes are like

class Department {
Long id;
String chairman_name;
String address;
}

class Person {
String name;
String phone;
}

Remember, I am NOT creating a "Department" class like

class Department {
Long id;
Person p;
String address;
}

So I think my two POJO classes should NOT involve any lazy loading issue, simply because when it loads my "Department" object, the object does not include "Person" object in it (it only has a "name" attribute), why did I still get lazy loading type error ? and when I disabled lazy loading the error message disapear ?

Thank you.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


I got a hibernate error and it shows something related to lazy loading. By simply adding "lazy=false" in th hibernate mapping file the problem is fixed.

What was the error message?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Are you using load or get when pulling the object from the database.

The bottom line is that the property is being accessed outside of a transaction. Why are you committing the transaction before you access the property? When you access the property, isn't it important that it's properly synchronized with the database, and you're not pulling out potentially bad data? Mabye, maybe not. But is it something you've thought about?

Keep the lazy loading as true, but when you access the object's properties, access them from within the transactional context. That will rid you of Hibernate's nasty LazyLoadingException.

-Cameron McKenzie
Raj Ohadi
Ranch Hand

Joined: Jun 30, 2006
Posts: 316
Sorry I missed sending the error message --

tuple.PojoEntityTuplizer:173 - could not create proxy factory

org.hibernate.HibernateException: CGLIB Enhancement failed:

proxy.BasicLazyInitializer:130 - CGLIB Enhancement failed

Secondly, for my case, what problem I may get by setting "lazy=false" ?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Only problem you will see with lazy="false" is that it will always be loaded, even in use cases where that data isn't needed, hence bringing down more data than the use case requires, which is a waste.

Anyway, what Cameron said would be my approach too.

So the Name field still comes from the Person table, so Hibernate still has to query two tables, so I would say it still needs the association to load the data, hence your lazy loading exceptions.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 375
Originally posted by Mark Spritzler:
Only problem you will see with lazy="false" is that it will always be loaded, even in use cases where that data isn't needed, hence bringing down more data than the use case requires, which is a waste.

Anyway, what Cameron said would be my approach too.

So the Name field still comes from the Person table, so Hibernate still has to query two tables, so I would say it still needs the association to load the data, hence your lazy loading exceptions.

Mark


Thanks Mark. bear with me for a stupid question ---


Does this lazy load go the reversed way ? i.e. if I am trying to use hibernate to retrieve "Person" object an dits attributes (see my above original tables of "department" and "person"), will it also use lazy load to load "department " objects ?

I really hope it does NOT.

Could you help verify with me ?

Thank you
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

No, if your query or loading of person does not require any fields from department, then it will not load the department record, unless you declare that association as eager.

See in department you said it also gets the person's name. So where is the name stored, well in the Person table, so Hibernate has to go to the Person table to get the name, hence going to two tables.

Mark
 
 
subject: (Hibernate) lazy loading even when no association between classes ?