-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: [HV] What if bean's hashCode() uses lazy-loaded property?
PostPosted: Tue Aug 03, 2010 11:40 am 
Newbie

Joined: Tue Aug 03, 2010 11:01 am
Posts: 2
Hello,
Recently We've ran into a well-known "collection [...] was not processed by flush()" issue with default Hibernate Validator event listener (org.hibernate.cfg.beanvalidation.BeanValidationEventListener) and validator (org.hibernate.validator.engine.ValidatorImpl) implementations.
See following related posts for more details on the issue itself and why it appears:
http://opensource.atlassian.com/project ... wse/HV-246
http://opensource.atlassian.com/project ... e/HHH-2763
(Just for the record, our environment is Hibernate 3.5.3-Final, Spring 3.0.3.RELEASE, Hibernate Validator 4.1.0.Final)

Having debugged the application with Validator sources attached and hibernate.show_sql=true I've managed to track down the place where validator has been referencing lazily-initialized objects.
It turned out that method org.hibernate.validator.engine.resolver.SingleThreadCachedTraversableResolver.buildHashCode(); uses traversed object's hashCode() method, making assumption that hashCode does not depend on lazily-initialized fields.
Code:
   public int buildHashCode() {
      int result = traversableObject != null ? traversableObject.hashCode() : 0; // here's the "assumption" place
                ...
      return result;
   }

And of-course our problem was that child object used lazily-initialized parent to build its hash-code. This was done because in most cases we do not use parent object && hash code. And when we use hash code the parent object is always loaded. Well, until now :)

The question is the following: is it the problem in our design which should be changed to eagerly fetch the parent object (if yes then why) or it's a use-case overlook in HV we should try out some workarounds like they suggest in the tickets mentioned above (E.g. extend the BeanValidationEventListener and having access to current session, clear the collection of unprocessed objects which additionally appeared after the validation took place)?

Regards,
Roman


Top
 Profile  
 
 Post subject: Re: [HV] What if bean's hashCode() uses lazy-loaded property?
PostPosted: Wed Aug 04, 2010 4:52 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
The question is the following: is it the problem in our design which should be changed to eagerly fetch the parent object (if yes then why) or it's a use-case overlook in HV we should try out some

It might be both:
I'm not an expert on this specification so your issue might be rejected, but it makes sense to discuss it so please open a JIRA issue.

But I'm sure that it's bad-practice to define your equals/hashcode including the value of other beans, you should always avoid that and consider what you're defining. It might even make sense to not define the equals/hashcode at all - this really depends on your requirements and not on the used frameworks.

Also consider that your defined equals/hashcode won't work in other cases too if it's not "attached" - kinda weird requirement - or worse yet the performance of these methods might be awful, especially if you insert it into java.util.collections containers.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: [HV] What if bean's hashCode() uses lazy-loaded property?
PostPosted: Wed Aug 04, 2010 12:08 pm 
Newbie

Joined: Tue Aug 03, 2010 11:01 am
Posts: 2
Hello Sanne,

Thanks for the response. I did create Jira ticket HV-356 as you've requested.
Even though I do agree that it is rather bad practice than a good one, it was convenient for us to use hashCode() and it was appropriate to use lazy properties in the hashCode() because it was always called within spring-managed hibernate session. Until the introduction of validator framework.

I do feel that this issue will rather be considered an end-usage problem. What I would ask though is to provide some (ideas for) workarounds that could be used in the meantime before major code review for changing e.g. from lazy to eager fetching in the future.

Regards,
Roman


Top
 Profile  
 
 Post subject: Re: [HV] What if bean's hashCode() uses lazy-loaded property?
PostPosted: Wed Aug 04, 2010 6:02 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
Even though I do agree that it is rather bad practice than a good one, it was convenient for us to use hashCode() and it was appropriate to use lazy properties in the hashCode() because it was always called within spring-managed hibernate session.

just to put this straight: I don't mean to say that it's bad practice to use hashCode(), but that this shouldn't include references to lazy fields - it's not hard to remove them and doubt that any code could meaningful rely on this lazy value to be considered.

Also, I'm not sure about why using it "within spring-managed hibernate session" has you to require an hashCode at all.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.