I am trying to migrate from hibernate 4 to hibernate 5.
My xml mapping contains dynamic-component tags.
We were initializing the configuration of these tags during the start of the application by adding elements to the XML DOM. The idea is to read a list of columns from the database to configure a list of extra columns.
But the dynamic-component tag seems to have disappeared from the hibernate 5 documentation.
Can I still use it ?
What could be a good replacement ?
The semantics of a mapping are identical to . The advantage of this kind of mapping is the ability to determine the actual properties of the bean at deployment time just by editing the mapping document. Runtime manipulation of the mapping document is also possible, using a DOM parser. You can also access, and change, Hibernate’s configuration-time metamodel via the Configuration object.
The idea is to be able to add some extra attributes (java types) to standard objects. Each of our customer can customize standard class by adding columns to standard table and declaring these extra columns in a meta-data table.
These meta-data are injected in the xml mapping.
This allows customization without touching the code and only adding columns and defining them in the database.
public class PContact implements java.io.Serializable {
/** storage for id : contact identifier */
private Integer id;
/** storage for externalId : ID from external datasource */
private String externalId;
/** storage for firstName : first name */
private String firstName;
/** storage for lastName : last name */
private String lastName;
/** storage for additionalAttributes */
private Map additionalAttributes;
...
/**
* getter for additionalAttributes
*/
public Map getAdditionalAttributes() {
return this.additionalAttributes;
}
/**
* setter for additionalAttributes
*/
public void setAdditionalAttributes(Map argadditionalAttributes) {
this.additionalAttributes = argadditionalAttributes;
}
}
And finally an extract of the code altering the xml mapping during application start :
InputStream xmlInputStream = classLoader.getResourceAsStream(mappingFile);
Document doc = xmlHelper.createSAXReader(errorHandler, entityResolver).read(new InputSource(xmlInputStream));
// iterate through child elements of root
for (Iterator<Element> itr = root.elementIterator("class"); itr.hasNext();) {
Element classElt = itr.next();
for (Iterator<Element> itr2 = classElt.elementIterator("dynamic-component"); itr2.hasNext();) {
Element dynElt = itr2.next();
String dname = null;
for (Iterator<Attribute> itrC = dynElt.attributeIterator(); itrC.hasNext();) {
Attribute attribute = itrC.next();
if ("name".equals(attribute.getName())) {
dname = attribute.getValue();
break;
}
}
if ("additionalAttributes".equals(dname)) {
res = stmt.executeQuery("SELECT PARAMETERNAME,JAVATYPE,SQLVALUE FROM XXX");
while (res.next()) {
String className = res.getString(1);
String type = res.getString(2);
String sql = res.getString(3);
dynElt.addElement("property").addAttribute("name", className).addAttribute("column", sql)
.addAttribute("type", fixSqlType(type));
}
res.close();
}
}
}
HbmBinder.bindRoot(new XmlDocumentImpl(doc, new OriginImpl("type", "name")), cfg.createMappings(), Collections.emptyMap(),Collections.EMPTY_SET);
The Glarch entity mapping uses dynamic-component and the test works just fine.
So, you can still use it. In 6.0 or 7.0, HBM will be changed so that we can provide an extension to the JPA orm.xml. However, meanwhile, this mapping should work.