Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get column name of property mapped with Hibernate

How can I access the Hibernate mapping of my model to find out the column name of a property?

The column name is not specified in the mapping so Hibernate generates it automatically - I would like to create a native SQL statement including this column name.

like image 600
Thomas Einwaller Avatar asked Jan 07 '10 21:01

Thomas Einwaller


People also ask

How are properties of a class mapped to the columns of a database table in hibernate?

In hibernate, the preffered way to do this is by annotations. Annotations are piece of code that eases our work in mapping classes with tables, and methods with coulmns. Also, rows of tables to the Objects of the classes.

Which provides mapping information in hibernate?

Though many Hibernate users choose to write the XML by hand, but a number of tools exist to generate the mapping document. These include XDoclet, Middlegen and AndroMDA for the advanced Hibernate users.


2 Answers

Thanks to Jherico I found out how to do that:

((Column) sessionFactoryBean.getConfiguration().getClassMapping(Person.class.getName())
        .getProperty("myProperty").getColumnIterator().next()).getName();
like image 137
Thomas Einwaller Avatar answered Oct 04 '22 16:10

Thomas Einwaller


((AbstractEntityPersister) sessionFactory.getClassMetadata(o.getClass()))
    .getPropertyColumnNames(property)[0];
like image 34
B T Avatar answered Oct 04 '22 17:10

B T