0

I am running a function that loops through the declared fields, finds the difference between 2 instances of an object and outputs it (for audit trails). However, if I use an object loading from the hibernate session, I.E:

HazardSubmission hs = (HazardSubmission)s.load(HazardSubmission.class, id);

The declared fields of that object come out at: default_interceptor,handler,_filter,methods where if I load an object of the same type without using session.load it finds the ACTUAL declared fields fine. If I run a getClass().toString() on this hs object, it returns:

class nz.co.g.hs.stripes.model.HazardSubmission_$$_javassist_1

Where as far as I can tell javaasssist_1 is the problem, for some reason it's not finding the actual class.

Any idea what I can do?

williamsandonz
  • 13,581
  • 21
  • 88
  • 171

1 Answers1

3

session.load(HazardSubmission.class, 1) will first check if the instance of the type HazardSubmission.class with the ID 1 can be found in the current session . If yes , that instance is returned . Otherwise , a proxy will be returned.

Proxies are created dynamically by sub-classing HazardSubmission.class .They are not HazardSubmission.class and that 's why getDeclaredFields() on the returned instance are not the actual Field of the HazardSubmission.class

To get the actual Class from the generated proxy instance , you can use Hibernate.getClass()

HazardSubmission hs = (HazardSubmission)s.load(HazardSubmission.class, id);

System.out.println(Hibernate.getClass(hs).toString());
for (Field field : Hibernate.getClass(hs).getDeclaredFields()) {
    System.out.println(field .toString());
}
Ken Chan
  • 64,456
  • 22
  • 117
  • 138