Hi,
I am trying to extract id from a Hibernate object without actually knowing the exact type of the object. I found no Hibernate or Spring API so far to give me the identifier of an object.
At the moment I inspect the annotations to find out which field/method is declared with @Id, like this:
Code:
for (Method m : entityClass.getDeclaredMethods()) {
if (m.getAnnotation(Id.class) != null) {
log.debug("id is defined by:" + m.getName());
}
}
Knowing the method name I can invoke it on the domain object to extract the identifier from it.
However, I feel there must be a better way - any ideas?
Thanks,
Dusan
Code: