Java 获取与 Hibernate 映射的属性的列名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2023700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get column name of property mapped with Hibernate
提问by Thomas Einwaller
How can I access the Hibernate mapping of my model to find out the column name of a property?
如何访问我的模型的 Hibernate 映射以找出属性的列名?
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.
映射中未指定列名,因此 Hibernate 会自动生成它 - 我想创建一个包含此列名的本机 SQL 语句。
采纳答案by Thomas Einwaller
Thanks to Jherico I found out how to do that:
感谢 Jherico,我发现了如何做到这一点:
((Column) sessionFactoryBean.getConfiguration().getClassMapping(Person.class.getName())
.getProperty("myProperty").getColumnIterator().next()).getName();
回答by Jherico
You have to have access to the Hibernate Configuration object.
您必须有权访问 Hibernate Configuration 对象。
回答by Thomas Jung
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();
}
回答by B T
((AbstractEntityPersister) sessionFactory.getClassMetadata(o.getClass()))
.getPropertyColumnNames(property)[0];