[英]How do I use reflection to get data out of a hibernate object?
這是我使用反射的一個問題示例。 這是一個簡單的案例,但是我最終需要的是動態地動態構建方法名稱...但是即使是這個簡單的案例,我也無法工作!
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);
該行:
getCTypeMethod = cData.getClass().getMethod("getClientType", int.class);
總是拋出異常:java.lang.NoSuchMethodException:Client1.getClientType(int)
方法getMethod接收參數類,而不是返回類型,您的getClientType
接收int嗎?
如果沒有,請嘗試:
cData.getClass().getMethod("getClientType");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.