How to read Hibernate mapping
I need to know which physical column is associated to a persistent's attribute.
e.g.
Class LDocLine has this attribute
private Integer lineNumber;
which is mapped in hibernate like this :
<property name="lineNumber" column="LINENUMBER" type="integer"/>
The method I need is something like :
getColumn("LDocLine","lineNumber) => "LINENUMBER"
开发者_如何学JAVAI assume its existence internally, but not sure if it's in the public api.
Thanks in advance
Do you have access to Configuration
object you've used to build your session factory? If so, you can use the following:
Value v = configuration.getClassMapping(entityName).getProperty(propertyName).getValue();
for (Iterator it = v.getColumnIterator(); it.hasNext(); ) {
Column column = (Column) it.next();
column.getName(); // or .getQuotedName() or bunch of other useful stuff
}
Column documentation.
If you don't have access to configuration, you can obtain column data from SessionFactory
instance, however in this case you're technically no longer using public API as you'll have to class cast to internal implementations:
AbstractEntityPersister persister = (AbstractEntityPersister) sessionFactory.getClassMetadata(entityName);
String[] columnNames = persister.getPropertyColumnNames(propertyName);
In both cases entityName
is the name of your entity (its class name unless explicitly overridden)
As you mentioned in your reply, you are not having access to 'Configuration' object.In case you are having access to hibernate 'Session' object, then following code may be helpful to you.
Collection clsMetaData = session.getSessionFactory()
.getAllClassMetadata().values();
for (Iterator i = clsMetaData.iterator(); i.hasNext();) {
ClassMetadata cmd = (ClassMetadata) i.next();
System.out.println("cmd" + cmd.getEntityName());
for (String s : cmd.getPropertyNames()) {
System.out.println("prop:" + s);
}
}
In this way you can get details about Class metadata information.
This is something that Hibernate is not generally used for as you do not need to refer to column names in order to retreive objects through HQL or Criteria.
Why do you need this functionality?
精彩评论