In short,
I want to do something like
MyObject myObject;
public String getField (String fieldName) {
return myObject.fieldName; // how could I do this since fieldName is a String?
}
Background:
I am getting data from the database using a stored procedure.
The stored procedure basically gets all the columns. But I want the user to choose which column to display in the table.
In the Hibernate object, I have all the fields corresponding to the resultset returned by the stored procedure.
With the list of fields (Strings) that the user wants, is there a way to display the value of the corresponding field in the Hibernate object given the field name?
You can access it using reflection:
In your case, you’d just use:
Here’s a little test of the code:
Output: