开发者

Get column name of property mapped with Hibernate

开发者 https://www.devze.com 2022-12-15 22:46 出处:网络
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 t

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.


Thanks to Jherico I found out how to do that:

((Column) sessionFactoryBean.getConfiguration().getClassMapping(Person.class.getName())
        .getProperty("myProperty").getColumnIterator().next()).getName();


((AbstractEntityPersister) sessionFactory.getClassMetadata(o.getClass()))
    .getPropertyColumnNames(property)[0];


You have to have access to the Hibernate Configuration object.


This will retrieve one-level composites and normal property mappings:

String columnName(String name) {
    PersistentClass mapping = configuration.getClassMapping(ExtendedPerson.class.getName());
    Property property = mapping.getProperty(name);
    if(property.isComposite()){
        Component comp = (Component) property.getValue();
        property = comp.getProperty(StringHelper.unroot(name));
        assert ! property.isComposite(); //go only one level down 
    }
    Iterator<?> columnIterator = property.getColumnIterator();
    Column col = (Column) columnIterator.next();
    assert ! columnIterator.hasNext();
    return col.getName();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号