I'm currently struggling with the lack of documentation too.
There are 2 different approaches I use:
- adding xml elements to the Configuration (like Envers). I used that for additional class mappings. You can take the hibernate xml configuration documentation as documentation. It basically looks something like this:
DOMWriter writer = new DOMWriter();
Document document = DocumentHelper.createDocument();
Element hibernate_mapping = document.addElement("hibernate-mapping");
hibernate_mapping.addAttribute("auto-import", "false");
Element class_mapping = hibernate_mapping.addElement("class");
class_mapping.addAttribute("entity-name", versionName);
class_mapping.addAttribute("table", clazz.getTable().getName() + "_version");
Element id_mapping = class_mapping.addElement("id");
id_mapping.addAttribute("name","id");
id_mapping.addAttribute("type","long");
id_mapping.addAttribute("column","id");
Element id_generator = id_mapping.addElement("generator");
id_generator.addAttribute("class","native");
Element main_mapping = class_mapping.addElement("many-to-one");
main_mapping.addAttribute("name", "main");
main_mapping.addAttribute("column", "main_id");
// main_mapping.addAttribute("class", clazz.getEntityName());
main_mapping.addAttribute("lazy", "false");
main_mapping.addAttribute("entity-name", clazz.getEntityName());
conf.addDocument(writer.write(document));
conf.buildMappings();
- adding stuff to PersistentClass and adding Collection mappings. I use that for adding a Relation to an already existing Class Mapping. Since I could not find any documentation or examples for this, I just made a sample entity with a similar relation and copied the structure I saw when debugging. The code I have (just testing it myself) looks something like this:
PersistentClass versionClass = conf.getClassMapping(versionName);
Property versionsRel = new Property();
Set set = new Set(clazz);
OneToMany one2Many = new OneToMany(clazz);
one2Many.setReferencedEntityName(versionName);
one2Many.setAssociatedClass(versionClass);
set.setCollectionTable(versionClass.getTable());
SimpleValue mainPropertyValue = new SimpleValue(clazz.getTable());
mainPropertyValue.addColumn(new Column("main_id"));
mainPropertyValue.setTypeName("long");
DependantValue dependantValue = new DependantValue(versionClass.getTable(),mainPropertyValue);
dependantValue.addColumn(new Column("main_id"));
Properties idGenProperties = new Properties();
idGenProperties.setProperty("target table", clazz.getTable().getName());
idGenProperties.setProperty("target column","id");
dependantValue.setIdentifierGeneratorProperties(idGenProperties);
set.setKey(dependantValue);
set.setRole(clazz.getEntityName() + ".versions");
set.setFetchMode(FetchMode.SELECT);
set.setLazy(false);
set.setElement(one2Many);
set.setInverse(true);
set.setNodeName("versions");
set.setOwner(clazz);
versionsRel.setValue(set);
versionsRel.setInsertable(false);
versionsRel.setName("versions");
versionsRel.setNodeName("versions");
versionsRel.setPropertyAccessorName("my special thing");
versionsRel.setLazy(false);
clazz.addProperty(versionsRel);
conf.createMappings().addCollection(set);
maybe this is a little inspiration to you.
Dirk
|