Currently, we are using Hibernate 4.3 and we are planning to upgrade to Hibernate 5.6. In our current implementation with Hibernate 4.3, we don’t use the XML or annotation based approach to create the mappings, but do it dynamically by using the classes e.g. org.hibernate.mapping.Column,org.hibernate.mapping.PrimaryKey, org.hibernate.mapping.Property, org.hibernate.mapping.RootClass, org.hibernate.mapping.SimpleValue,org.hibernate.mapping.Table etc.
In Hibernate 5.6, the SimpleValue and RootClass constructor have been changed to accept the instance of org.hibernate.boot.spi.MetadataBuildingContext. I would like to know if we could get the org.hibernate.boot.spi.MetadataBuildingContext to pass to the required constructors mentioned above.
Here is a code snippet with Hibernate 4.3
public void configureMetaModel(Mappings mappings) {
RootClass rclass = new RootClass();
rclass.setClassName(getClassName());
rclass.setDynamicUpdate(true);
rclass.setEntityName(getOriginalBOName());
rclass.setJpaEntityName(getOriginalBOName());
table = new Table();
//
addColumn(getBOID(), DATATYPE_STRING_JTYPE, true, "boID", false);
rclass.setTable(table);
mappings.addClass(rclass);
}
First of all, constructing the model by hand is not necessarily considered “supported”, so if you have trouble updating the Hibernate ORM version because of using these APIs in an unsupported way, then you’re on your own.
Apart from that, Hibernate ORM 5 only receives limited support, so unless you’re a Red Hat customer, you’ll have to figure out yourself how you deal with any issues that you encounter.
Having said that, take a look at how the annotation processing does things and try to match that in your code if you really want to go down this path. You should be able to construct a MetadataBuildingContextRootImpl yourself and inject that into all your mapping objects.
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
MetadataBuildingOptionsImpl buildingOptions = new MetadataBuildingOptionsImpl(serviceRegistry);
BootstrapContextImpl bootstrapContext = new BootstrapContextImpl(serviceRegistry, buildingOptions);
buildingOptions.setBootstrapContext(bootstrapContext);
InFlightMetadataCollectorImpl metadataCollector = new InFlightMetadataCollectorImpl(bootstrapContext, buildingOptions);
MetadataBuildingContext metadataBuildingContext = new MetadataBuildingContextRootImpl(bootstrapContext, buildingOptions,
metadataCollector);
/*
* Modify metadataCollector here
*/
MetadataImpl metadataInstance = metadataCollector.buildMetadataInstance(metadataBuildingContext);
return metadataInstance.getSessionFactoryBuilder().build();
Thanks for your prompt reply. I hope this what you are referring to. We do have a mix approach where 90% mapping are created by hand as mentioned in the first post and 10% using xml. I am not sure we could achieve to build both types of mappings if we build the session factory and MetadataBuildingContextRootImpl instance as mentioned above as we are not using the MetadataSources instance.