简体   繁体   English

如何使用反射从休眠对象中获取数据?

[英]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)

The method getMethod receives the param classes, not the return type, your getClientType receive a int? 方法getMethod接收参数类,而不是返回类型,您的getClientType接收int吗?

If not, try: 如果没有,请尝试:

cData.getClass().getMethod("getClientType");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将反射拉出此循环? - How do I pull reflection out of this loop? Spring Data JPA + Hibernate:如何使用抽象超类上的存储库进行对象检索? - Spring Data JPA + Hibernate: How do I use a repository on abstract super class for object retrieval? 如何从HashMap中获取对象? - How do I get Object out of HashMap? 如何让Jersey显示Hibernate保留的对象中的列表? - How do I get Jersey to show a List in an object persisted by Hibernate? 如何使用Hibernate获取数据库数据 - How Do I get the DataBase Data using Hibernate 如何确定应用程序中的特定对象已不再使用? - How do I figure out that a particular object in an application is no longer in use? 我需要防止使用反射来从超类获取私有的Properties字段。 如何使用Java安全管理器执行此操作 - I need to prevent the use of reflection to get a private Properties field from a superclass. How can I use the Java security manager to do this Android-如何从选定的微调器对象中获取值? - Android - How do I get the value out of the selected spinner object? 如何对通过反射创建的对象进行类型转换以运行方法? - How do I typecast an object created through reflection to run a method? 如何使用反射从Field / Class中获取Object? - How can I get the Object from a Field/Class using reflection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM