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.