Hibernate objects and Java reflection
I am lazily loading an object from hibernate using the following line of code
Object object = session.load(Class.forName(javaType), objec开发者_高级运维tId);
If I print object.getClass().getSimpleName()
it displays the name of the hibernate proxy and not my implementation class as expected.
However If I then use reflection on this proxy object to return an attribute called children as follows:
List children = (List) object.getClass().getMethod("getChildren", new Class[ {}).invoke(object, new Object[]{});
It is returning a list of objects where object.getClass().getSimpleName()
on any of the objects returns the implementation class name. Why are these objects not returning the proxy object name?
The list itself can be lazy-loaded, but if you enumerate the elements, an sql query is required and there would be no performance gain to create a proxy for each element (on the contrary).
精彩评论