Problem with serializing Hibernate objects using XStream

I ran into this problem when hibernating objects during serialization cause unexpected xmls containing all the tool code from Hibernate.

We did some cleanup of the object before serializing the object.

But is there a standard option for serializing an object directly?

+5
source share
6 answers

I have not used XStream before, but I have serialized objects managed by Hibernate. This is not fun.

There are two big problems:

  • lazy loading;
  • One-to-many relationships.

- . - " ", (: Set<T>), Hibernate own (unserializable!). , Hibernate .

( ), :

  • , , ;
  • Hibernate ( , );
  • , , ArrayList, HashSet HashMap ( ).

, 2 - , Hibernate ...

: @cliff.meyers , : , (: , ).

+3

. PersistentSets XML, XStream. XStream ( Hibernate ):

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);

XML :

  <processLogs class="org.hibernate.collection.PersistentSet">
    <pl.net.bluesoft.rnd.processtool.model.ProcessInstanceLog>
      <id>813017</id>
      <entryDate>
        <time>1310832421216</time>
        <timezone>GMT</timezone>
      </entryDate>
      <eventI18NKey>process.log.action-performed</eventI18NKey>
      <additionalInfo>Wydrukuj wniosek</additionalInfo>
      <logValue>GENERATE_APPLICATION</logValue>
      <logType>PERFORM_ACTION</logType>
      <state reference="../../../definition/states/pl.net.bluesoft.rnd.processtool.model.config.ProcessStateConfiguration[8]"/>
      <processInstance reference="../../.."/>
      <user reference="../../../creator"/>
    </pl.net.bluesoft.rnd.processtool.model.ProcessInstanceLog>
    <pl.net.bluesoft.rnd.processtool.model.ProcessInstanceLog>
      <id>808211</id>
      <entryDate>
        <time>1310828206169</time>
        <timezone>GMT</timezone>
      </entryDate>
      <eventI18NKey>process.log.action-performed</eventI18NKey>
      <additionalInfo>Zaakceptuj</additionalInfo>
      <logValue>ACCEPT</logValue>
      <logType>PERFORM_ACTION</logType>
      <state reference="../../../definition/states/pl.net.bluesoft.rnd.processtool.model.config.ProcessStateConfiguration[4]"/>
      <processInstance reference="../../.."/>
      <user reference="../../../creator"/>
    </pl.net.bluesoft.rnd.processtool.model.ProcessInstanceLog>

class , . , , .

+3

XStream / Hibernate. , XStream .

+3

Codehaus JIRA ( ):

http://jira.codehaus.org/browse/XSTR-226

(Axis 1, Blaze DS ..). , , , , , "", ; "n + 1 selects", !:) , XStream- , . FlushMode.MANUAL , , Hibernate, - . , .

+1

, xstream-for-beans ():

, XStream :

  • , . XStream, , , proprerties getter/setter.
  • : .
  • "" -.

XStream Converter , , . xstream-for-beans , .

Terracotta Pojoizer , , .

+1

All Articles