java - How do I use reflection to get data out of a hibernate object? -
here example of issue having using reflection. simple case, need dynamically build method name on fly... simple case can not 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(); (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 exception: java.lang.nosuchmethodexception: client1.getclienttype(int)
the method getmethod receives param classes, not return type, getclienttype
receive int?
if not, try:
cdata.getclass().getmethod("getclienttype");
Comments
Post a Comment