Thursday, July 19, 2007

Persist XMLBeans using Hibernate

I'm currently working on a project where we need to persist xmlbean objects using hibernate (well - we may choose to do so to avoid having to map from xmlbeans to POJO beans since they are both, ahem BEANS! ). This hibernate Interceptor will handle instanciation and mappings of XMLBeans

public class MyHibernateInterceptor
extends EmptyInterceptor
implements Interceptor
protected SessionFactory factory;

public MyHibernateInterceptor(SessionFactory factory) {
this.factory = factory;

public String getEntityName(Object object)
Class[] interfaces = object.getClass().getInterfaces();
for (int i=0;i
if (XmlObject.class.isAssignableFrom(interfaces[i])) {
return interfaces[i].getName();
return super.getEntityName(object);

public Object instantiate(String entityName, EntityMode entityMode, Serializable id)
if (EntityMode.POJO.equals(entityMode)) {
// Only load POJO style
try {
Class clazz = Class.forName(entityName);
if (XmlObject.class.isAssignableFrom(clazz)) {
Class factoryClazz = Class.forName(entityName+"$Factory");
Method newInstanceMethod = factoryClazz.getMethod("newInstance", null);
Object obj = newInstanceMethod.invoke(null, null);
setIdentity(entityName, obj, id, entityMode);
return obj;
} catch (Exception e) {
// Nothing to do, but to let hibernate deal with it
return super.instantiate(entityName, entityMode, id);

protected void setIdentity(String entityName, Object obj, Serializable id, EntityMode entityMode)
ClassMetadata classMetaData = factory.getClassMetadata(entityName);
classMetaData.setIdentifier(obj, id, entityMode);


And this is how I configure the lot

cfg = new Configuration().configure("hibernate.cfg.xml");
cfg.setInterceptor(new MyHibernateInterceptor(cfg.buildSessionFactory()));



Blogger Brian said...

Lars, I am curious...have you had good success with persisting XMLBeans using Hibernate this way? I recently joined a project that is keeping two data models (synchronizing between XMLBeans and POJO) and I'd really like to get rid of it! Any road blocks you ran into with this method that I should be aware of before taking the plunge?


27/6/08 23:11  
Blogger lborupj said...

Hi Brian,

Unfortunately, forces higher up the hierarchy desided we had to implement this using Hibernate tools and generated POJO beans, so we do have a mapping/converting layer between our XMLBeans and Hibernate (POJO) Beans.
But for my proof of concept I did manage to persist a rather large graph persisting XMLBeans directly.

28/6/08 17:35  
Blogger Pattabi said...

This comment has been removed by the author.

20/11/08 13:05  
Blogger Julio Arg├╝ello said...

This post was very helpful!

Since we use not only entities but also components, more classes are needed (i.e.: XmlBeansComponentTuplizer)

For persisting a single graph of object everything works fine, however:

How did you solve the problem with values returned by getters are recreated again and again causing Hibernate to almost get lazy!

Your help is very appreciated, thanks indeed!!!

(PD: Anyway, I can share my code)

29/10/12 07:28  

Post a Comment

<< Home