instanceOfObject instanceOfObject - 10 months ago 65
SQL Question

Get physical column value with entity property value using hibernate

I have a table T with columns defined as usual.

public class T {
private Long testId;


Given entity property "testId", I want to get corresponding DB column name (i.e. "test_id"). How could it be achieved?

Edit 1:
I want to keep this column at separate location with actual DB column name (test_id) than testId. I fetched these values from DB using HQL which have key as entity name (i.e. testId) and I want actual column name in DB.

Answer Source

If I understood your requirement correctly, you want to use HQL while having a consistent name for both DB column and the entity field, like this:

SELECT t.test_id FROM Test t 

instead of

SELECT t.testId FROM Test t 

There is only one way to do that - renaming the field to test_id. HQL works on entities, not on DB tables, so you must use proper field names in the query.

Since test_id contradicts the usual Java coding conventions, I would advise against it.

EDIT: Getting the annotation attribute value with reflection would work along this outline:

Field field = MyEntity.class.getDeclaredField("testId");
Column a = field.getAnnotation(Column.class);
String columnName =;