ashwinsakthi
ashwinsakthi

Reputation: 1956

Get DB column names from annotated bean in java

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;
}

Upvotes: 0

Views: 1757

Answers (2)

Stefan
Stefan

Reputation: 12462

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() + "!");
        }
     }
 }

Upvotes: 1

StanislavL
StanislavL

Reputation: 57421

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.

Upvotes: 2

Related Questions