Hibernate: Query Metadata

2019-09-09 08:11发布

问题:

I'm currently working on a solution to automatically generate ExtJS forms based on a hibernate mapping (I use hibernate reverse engineering for that based on @Annotations).

Hibernate has a

getPropertiesInterator() 

http://docs.jboss.org/hibernate/core/3.5/api/org/hibernate/mapping/PersistentClass.html#getPropertyIterator%28%29

funciton for that. Which is accessible like this

((LocalSessionFactoryBean)sessionFactory).getConfiguration().getClassMapping(<Classname>.class.getName())

Which lists the properties of the table. I use this information to generate ExtJS inputs out of it

varchar => input text => textarea etc.

My question is, if there is maybe another (maybe better) way to get this information out of my hibernate configuration?

Regards

JS

回答1:

I ended up using

ClassMetadata hibernateMetadata = session.getSessionFactory().getClassMetadata(fullClassName);

To get the MetaData either by [String] ClassName or by Class.class

With this information you can do something like this...

String[] propertyNames = hibernateMetadata.getPropertyNames();
Type[] propertyTypes = hibernateMetadata.getPropertyTypes();

And pars this information later on.

See

http://docs.jboss.org/hibernate/core/3.5/api/org/hibernate/SessionFactory.html#getClassMetadata%28java.lang.String%29

for more information.

Hibernate has even an example for this in their docs...

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/objectstate.html#objectstate-metadata