-->
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: getID() returns null for CGLIB proxy
PostPosted: Wed Nov 16, 2005 2:48 pm 
Newbie

Joined: Thu Jan 06, 2005 1:48 pm
Posts: 3
Hibernate version:
3.0.5

I have a many-to-one mapping for an object B that is associated with A. I load A, and B loads as a dynamic proxy. Great. However, when I call B.getId(), I get null. I can call B.getSomeOtherRandomProperty() and I get the value back fine.

Is getId() special for dynamically generated proxies? How would I get at this attribute?

In a related note, I have looked all over for the difference between lazy="proxy" and lazy="true", and I can't find a clear explanation. I do notice that if I mark my above many-to-one as lazy="true" my getId() problem goes away. But I would rather not do this without understanding why.

Chris


Top
 Profile  
 
 Post subject: Re: getID() returns null for CGLIB proxy
PostPosted: Mon Mar 21, 2011 4:48 pm 
Newbie

Joined: Mon Mar 21, 2011 4:35 pm
Posts: 1
Well, I hope you found a solution for your problem, given that it's been 6 years ;)

I had a similar problem, and your post came up near the top of the list when I googled "getId returns null hibernate" so I figured I'd post my findings in case it helps someone else searching on the same terms.

I was seeing the exact same issue with Hibernate 3.6.0.Final and JPA 2.0 - I had one entity derived from a superclass (marked with an @MappedSuperclass annotation). In that superclass, I had the id field declared like this:

Code:
@Id
@Basic(optional = false)
@Column(name = "objectid")
private Long id;

And then a getter with the following signature:

Code:
public final Long getId()

In my case, the problem was the final modifier. When the entity in question is a proxy, the getId() method cannot be overridden by the proxy's implementation. The proxy has a field called id (with value set to null) and so getId() returned null. For the entity's other properties, my getters were not declared final, and so the proxy's reflection black magic could work properly.

This behavior was sporadic - only certain test cases would cause a proxy to be instantiated, so tracking down the issue took some time.

I would consider this to be a case of silent failure. It would be nice if Hibernate would throw some kind of warning or exception on startup, such as "Getters for @Id fields cannot (should not?) be final".

At any rate, I hope this helps someone...


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.