简体   繁体   English

休眠对象和Java反射

[英]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), objectId); 我正在使用以下代码从休眠状态懒惰地加载Object object = session.load(Class.forName(javaType), objectId);

If I print object.getClass().getSimpleName() it displays the name of the hibernate proxy and not my implementation class as expected. 如果我打印object.getClass().getSimpleName()它将显示休眠代理的名称,而不是预期的实现类。

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[]{}); 但是,如果我然后在此代理对象上使用反射来返回称为children的属性,如下所示: 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. 它返回一个对象列表,其中任何对象上的object.getClass().getSimpleName()返回实现类名称。 Why are these objects not returning the proxy object name? 为什么这些对象不返回代理对象名称?

列表本身可以延迟加载,但是如果枚举元素,则需要执行sql查询,并且为每个元素创建代理都不会提高性能(相反)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM