[英]Hibernate session.load() not working with Java reflection getDeclaredFields?
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). 我正在运行一个遍历声明字段的函数,查找对象的2个实例之间的差异并将其输出(用于审计跟踪)。 However, if I use an object loading from the hibernate session, IE: 但是,如果我使用从休眠会话中加载的对象,则IE:
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. 该对象的声明字段来自:default_interceptor,handler,_filter,methods,如果我在不使用session.load的情况下加载相同类型的对象,它将发现ACTUAL声明字段很好。 If I run a getClass().toString() on this hs object, it returns: 如果在此hs对象上运行getClass()。toString(),它将返回:
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. 据我所知,javaasssist_1的问题出在哪里,由于某种原因,它没有找到实际的类。
Any idea what I can do? 知道我能做什么吗?
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 . session.load(HazardSubmission.class, 1)
将首先检查在当前session中是否可以找到ID为1的HazardSubmission.class
类型的实例。 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
代理是通过对HazardSubmission.class
进行子类化而动态创建的,它们不是HazardSubmission.class
,这就是为什么返回实例上的getDeclaredFields()
不是HazardSubmission.class
的实际Field
的HazardSubmission.class
To get the actual Class
from the generated proxy instance , you can use Hibernate.getClass() 要从生成的代理实例获取实际的Class
,可以使用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());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.