Search code examples
spring-bootjpaspring-data-jpajpa-2.1

How to get column name from JPA


My code:

empDet emp = repository.findByActiveFlag("Y");

Here empDet is a entity with 3 columns:

  1. empid
  2. empName
  3. ActiveFlag

Table with 10 rows so i will use foreach

emp.forEach(e-> {
---mycode---
---and here i got values--

});

Now my question is how to get Column name from Jpa or foreach loop


Solution

  • If you have object of Entity class (empDet) in JPA then from that object you can get the all properties of that class.

    As you know in JPA class represent table in database and properties represent columns of that table.

    if you have an object of empDet e.i emp

    empDet emp=repository.findByActiveFlag("Y");
    
    Field[] members = emp.getClass().getDeclaredFields();
            for(Field member:members){
                System.out.println(member.getName());
            }