Search code examples
javaannotationsspring-annotations

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

Solution

  • 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.