Get physical column value with entity property value using hibernate

10,295

Solution 1

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 = a.name();

Solution 2

I would try to avoid this by any means, but if you're really sure you'll need it, use:

Configuration configuration = sessionFactory.getConfiguration();
PersistentClass persistentClass = configuration
                .getClassMapping(T.class.getName());
String columnName = ((Column) persistentClass.getProperty("testId")
                .getColumnIterator().next()).getName();

See also Get table column names in Hibernate

Share:
10,295
instanceOfObject
Author by

instanceOfObject

Just a learner!!! #SOreadytohelp

Updated on June 14, 2022

Comments

  • instanceOfObject
    instanceOfObject almost 2 years

    I have a table T with columns defined as usual.

    @Entity
    @Table(name="T")
    public class T {
    @Column(name="test_id")
    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.

    • kostja
      kostja over 10 years
      could you please clarify your requirements? Do you want to rename the field in your code?
    • instanceOfObject
      instanceOfObject over 10 years
      No, I don't want to rename. I just want to fetch db column_name corresponding to entity columnName.
    • Adrian Shum
      Adrian Shum over 10 years
      Just curious: why would you want to do that?
    • instanceOfObject
      instanceOfObject over 10 years
      I have got these logical values from HQL and want to store physical column name in the audit to keep track of the changes made on that column.
    • jean
      jean over 5 years
      It seems meaningless. Because you still have to provide a string which is the name of your field instead string of column name
  • instanceOfObject
    instanceOfObject over 10 years
    I understand that HQL works on entities. What I want is to get DB column name from entity property (get test_id from testId). I have read that Hibernate Configurations could be used for this but not quite sure of the way.
  • kostja
    kostja over 10 years
    So you want to extract the name attribute of the Column annotation, you could use reflection for that. Or am I misunderstanding you again?
  • instanceOfObject
    instanceOfObject over 10 years
    Correct! Reflection is one way to do this :)