1

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
}
Tom Anderson
  • 46,189
  • 17
  • 92
  • 133
  • I have absolutely no idea what is being asked here. Voting to close. – Tom Anderson Mar 02 '12 at 12:27
  • 2
    or you can ask for more information. Strange to vote for closing – Iggy Van Der Wielen Mar 02 '12 at 12:54
  • There is nowhere near enough context in your question for it to be useful to anyone else reading it. If you want to fix that, great, and i will be more than happy to rescind my vote (and even try to answer it, if i can!), but in its present state, i just don't think it's a useful addition to StackOverflow. Sorry. – Tom Anderson Mar 02 '12 at 13:00
  • ok is this more clear for you? – Iggy Van Der Wielen Mar 02 '12 at 13:07
  • Much better! I've reformatted the code - StackOverflow doesn't use HTML tags, but if you indent text by four spaces, it will be shown as code. You can apply that using the `{}` button in the editor. – Tom Anderson Mar 02 '12 at 14:23
  • However, two questions remain. (1) The title refers to a "public get", and you mention you're using the getter, but i don't see a getter being used in this code. Are you thinking of implicit use of getters by JAXB? (2) Where is the `@XmlIDREF` annotation, and how do you think it's relevant? – Tom Anderson Mar 02 '12 at 16:09
  • Correct i did not add the code of the Entity class. But i came further myself because the lazy annotation of hibernate in combination with jaxb does work with OneToMany relationship only not with the OneToOne relationship but there is no problem to keep the fetching strategy to eager for this variables – Iggy Van Der Wielen Mar 06 '12 at 09:01

1 Answers1

0

If you are marshalling lazy loaded attributes of your entity then it cannot be detached, aka it needs to be associated with an active session. Your options are:

  1. Retain the session the original entity was loaded in, long enough to complete the marshalling.
  2. Start a new sessions and merge your entity into it, then perform the marshalling.
  3. Make your lazy loaded attributes eager.

On a side note I find it odd that you are getting a null pointer exception. You must be swallowing a LazyInitializationException somewhere in your code.

Perception
  • 79,279
  • 19
  • 185
  • 195