-->
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.  [ 1 post ] 
Author Message
 Post subject: Do not initialize proxy when calling my overriden hashCode
PostPosted: Thu Feb 21, 2008 5:47 am 
Regular
Regular

Joined: Thu Oct 13, 2005 4:19 am
Posts: 98
I've implemented a custom hashCode (and equals) method based on the id of my persistent object.
This hashCode method works, even if it's a proxy.

Code:
    @Override
    public int hashCode()
    {
        // getClass() is not hashbaar
        return new HashCodeBuilder()
                .append(getId())
                .toHashCode();
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o)
        {
            return true;
        }
        if (o instanceof JpaIdentifiable)
        {
            JpaIdentifiable other = (JpaIdentifiable) o;
            if (getId() == null || other.getId() == null)
            {
                return false;
            }
            return getId().equals(other.getId())
                    && isSameClassIgnoringProxySubclass(other);
        } else {
            return false;
        }
    }


The problem is, that hibernate insists on intializing this object anyway when hashCode() is called. At that point it's detached in my swing client and I get a LazyInitializationException of course.

From the hibernate manual:

Code:
Certain operations do not require proxy initialization

    *

      equals(), if the persistent class does not override equals()
    *

      hashCode(), if the persistent class does not override hashCode()
    *

      The identifier getter method

Hibernate will detect persistent classes that override equals() or hashCode().


Can I tell hibernate "yes I 've overridden the hashCode() method, but I still don't want you to require proxy initialization for it"?

Thanks for any and all help :)

_________________
http://www.ohloh.net/accounts/ge0ffrey


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

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.