Forums Register Login

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

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 


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?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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" ?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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
Hug your destiny! And hug this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 3851 times.
Similar Threads
Fetch only one column using hibernate 3.0
Using Struts2 to xml format Hibernate objects
Parent-Child Confusion ....
How to lazily load references when the object is detached from session ?
hibernate mapping problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 01:44:11.