In my Java program I want to marshal my Hibernate persistent objects to XML.
For this i am using JAXB. The problem is when marshalling the object i got a NullPointerException
.
This is caused by the lazy load fetching strategy. When switching to eager fetching the marshaling process works.
I use the getter instead of the variable so Hibernate can initialize the object. I got no LazyInitializationException
so there is no closed session etc.
Pseudocode:
Session s = sessionFactory.openSession();
Criteria crit = s.createCriteria(Entity.class);
List list = crit.list();
Entity entity = (Entity) list.get(0)
try {
DocumentResult dr = new DocumentResult();
context = JAXBContext.newInstance(entity.getclass());
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(entity, dr);
}
catch(JAXBException e) {
// ignore
}