No one answer me. I find a solution myself:
Hibernate Configuration object does not provide API for remove mappings. We need write a new Configureation extends from Hibernate Configuration, in order to add remove mapping method.
Please refer to the HibernateConfiguration class in our POC project for detail. Here are remove mapping method:
Code:
public void removeMapping(String path) throws MappingException {
log.info( "Remove mappings from resource: " + path );
PersistentClass cls = this.getClassMapping(path);
Table tbl = cls.getRootTable();
try {
super.classes.remove(path); //Remove mapped class
super.tables.remove(tbl.getName()); //Remove mapped table
super.imports.remove(path); //Remove mapping from import entry.
super.imports.remove(cls.getNodeName()); //Remove mapping from import entry.
}catch (MappingException me) {
throw new MappingException( "Could not remove mappings from resource: " + path, me );
}
}
Please notes that there are 2 import entries for each mapping, one’s key is the mapping’s full class name, such as “com.report.PO”, the other is class name, such as “PO”.
This method can work well to simple mappings. But if you want support following mappings, please investigate the method and add more process:
(1) One mapping related to multiple tables.
(2) The mapping include collections.
(3) The mapping include sub classes.