mardi 14 avril 2015

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)






Aucun commentaire:

Enregistrer un commentaire