Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
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
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
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic