-->
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.  [ 2 posts ] 
Author Message
 Post subject: Identifier method initializes proxy if getId in base class
PostPosted: Tue Mar 01, 2011 10:35 pm 
Newbie

Joined: Tue Mar 01, 2011 10:04 pm
Posts: 1
I've come across an issue in which proxies are initialized when the getId method is called. I'm wondering if this is a bug, improper use, (or maybe a combination). So I'm using annotations on the getters; so everything is property-access rather than field. I have a base class where I define the id, version, getters/setters etc. Then there are concrete classes that define their own fields, relying on the base for the id. Something like this:

Code:
@MappedSuperclass
class AbstractBean {
  ...
  @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(nullable = false, updatable = false)
  public Long getId() {
    return id;
  }
}

@Entity
class RealBean extends AbstractBean {
  ...
}

I'm trying to simply get the id of an uninitialized proxy, but I keep getting LazyInitializationExceptions. Debugging, I can see that the BasicLazyInitializer invoke method is not getting the fact that it has the id already. It should be returning the cached id value after passing this check in BasicLazyInitializer.invoke():84:

Code:
  else if ( isUninitialized() && method.equals(getIdentifierMethod) ) {
    return getIdentifier();
  }

The problem is that the method and getIdentifierMethod are not equal. The method is: Long AbstractBean.getId(), while getIdentifierMethod is: Serializable RealBean.getId(). Note the different return types. So it definitely looks like Hibernate is wrong about what the getId method is supposed to be on the concrete class.

So what is this? Do I have to repeat my id declarations in all of my concrete classes? Thanks.


Top
 Profile  
 
 Post subject: Re: Identifier method initializes proxy if getId in base class
PostPosted: Thu Aug 18, 2011 11:31 pm 
Newbie

Joined: Sat Nov 06, 2010 4:21 pm
Posts: 3
I think you're right that the different return value in the method signature is preventing Hibernate from recognizing it as your identifier getter. I had a similar problem and worked around by creating a utility method like this:

Code:
    public static Serializable getIdentifier(Object object) {
        if (!(object instanceof HibernateProxy) || Hibernate.isInitialized(object)) {
            return ((AbstractBean)object).getId();
        }

        HibernateProxy proxy = (HibernateProxy)object;
        LazyInitializer initializer = proxy.getHibernateLazyInitializer();
        return initializer.getIdentifier();
    }


(This would be a useful utility for the Hibernate class, but there doesn't seem to be one currently.)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.