【问题标题】:how to find no of attributes in an entity class如何在实体类中查找没有属性
【发布时间】:2019-05-31 12:30:18
【问题描述】:
 public int getColumnCount() {
            return getClass().getDeclaredFields().length;
        }
    this is the code which return no of attributes of same class,but not anaother class

在我的服务类中,我想知道一个属性的数量 特定的实体类。

我尝试过一种方法,它告诉 同一个类。但我想知道另一个类的属性。

你能帮我达到要求吗?

【问题讨论】:

  • 将该方法添加到其他类中。然后你可以在需要的地方调用它。
  • 嗨,罗西,感谢您在我的问题上花费宝贵的时间。您的建议绝对正确。但我有很多实体类,所以在所有实体类中编写相同的方法,有什么方法可以接受任何实体类并且不返回任何属性?
  • getClass()Object 定义,因此您只需要引用任何类的实例即可。我只需在您的方法签名中添加一个 Class<?> entityClass 参数,并在我调用它时传入适当的类对象

标签: java spring hibernate rest


【解决方案1】:

非常感谢 jonK,它帮助了我。

here is the code.
public int getAttributes() {
        EmployeeDO employeeDo=new EmployeeDO();
        Class<?> e=employeeDo.getClass();
        return getAllAttributes(e);
    }

    private int getAllAttributes(Class<?> entityClass) {

        return entityClass.getDeclaredFields().length;
    }

【讨论】:

    【解决方案2】:

    这是对前一种方法的修改。

    private int getAllAttributes(Object entityClass) {      
            return entityClass.getClass().getDeclaredFields().length;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多