Using metadata in Hibernate by R4R Team

Hibernate exposes metadata via the ClassMetadata and CollectionMetadata interfaces and the Type hierarchy. Instances of the metadata interfaces can be obtained from the SessionFactory.

Student stud = ......;
ClassMetadata studentMeta = sessionfactory.getClassMetadata(Student.class);

Object[] propertyValues = studentMeta.getPropertyValues(stud);
String[] propertyNames = studentMeta.getPropertyNames();
Type[] propertyTypes = studentMeta.getPropertyTypes();

// we can get a Map of all properties which are not collections or associations
Map namedValues = new HashMap();
for ( int i=0; i<propertyNames.length; i++ ) 
    if ( !propertyTypes[i].isEntityType() && !propertyTypes[i].isCollectionType() ) 
        namedValues.put( propertyNames[i], propertyValues[i] );

Hibernate requires a rich meta-level model of all entity and value types. So This model can be useful to the application itself. For example, the application might use Hibernate's metadata to implement a "smart" deep-copy algorithm that understands which objects should be copied ( mutable value types) and which objects that should not ( immutable value types and, possibly, associated entities).
Leave a Comment:
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!