[英]How do I use reflection to get data out of a hibernate object?
Here is an example of an issue I am having using reflection. 这是我使用反射的一个问题示例。 This is a simple case, but what I eventually need is to dynamically build the method name on the fly... but even this simple case I can not get to work!
这是一个简单的案例,但是我最终需要的是动态地动态构建方法名称...但是即使是这个简单的案例,我也无法工作!
Client1 cData = (Client1) session.get(Client1.class, 1);
int cType = cData.getClientType();
int cType2 = -1;
Method method[] = null;
Method getCTypeMethod = null;
try {
method = cData.getClass().getMethods();
for (Method m : method){
System.out.println(m.getName()); // displays getClientType
}
getCTypeMethod = cData.getClass().getMethod("getClientType", int.class);
if (getCTypeMethod != null){
cType2 = (int) getCTypeMethod.invoke(cData, int.class);
}
} catch (Exception e) {
e.printStackTrace();
}
assertEquals(cType, cType2);
The line: 该行:
getCTypeMethod = cData.getClass().getMethod("getClientType", int.class);
Always throws an exception: java.lang.NoSuchMethodException: Client1.getClientType(int) 总是抛出异常:java.lang.NoSuchMethodException:Client1.getClientType(int)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.