0

In the below bean where the annotation is mapped to a DB column , how to get the values in a string.

I need all values like below

"TY_CD,ACTION_CD,ID"

public class SampleBO implements SQLData{

    @DbColumnMap(columnName = "TY_CD")
    private String typeCode;

    @DbColumnMap(columnName = "ACTION_CD")
    private String actionCd;

    @DbColumnMap(columnName = "ID")
    private String id;
}
ashwinsakthi
  • 1,856
  • 4
  • 27
  • 56

2 Answers2

2

You can get list of the class' fields

SampleBO.class.getFields()

(An array of Field is returned) and go through the array to check whether it has the annotation. See the example If the annotation present you can get value of the annotation's columnName.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
1

Here is a quick example. Notice that I searched for the "name" of a JPA "column" annotation in an "Address" class here:

 public static void main(String[] args) {
     Field[] fields = Address.class.getDeclaredFields();
     for (Field field : fields) {
        Column col = field.getAnnotation(Column.class);
        if (col != null) {
            System.out.println(col.name() + "!");
        }
     }
 }
Stefan
  • 12,108
  • 5
  • 47
  • 66