2010-01-07 58 views

回答

11

感谢Jherico我发现了如何做到这一点:

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

什么是“sessionFactoryBean”?我确定它不是sessionFactory。 – instanceOfObject 2014-02-06 12:10:15

+0

它可以是sessionFactory或SessionFactoryBean,它取决于版本 – Shilan 2016-03-29 14:26:45

1

您必须有权访问Hibernate配置对象。

+0

应该在春天可能环境...... – 2010-01-07 21:22:56

1

这将检索一个层次的复合材料和常规属性映射:

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(); 
} 
3
((AbstractEntityPersister) sessionFactory.getClassMetadata(o.getClass())) 
    .getPropertyColumnNames(property)[0]; 
+0

在'o'是代理的情况下,您应该使用'Hibernate.getClass(o)'而不是'o.getClass()'。 – 2014-06-24 19:50:48