Normally this is what you do to create your session factory:
Code:
org.hibernate.cfg.Configuration cfg = new org.hibernate.cfg.Configuration( );
cfg.configure( ); // Looks for hibernate.cfg.xml in classpath
org.hibernate.SessionFactory factory = cfg.buildSessionFactory( );
Now, both the SessionFactory and the Configuration object hold information about your mappings. The information in SessionFactory is immutable and really hard to get to, but it is always accurate. The information in the Configuration object is mutable, and as such should not be trusted. However, the Configuration object is probably your best bet:
Example using the SessionFactory object:
Code:
org.hibernate.metadata.ClassMetaData cmd = factory.getClassMetadata( SomeDomainObject.class );
String idName = cmd.getIdentifierPropertyName( );
org.hibernate.type.Type idType = cmd.getIdentifierType( );
I don't think that's what you want, especially if you have composite keys. Here is an equivalent example using the Configuration object:
Code:
org.hibernate.mapping.PersistentClass pc = cfg.getClassMapping( SomeDomainObject.class.getName( ) );
org.hibernate.mapping.Property id = pc.getIdentifierProperty( );
String idPropertyName = id.getName( );
java.util.Iterator idColIter = id.getColumnIterator( );
while( idColIter.hasNext( ) )
{
org.hibernate.mapping.Column idCol = (Column)idColIter.next( );
String name = idCol.getName( );
...
}
java.util.Iterator subclassIter = pc.getSubclassIterator( );
while( subclassIter.hasNext( ) )
{
org.hibernate.mapping.Subclass subclass = (Subclass)subclassIter.next( );
org.hibernate.mapping.KeyValue key = subclass.getKey( );
java.util.Iterator keyColIter = key.getColumnIterator( );
while( keyColIter.hasNext( ) )
{
org.hibernate.mapping.Column keyCol = (Column)keyColIter.next( );
String keyColName = keyCol.getName( );
...
}
}
And so on. The API documentation is really helpful:
http://www.hibernate.org/hib_docs/v3/api/
- Jesse