I've found an interesting case where Lazy Loading is causing a property to be set to null where it really shouldn't be. I've found an "acceptable" solution, but I'm wondering if theres a better way around what Im doing.
Heres the case:
I'm using Struts/Spring/hibernate, so this is a web-app.
I have a Quote, which has an object property of type User.
I also have an Order, which also has a User property.
I have a user name stored in the session.
If I run the following:
Code:
Quote quote = quoteService.findByID(quoteId);
User user = userDao.findById(getLoggedInUserId);
order.setUser(user);
I get an SQL error saying that I cannot insert null into the order tables user column.
However, if I just change the order of the statements like so:
Code:
User user = userDao.findById(getLoggedInUserId);
Quote quote = quoteService.findByID(quoteId);
order.setUser(user);
everything works fine.
After some debugging, I figured out that in the first case, the userDao.findById method was returning a user object with all of it's properties set to null, but in the second, everything was filled in. Why does this happen? The quote that I happened to be looking at was for the logged in user, and quote fetches the user through lazy loading. So, when I get to the userDao.findById, it just hits the cached, proxy object and returns it. However, when I then try and set that proxied object as the property of another object, it still has all of it's null values when I get to the insert.
I have to figure that this is a somewhat typical use case, so my question is, is there a way to have hibernate load those properties when it comes time to insert? Or is there a better way to be doing something like this altogether?
Thanks,
Tim