Issue with serializing Hibernate objects using XStream

前端 未结 6 1509
南笙
南笙 2021-02-04 19:52

I\'ve been facing this issue where, the hibernate objects on serialization produces unexpect xmls containing all the instrumented code from Hibernate.

We did some cleani

6条回答
  •  -上瘾入骨i
    2021-02-04 20:27

    I've came up with somewhat sufficient solution. In my application, only PersistentSets were messing up XML generated by XStream. So I have added another Converter to XStream (which runs with open Hibernate Session and live objects):

    XStream xs = new XStream();
    xs.registerConverter(new CollectionConverter(xs.getMapper()) {
        @Override
        public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
            org.hibernate.collection.PersistentSet ps = (PersistentSet) source;
            super.marshal(new HashSet(ps), writer, context);
        }
    
        @Override
        public boolean canConvert(Class type) {
            return type.isAssignableFrom(org.hibernate.collection.PersistentSet.class);
        }
    }, XStream.PRIORITY_VERY_HIGH);
    String s = xs.toXML(processInstance);
    

    The serialized XML looks like below:

      
        
          813017
          
            
            GMT
          
          process.log.action-performed
          Wydrukuj wniosek
          GENERATE_APPLICATION
          PERFORM_ACTION
          
          
          
        
        
          808211
          
            
            GMT
          
          process.log.action-performed
          Zaakceptuj
          ACCEPT
          PERFORM_ACTION
          
          
          
        
    

    In my case, the class attribute was not important, so I have ignored its value. You can of course tinker with it.

提交回复
热议问题