-4

I fetch an object using reflection as following. Here the field I fetched is mapped by hibernate Lazy loading.

final Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
Object obj = field.get(instance);

Then I use the resulting object as follows

obj.getClass().getDeclaredFields()

But this does not return me all the fields which is defined in the Object. This Object can be any of the entity in my JPA entities list. I believe there is a problem with using Object instead of casted object.

How can I do this, and get all the fields of this new found object.

dinesh707
  • 10,694
  • 20
  • 74
  • 121
  • I'm not sure who down voted this. But I would like to know the explanation. May be i can provide more info.. For more clarity I dont know what type it should be casted into. The class type is there in field but its dynamic – dinesh707 Jun 11 '14 at 12:21
  • Are you looking for a declared field on a superclass instead of one on the current actual class? http://stackoverflow.com/a/16966699/504685 – Charlie Jun 11 '14 at 12:22
  • 1
    Please show a short but complete program demonstrating the problem. (And no, this will have nothing to do with casting.) – Jon Skeet Jun 11 '14 at 12:22
  • Give us a sample that we can reproduce. In your code you'll find that `obj.getClass()` does *not* refer to `Object` but it will indeed have the type of the field. – Jeroen Vannevel Jun 11 '14 at 12:22
  • i added explenation of what i'm trying to do – dinesh707 Jun 11 '14 at 12:26
  • 1
    You're just repeating yourself and ignoring our request. Show the actual code, not an explanation of what you want your code to do. – Jeroen Vannevel Jun 11 '14 at 12:27
  • ok. code is added too' – dinesh707 Jun 11 '14 at 12:31
  • Seems like my question is still unclear for people. Thank you for the time – dinesh707 Jun 11 '14 at 12:51
  • Are you sure the field you are looking for exists on the actual class of the object and not a superclass? Have you inspected with a debugger that the object you're calling getDeclaredFields on is not in fact a generated surrogate that extends the class where the field you think should exist is actually declared? – Charlie Jun 11 '14 at 13:06
  • Ok. i found the problem. The object fetched is pointing to Hibernate reference. not to the actual object because that field is a mapped with Lazy fetch. Thankx all – dinesh707 Jun 11 '14 at 13:08
  • 1
    A lesson to learn the hard way: using reflection or the instanceof operator in combination with Hibernate entities is not a reliable thing to do as you may just run into a proxy class. I felt lots of pain in that area until I discovered this SO question: http://stackoverflow.com/questions/2216547/converting-hibernate-proxy-to-real-object – Gimby Jun 11 '14 at 14:02

1 Answers1

1

Problem simply is Reflection and Hibernate does not work with easy steps.

the instance object in code is a fetched object by hibernate (Lazy loading). That Object does not return a real java object, but a hibernate proxy object. Calling Reflection on proxy object will give totally unexpected results than what you looking for.

In comments https://stackoverflow.com/users/424903/gimby pointed to a solution which can be used to un-proxy the hibernate lazy loaded object. Converting Hibernate proxy to real object

Community
  • 1
  • 1
dinesh707
  • 10,694
  • 20
  • 74
  • 121